Compile, deploy and start Tomcat with Ant script
Introduction
Apache ant is a tool used to automate repetitive development tasks. One task that is particularly useful is the compile and deploy a web application. However, the process can be automated a little further by starting the Tomcat server after the final deploy task has finished.
Example Script
This example shows how to automate the starting of the Tomcat server. I use Apache Tomcat 7.0.27 (tomcat.apache.org) and Ant 1.8.4 (ant.apache.org).
The assumptions are that the penultimate task is called war and that there is a build.properties file that defines the required directories.
The Build Properties
Here is my build.properties file:
root.dir=.. server.location=E:/Development/servers server.name=apache-tomcat-7.0.27 server.webapp=webapps lib.dir=lib src.dir=src conf.dir=etc web.content=web project.name=shoppingCart build.dir=build dist.dir=dist
Define The Class Path
Firstly you must tell Ant where the Tomcat classpath is located. We use the path task as follows:
<path id="tomcat.class.path"> <fileset dir="${server.location}/${server.name}/lib"> <include name="**/*.jar"/> <include name="**/*.zip"/> </fileset> <pathelement location="${server.location}/${server.name} /bin/bootstrap.jar"/> <pathelement location="${server.location}/${server.name} /bin/tomcat-juli.jar"/> </path>
Tomcat Stop and Start Targets
We must then define the start and stop Tomcat targets as follows:
<target name="tomcat-start"> <echo>Stop Tomcat</echo> <java classname="org.apache.catalina.startup.Bootstrap" fork="true" classpathref="tomcat.class.path"> <jvmarg value="-Dcatalina.home=${server.location}/${server.name}"/> </java> </target> <target name="tomcat-stop" depends="tomcat-check-status" if="tomcat.started"> <echo>Start Tomcat</echo> <java classname="org.apache.catalina.startup.Bootstrap" fork="true" classpathref="tomcat.class.path"> <jvmarg value="-Dcatalina.home=${server.location}/${server.name}"/> <arg line="stop"/> </java> <sleep seconds="5"/> </target> <target name="tomcat-check-status"> <condition property="tomcat.started"> <socket server="localhost" port="8000"/> </condition> </target>
Check Tomcat Status
The final target called “tomcat-check-status” is called by the “tomcat-stop” and Tomcat is only stopped if the target returns that Tomcat is running.
Deploy Task
The last target we need to write is “deploy”. This target is dependent on the “war” target. A call to “tomcat-stop” is made before the application is deployed to the server. After the deployment, a call to “tomcat-start” is made and the server is started.
<target name="deploy" depends="war"> <sequential> <antcall target="tomcat-stop"/> <echo>Deploy to server</echo> <copy todir="${server.location}/${server.name}/${server.webapp}/ ${project.name}"> <fileset dir="${build.dir}"/> </copy> <antcall target="tomcat-start"/> </sequential> </target>
This is a very useful addition to the normal ant tasks that deploy web applications.
Interesting Links
If you want to learn more about the power of Apache Ant then consider the following books:
- Pro Apache Ant (Expert’s Voice in Java)
- Ant: The Definitive Guide, 2nd Edition
- Ant in Action
- Apache Ant: Questions and Answers
- Java Development with Ant
Related Articles
One of the most important developments in Java EE 8 and Java 9 will be support for HTTP/2. Tomcat 9 supports HTTP/2 but it must be configured to use TLS. Head on over to Configure Tomcat 9 for HTTP/2 to find out how to add the appropriate configurations.
Apache Ant can be used to automate the compilation, deployment, and launch of Tomcat all with a simple Ant script. Find out how to automate these task in my article: Compile, deploy and start Tomcat with Ant script.
Amazon Web Services offers free usage for 12 months that allows you to install Tomcat. The free tier consists of 14 services, the EC2 service is of most interest to developers. I show you how to install and configure Tomcat on an EC2 instance: Amazon Free Usage Tier: Installing Tomcat 7 on an EC2 Linux instance.
One of the most important configuration files in Tomcat is server.xml.I have posted an example of this file in my blog post: Tomcat server.xml example.
Tomcat is often set up with Apache and mod_jk cluster. In this article I will go through a common set-up for a small production environment: a single tier, load balanced application server cluster.
Leave a Reply