This post is tutorial show how to write a gant script for tomcat deployment automation, and to keep your life simple, just using the
grails tomcat plugin which provides all we need.
Set TOMCAT_HOME
before you can start write gant script, you need to make sure you have Tomcat installed and TOMCAT_HOME set to the location where you installed it.
Do this in .bashrc
Add the following content to .bashrc
TOMCAT_HOME=/home/htxiong/apache-tomcat-7.0.39
export TOMCAT_HOME
Set up a user in Tomcat and assign the essential roles to it.
See http://tomcat.apache.org/tomcat-7.0-doc/manager-howto.html for more info.
Add the following content to $TOMCAT_HOME/conf/tomcat-users.xml.
<user username="deployer" password="secret" roles="manager-gui,manager-script,manager-jmx,manager-status,manager"/>
Write a Gant script
Create the script
run the grails create-script command as follows:
$ grails create-script tomcat-deploy
With that done, you should have a TomcatDeploy.groovy file in the scripts directory of your project.
Modify the script TomcatDeploy.groovy
To begin with, you’re going to need to figure out the path to the Tomcat installation directory.
Inspecting the TOMCAT_HOME environment variable can help you achieve this:
grailsHome = ant.project.properties."environment.GRAILS_HOME"
tomcatHome = ant.project.properties."environment.TOMCAT_HOME"
the next thing to do is include the War.groovy script
available in GRAILS_HOME/scripts. The _GrailsWar.groovy template contains targets that allow you to construct a valid WAR file:
includeTargets << grailsScript("_GrailsWar") OR includeTargets << grailsScript("War")
To take advantage of the Tomcat Ant tasks, you have to define them by calling the taskdef method.
This method relates to the <taskdef> target of Ant, so defining Ant tasks in Gant is pretty much identical to doing so in pure Ant—minus the angle brackets:
ant.path(id:"tomcat.lib.path") {
fileset(dir:"${tomcatHome}/lib",includes:"*.jar")
}
ant.taskdef(name:"deploy",
classname:"org.apache.catalina.ant.DeployTask",
classpathref:"tomcat.lib.path")
Then, moving onto the main target of the TomcatDeploy.groovy script, you can change it to depend on the war target, which will ensure a valid WAR file is constructed before the rest of the code runs:
target(main: "Deploys the Grails application to Tomcat") {
depends war
...
}
Once that is done, you need to establish the destination to publish the WAR to by adding following line to you target.
def dest = argsMap.params ? argsMap.params[0] : "http://localhost:8080/manager"
OR for Tomcat 7
def dest = argsMap.params ? argsMap.params[0] : "http://localhost:8080/manager/text"
The final step is left to the deploy target supplied by the org.apache.cat-alina.ant.
Add the following content to your target.
deploy( war:warName,
url:dest,
path:serverContextPath,
username:"deployer",
password:"secret")
DONE
The full copy of TomcatDeploy.groovy will looks like:
grailsHome = ant.project.properties."environment.GRAILS_HOME"
tomcatHome = ant.project.properties."environment.TOMCAT_HOME"
includeTargets << grailsScript("War")
ant.path(id:"tomcat.lib.path") {
fileset(dir:"${tomcatHome}/lib",includes:"*.jar")
}
ant.taskdef(name:"deploy",
classname:"org.apache.catalina.ant.DeployTask",
classpathref:"tomcat.lib.path")
target(main: "The description of the script goes here!") {
depends(parseArguments, war)
def dest = argsMap.params ? argsMap.params[0] : "http://localhost:8080/manager/text"
deploy(war:warName,
url:dest,
path:serverContextPath,
username:"deployer",
password:"secret")
}
setDefaultTarget(main)
Add support to updeploy and list
The taskdef method of undeploy and list related to <taskdef> targets of Ant are org.apache.catalina.ant.UndeployTask and org.apache.catalina.ant.ListTask respectively.
So enable supporting of updeploy and list, just add the following content to script.
ant.taskdef(name:"list",classname:"org.apache.catalina.ant.ListTask")
ant.taskdef(name:"undeploy",classname:"org.apache.catalina.ant.UndeployTask")
and the corresponding list and undeploy methods body are shown below.
list(
url:dest,
username:user,
password:pass)
undeploy(
url:dest,
path:serverContextPath,
username:user,
password:pass)
Cheers!