I am trying to have multiple Gradle tasks in my build.gradle
file to deploy a war to different environments. I want to be able to have a deployToLocal
task along with deployToQA
and deployToDev
, after a war file has been generated.
Of course, this means that I want to stop the Tomcat service, delete existing war files and expanded wars in the Tomcat webapps directory, copy the war, and restart Tomcat again to deploy the service.
My question is twofold:
- I have specified the location of the war that I will be copying along with all the hostname, port and username/ password information. My question is: how do I specify which location in the remote environments (directory structure) the war should be copied into?
- For the end I am trying to achieve, is this the correct way to move forward?
My code so far:
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
}
I want to call task gradle deployToLocal
and expect the behavior that local Tomcat instance is stopped, the war is copied to my Tomcat\webapps folder (with any previously existing files or folders in the same folder having been deleted), and then the Tomcat service is started again and the war is deployed.
This doesn't work, and obviously I am missing something. I also scoured the web for a solid fully-worked-out example of an implementation of the gradle cargo/ tomcat plugin without much luck. Could someone please point me in the right direction?
Thank you in advance for any help!