Deploy war application to multiple tomcat servers using ANT Script
It is tedious work if you have say 5 servers and you need to deploy same application in all servers manually.
So, I developed below ant script which will deploy the application in all servers with single command.
All you need to do is to update server details in the build.properties file.
build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project name="OneClickDeploy" default="deploy" basedir=".">
<property file="build.properties" />
<property name="war-file-name" value="${project-name}.war" />
<property name="build-directory" value="${build-directory}" />
<taskdef name="deploy" classname="org.apache.catalina.ant.DeployTask" />
<taskdef name="undeploy" classname="org.apache.catalina.ant.UndeployTask" />
<taskdef name="reload" classname="org.apache.catalina.ant.ReloadTask" />
<target name="deploy" description="deploy to tomcat">
<echo>deploying to server ${tomcat-manager-url1} </echo>
<!--
undeploy application
<undeploy
url="${tomcat-manager-url1}"
username="${tomcat-manager-username1}"
password="${tomcat-manager-password1}"
path="/${project-name}"
/>
-->
<!--
reload application
<reload
url="${tomcat-manager-url1}"
username="${tomcat-manager-username1}"
password="${tomcat-manager-password1}"
path="/${project-name}"
war="file:/${build-directory}/${war-file-name}
/>
-->
<deploy
url="${tomcat-manager-url1}"
username="${tomcat-manager-username1}"
password="${tomcat-manager-password1}"
path="/${project-name}"
war="file:/${build-directory}/${war-file-name}"
/>
<echo>deploying to server ${tomcat-manager-url2} </echo>
<!--
undeploy application
<undeploy
url="${tomcat-manager-url2}"
username="${tomcat-manager-username2}"
password="${tomcat-manager-password2}"
path="/${project-name}"
war="file:/${build-directory}/${war-file-name}"
/>
-->
<!--
reload application
<reload
url="${tomcat-manager-url2}"
username="${tomcat-manager-username2}"
password="${tomcat-manager-password2}"
path="/${project-name}"
war="file:/${build-directory}/${war-file-name}
/>
-->
<deploy
url="${tomcat-manager-url2}"
username="${tomcat-manager-username2}"
password="${tomcat-manager-password2}"
path="/${project-name}"
war="file:/${build-directory}/${war-file-name}"
/>
</target>
</project>
build.properties
#build.properties file
#project name or the application context name. if your application accessible using http://localhost:8080/iiq then provide project-name as iiq
project-name=myapp
#compiled project war file directory
build-directory=C:/Users/Desktop/OneClickDeploy
#Server1 tomcat details, tomcat user should have role 'manager-script', update context.xml in webapps/manager/META-INF to access manager console from out-site server machine
tomcat-manager-url1=http://<HOSTNAME>:8080/manager/text
tomcat-manager-username1=root
tomcat-manager-password1=root
tomcat-manager-url2=http://<HOSTNAME>:8080/manager/text
tomcat-manager-username2=root
tomcat-manager-password2=root
Prerequisite:
1. Local machine
o Working ANT console
o Tomcat libraries should be included to classpath or to ANTs lib directory
o Keep build.properties and build.xml in same directory
2. Server machine
o Up and Running Tomcat server
o Tomcat server manager console should be accessible from local machine
Ex: You should be able to access http://<SERVER_HOST>:8080/manager from local
machine
Required settings can be updated in context.xml in /webapps/manager/META-INF
o Tomcat user with deploy permission using scripts
Ex: add below line in tomcat-users.xml
<user username="root" password="root" roles="admin-
gui,manager-gui,manager-script"/>
Execution: Simply open command prompt on build.xml directory and run ant command
Comments
Post a Comment