gpt4 book ai didi

tomcat - 使用 Gradle Cargo 插件部署到多个环境

转载 作者:行者123 更新时间:2023-11-28 21:46:23 26 4
gpt4 key购买 nike

我正在尝试在我的 build.gradle 文件中包含多个 Gradle 任务,以将 war 部署到不同的环境。在生成 war 文件后,我希望能够执行 deployToLocal 任务以及 deployToQAdeployToDev

当然,这意味着我要停止Tomcat服务,删除Tomcat webapps目录下已有的war文件和展开的war,复制war,再次重启Tomcat部署服务。

我的问题有两个:

  1. 我已经指定了我要复制的 war 地点连同所有主机名、端口和用户名/密码信息。我的问题是:如何指定远程环境(目录结构) war 应该被复制进入?
  2. 对于我要达到的目标,这是前进的正确方法吗?

到目前为止我的代码:

task deleteDirLocalhost(type: Delete){
//delete fileTree(dir: local_Tomcat_webapps_dir)
delete fileTree(dir: 'C:\\Tomcat7.0\\webapps')
}

task deployToLocal{
dependsOn war, tomcatStop, deleteDirLocalhost
println "Starting Cargo"
println "Using war from: $warLocation" // warLocation is a variable defined in gradle.properties file

cargo {
containerId = 'tomcat7x'
port = TomcatPortLocalhost as Integer
local {
homeDir = file(local_Tomcat_webapps_dir) // local_Tomcat_webapps_dir is a variable defined in gradle.properties file
}

deployable {
file = file(warLocation) // This is where I specify where the war file is to be copied from. In the 'local' section, I have specified 'homeDir' as the location where I want this file to be copied to. Is that correct usage?
context = 'web-application'
}
}
tomcatRunWar
}

task deployToQA{
dependsOn war, tomcatStop, deleteDirQA
println "Starting Cargo"
println "Using war from: $warLocation"

cargo {
containerId = 'tomcat7x'
port = TomcatPortQA as Integer
remote {
hostname = server_address_qa // all these are variables defined in the gradle.properties file
username = username_qa
password = password_qa
}

deployable {
file = file(warLocation) // This is where I specify the location of the war file. How do I specify in the 'remote' section in which directory in the server this file should be copied to?
context = 'web-application'
}
}
tomcatRunWar
}

task deployToDev{
dependsOn war, tomcatStop, deleteDirDev
println "Starting Cargo"
println "Using war from: $warLocation"

cargo {
containerId = 'tomcat7x'
port = TomcatPortDev as Integer
remote {
hostname = RT3_webapp_address_dev
username = username_dev
password = password_dev
}

deployable {
file = file(warLocation)
context = 'web-application'
}
}
tomcatRunWar
}

我想调用任务 gradle deployToLocal 并期望本地 Tomcat 实例停止的行为,war 被复制到我的 Tomcat\webapps 文件夹(与同一文件夹中的任何以前存在的文件或文件夹已被删除),然后再次启动Tomcat服务,部署war。

这行不通,显然我遗漏了一些东西。我还在网上搜索了 gradle cargo/tomcat 插件实现的可靠且完整的示例,但运气不佳。有人可以指出我正确的方向吗?

提前感谢您的帮助!

最佳答案

Tomcat 和 Cargo 插件是两个截然不同的插件,它们无法以您尝试解决问题的方式相互配合。 Tomcat 插件仅为 Tomcat 提供一个内存中的嵌入式容器(意味着您不必进行本地安装),而 Cargo 需要您指向本地容器安装或远程 URL。出于您的目的,我将专门使用 Cargo 插件。

您在任务中定义的所有代码都在配置阶段执行。在多个任务中使用 Cargo 插件的 DSL 会覆盖之前的声明。为了更好地理解执行阶段与配置阶段,我建议查看 Gradle 在线用户指南。请记住,Cargo 通常不提供远程容器的管理功能(启动/停止)。 README file 中也提到了这一点的插件。如果您想执行类似的操作,则必须编写运行远程 SSH 命令的任务。

除了无法使用插件启动/停止容器外,您的实际问题可以通过多种方式解决。您要么使用 Cargo 插件 DSL 并动态分配远程主机名、端口、用户名、密码,要么创建多个任务。看起来您仍然想为每个环境创建多个任务。为此应用 cargo-base 插件并创建两个 CargoDeployRemote 类型的任务,一个用于您的开发,一个用于您的 QA 环境。在那种情况下,您没有使用插件公开的 DSL。当然,您还可以为本地 Tomcat 安装创建另一个具有适当任务类型的任务。

apply plugin: 'cargo-base'

import com.bmuschko.gradle.cargo.convention.Deployable
import com.bmuschko.gradle.cargo.tasks.remote.CargoDeployRemote

task deployToQA(type: CargoDeployRemote) {
containerId = 'tomcat7x'
hostname = server_address_qa
port = TomcatPortQA as Integer
username = username_qa
password = password_qa
deployables = [new Deployable(file: file(warLocation), context: 'web-application')]
}

关于tomcat - 使用 Gradle Cargo 插件部署到多个环境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21715645/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com