Matt RaibleMatt Raible is a Java Champion and Developer Advocate at Okta. developer.okta.com

The JHipster Mini-Book The JHipster Mini-Book is a guide to getting started with hip technologies today: Angular, Bootstrap, and Spring Boot. All of these frameworks are wrapped up in an easy-to-use project called JHipster.

This book shows you how to build an app with JHipster, and guides you through the plethora of tools, techniques and options you can use. Furthermore, it explains the UI and API building blocks so you understand the underpinnings of your great application.

For book updates, follow @jhipster-book on Twitter.

10+ YEARS


Over 10 years ago, I wrote my first blog post. Since then, I've authored books, had kids, traveled the world, found Trish and blogged about it all.

Deploying to Tomcat using Ant

If you're using Tomcat 4.1.x, did you know you can deploy using an Ant task that ships with Tomcat. You'll need to add $CATALINA_HOME/server/lib/catalina-ant.jar to your classpath, but then you can configure your ant task as follows:

<taskdef name="deploy" classname="org.apache.catalina.ant.DeployTask"/>

<deploy url="${manager.url}"
         username="${manager.username}"
         password="${manager.password}"
         path="/${name}"
         war="file:/${dist.dir}/${name}.war" />

I haven't tried it, but it looks cool. Right now I use a simple copy task that works pretty well for me, so no need to change at this point.

<target name="deploy" depends="package-web" if="tomcat.home"
    description="unwar into the servlet container's deployment directory">
          
    <unwar src="${webapp.dist}/${webapp.war}" 
        dest="${tomcat.home}/webapps/${webapp.name}"/>
    
</target>

If you know of any advantages to using Tomcat's deploy task, or you'd like to share your experience using it - please post a comment.

Posted in Java at Feb 07 2003, 06:58:04 AM MST 12 Comments
Comments:

Well, no experience using it, but it looks like the Tomcat deploy task lets you deploy to a remote Tomcat server, possibly using the manager webapp. It probably also triggers a reload of the webapp.

Posted by Anthony Eden on February 07, 2003 at 08:47 AM MST #

It allows me to build and test my apps with one ant command. Im using a remove, webapp, install sequence to refresh and reload my app. BTW I read your resume app, thanks a lot!, I learned hibernate and xdoclet with it. Terrific example, thanks! Im tweaking it right now, I refactored most of the code of UserDAOHibernate.getUser() to its superclass (Im reusing that logic with other classes), added a SwitchSkinAction to the tiles-defs layout, and some uml.png to javadoc packages.

Posted by Jano on February 07, 2003 at 10:16 AM MST #

Jano - RE: struts-resume Cool - feel free to submit patches as it's only a .5 version at this point. You can check it out from CVS from http://www.sf.net/projects/struts I plan adding/refactoring the following in the next couple weeks: 1. Hibernate connection to get Session at Web layer and pass it down. 2. Remember Me feature. 3. Further development to make it a workable/usable application. I hope to have the entire app in a usable state by March 1st. In March, I need to start looking for a job in FL very actively - and I'd like to use this app to host my resume.

Posted by Matt on February 07, 2003 at 10:27 AM MST #

I have that ant task to deploy quite a bit and it works great. No more starting and stoping Tomcat all the time anymore! I didn't use it with war file options however.

Posted by Kurt on February 07, 2003 at 11:32 AM MST #

I use the reload function to tell Tomcat to reload my application after a successful build. I pretty much never have to restart my server and I don't have to go through the Tomcat admin interface anymore either. Really nice.

Posted by John Munsch on February 07, 2003 at 02:53 PM MST #

This is a great feature - I've been using it for a while too. But if I recall correctly, one of the drawbacks is that anything you send to Tomcat this way is not persistent across invocations. That is, if Tomcat gets restarted, you will be left with what was last deployed the "normal" way. It was a half hour of mystery for me as I figured this one out. I'm sure it's written down somewhere but ... hehe.

Posted by David Perkowski on February 07, 2003 at 07:25 PM MST #

I've used the tomcat deploy task for a long time on several projects.

This is appropriate when you want to deploy the entire war file. I love using it for production deployments.

Windows users should note that you are going to have problems with the tomcat deployer unless you set the context options antiResourceLocking and antiJarLocking to true in $CATALINA_HOME/conf/context.xml.

(See: tomcat FAQ on locking issues)

This is not recommended for production since it is a performance pig, but if you do not do the above, the jars from the previous app reload will not unload and you will have problems. I think the last comment on this thread alludes to the problem.

The workaround is to restart tomcat each time you deploy and this is definitely not optimal.

Even with the copy file approach on windows, you probably want to make sure the tomcat options are set to true.

The way that tomcat avoids windows locking issues is to copy the exploded war contents into tmp/context-[0-n] and redirect inbound requests to that versioned directory.

After a while, you'll want to clean out that tmp directory because tomcat does not clean up after itself. It just keeps creating new directories after the app is reloaded.

This is not an issue under linux and it isn't recommended to use those options at all since linux doesn't have the same issues with locking resources and files.

Posted by Rick Fisk on March 09, 2007 at 11:26 AM MST #

I've used the tomcat deploy task for a long time on several projects.

This is appropriate when you want to deploy the entire war file. I love using it for production deployments.

Windows users should note that you are going to have problems with the tomcat deployer unless you set the context options antiResourceLocking and antiJarLocking to true in $CATALINA_HOME/conf/context.xml.

(See: tomcat FAQ on locking issues)

This is not recommended for production since it is a performance pig, but if you do not do the above, the jars from the previous app reload will not unload and you will have problems. I think the last comment on this thread alludes to the problem.

The workaround is to restart tomcat each time you deploy and this is definitely not optimal.

Even with the copy file approach on windows, you probably want to make sure the tomcat options are set to true.

The way that tomcat avoids windows locking issues is to copy the exploded war contents into tmp/context-[0-n] and redirect inbound requests to that versioned directory.

After a while, you'll want to clean out that tmp directory because tomcat does not clean up after itself. It just keeps creating new directories after the app is reloaded.

This is not an issue under linux and it isn't recommended to use those options at all since linux doesn't have the same issues with locking resources and files.

Posted by Rick Fisk on March 09, 2007 at 11:28 AM MST #

I've used the tomcat deploy task for a long time on several projects.

This is appropriate when you want to deploy the entire war file. I love using it for production deployments.

Windows users should note that you are going to have problems with the tomcat deployer unless you set the context options antiResourceLocking and antiJarLocking to true in $CATALINA_HOME/conf/context.xml.

(See: tomcat FAQ on locking issues)

This is not recommended for production since it is a performance pig, but if you do not do the above, the jars from the previous app reload will not unload and you will have problems. I think the last comment on this thread alludes to the problem.

The workaround is to restart tomcat each time you deploy and this is definitely not optimal.

Even with the copy file approach on windows, you probably want to make sure the tomcat options are set to true.

The way that tomcat avoids windows locking issues is to copy the exploded war contents into tmp/context-[0-n] and redirect inbound requests to that versioned directory.

After a while, you'll want to clean out that tmp directory because tomcat does not clean up after itself. It just keeps creating new directories after the app is reloaded.

This is not an issue under linux and it isn't recommended to use those options at all since linux doesn't have the same issues with locking resources and files.

Posted by Rick Fisk on March 09, 2007 at 11:29 AM MST #

<?xml version="1.0" encoding="UTF-8"?> <project name="Flyball" default="tomcat-redeploy-webapp" basedir="."> <property name="tomcat.home" value="E:\Tomcat5" /> <property name="tomcat.server" value="127.0.0.1" /> <property name="tomcat.manager.url" value="http://${tomcat.server}:8080/manager" /> <!-- Create a tomcat user in ${tomcat.home}\conf\tomcat-users.xml with role manager. <user username="admin" password="admin" roles="manager,admin"/> --> <property name="tomcat.username" value="admin" /> <property name="tomcat.password" value="admin" /> <property name="webapp.name" value="Flyball" /> <property name="output.dir" value="C:\temp" /> <property name="webapp.dir" value=".\main\webapp" /> <path id="project.class.path" description="Classpath from where Tomcat Jars will be loaded."> <fileset dir="${tomcat.home}/bin"> <include name="*.jar" /> </fileset> <!-- Tomcat 5.5 --> <fileset dir="${tomcat.home}/common/lib"> <include name="*.jar" /> </fileset> <!-- Tomcat 6 <fileset dir="${tomcat.home}/lib"> <include name="*.jar" /> </fileset> --> <pathelement path="${tomcat.home}\server\lib\catalina-ant.jar" /> </path> <taskdef resource="org/apache/catalina/ant/catalina.tasks" classpathref="project.class.path" /> <target name="tomcat-start-server" description="Start tomcat server."> <java jar="${tomcat.home}/bin/bootstrap.jar" fork="true" classpathref="project.class.path"> <jvmarg value="-Dcatalina.home=${tomcat.home}" /> </java> </target> <target name="tomcat-stop-server" description="Stop tomcat server."> <java jar="${tomcat.home}/bin/bootstrap.jar" fork="true" classpathref="project.class.path"> <jvmarg value="-Dcatalina.home=${tomcat.home}" /> <arg line="stop" /> </java> </target> <target name="tomcat-start-debug-server" description="Start tomcat in debug mode."> <java jar="${tomcat.home}/bin/bootstrap.jar" fork="true" classpathref="project.class.path"> <jvmarg value="-Dcatalina.home=${tomcat.home}" /> <jvmarg value="-Xdebug" /> <jvmarg value="-Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n" /> </java> </target> <!-- Task to generate war file. --> <target name="tomcat-prepare-war" description="Prepare war file to deploy."> <war destfile="${output.dir}\${webapp.name}.war" webxml="${webapp.dir}/WEB-INF/web.xml" basedir="${webapp.dir}" /> </target> <target name="tomcat-deploy-webapp" depends="tomcat-prepare-war" description="Deploy webapp on tomcat."> <deploy url="${tomcat.manager.url}" username="${tomcat.username}" password="${tomcat.password}" war="file:${output.dir}\${webapp.name}.war" path="/${webapp.name}" logerror="true" /> </target> <target name="tomcat-undeploy-webapp" description="Undeploy webapp on tomcat."> <undeploy url="${tomcat.manager.url}" username="${tomcat.username}" password="${tomcat.password}" path="/${webapp.name}" /> </target> <target name="tomcat-reload-webapp" description="Reload webapp on tomcat."> <reload url="${tomcat.manager.url}" username="${tomcat.username}" password="${tomcat.password}" path="/${webapp.name}" /> </target> <target name="tomcat-stop-webapp" description="Stop webapp on tomcat."> <stop url="${tomcat.manager.url}" username="${tomcat.username}" password="${tomcat.password}" path="/${webapp.name}" /> </target> <target name="tomcat-list-all-webapps" description="List all the webapps deployed on tomcat."> <list url="${tomcat.manager.url}" username="${tomcat.username}" password="${tomcat.password}" /> </target> <target name="tomcat-redeploy-webapp" description="Redeploy webapp on tomcat."> <antcall target="tomcat-undeploy-webapp" /> <antcall target="tomcat-deploy-webapp" /> </target> </project>

Posted by Sameer on November 25, 2007 at 03:13 AM MST #

<?xml version="1.0" encoding="UTF-8"?> <project name="Flyball" default="tomcat-redeploy-webapp" basedir="."> <property name="tomcat.home" value="E:\Tomcat5" /> <property name="tomcat.server" value="127.0.0.1" /> <property name="tomcat.manager.url" value="http://${tomcat.server}:8080/manager" /> <!-- Create a tomcat user in ${tomcat.home}\conf\tomcat-users.xml with role manager. <user username="admin" password="admin" roles="manager,admin"/> --> <property name="tomcat.username" value="admin" /> <property name="tomcat.password" value="admin" /> <property name="webapp.name" value="Flyball" /> <property name="output.dir" value="C:\temp" /> <property name="webapp.dir" value=".\webapp" /> <path id="project.class.path" description="Classpath from where Tomcat Jars will be loaded."> <fileset dir="${tomcat.home}/bin"> <include name="*.jar" /> </fileset> <!-- Tomcat 5.5 --> <fileset dir="${tomcat.home}/common/lib"> <include name="*.jar" /> </fileset> <!-- Tomcat 6 <fileset dir="${tomcat.home}/lib"> <include name="*.jar" /> </fileset> --> <pathelement path="${tomcat.home}\server\lib\catalina-ant.jar" /> </path> <taskdef resource="org/apache/catalina/ant/catalina.tasks" classpathref="project.class.path" /> <target name="tomcat-start-server" description="Start tomcat server."> <java jar="${tomcat.home}/bin/bootstrap.jar" fork="true" classpathref="project.class.path"> <jvmarg value="-Dcatalina.home=${tomcat.home}" /> </java> </target> <target name="tomcat-stop-server" description="Stop tomcat server."> <java jar="${tomcat.home}/bin/bootstrap.jar" fork="true" classpathref="project.class.path"> <jvmarg value="-Dcatalina.home=${tomcat.home}" /> <arg line="stop" /> </java> </target> <target name="tomcat-start-debug-server" description="Start tomcat in debug mode."> <java jar="${tomcat.home}/bin/bootstrap.jar" fork="true" classpathref="project.class.path"> <jvmarg value="-Dcatalina.home=${tomcat.home}" /> <jvmarg value="-Xdebug" /> <jvmarg value="-Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n" /> </java> </target> <!-- Task to generate war file. --> <target name="tomcat-prepare-war" description="Prepare war file to deploy."> <war destfile="${output.dir}\${webapp.name}.war" webxml="${webapp.dir}/WEB-INF/web.xml" basedir="${webapp.dir}" /> </target> <target name="tomcat-deploy-webapp" depends="tomcat-prepare-war" description="Deploy webapp on tomcat."> <deploy url="${tomcat.manager.url}" username="${tomcat.username}" password="${tomcat.password}" war="file:${output.dir}\${webapp.name}.war" path="/${webapp.name}" logerror="true" /> </target> <target name="tomcat-undeploy-webapp" description="Undeploy webapp on tomcat."> <undeploy url="${tomcat.manager.url}" username="${tomcat.username}" password="${tomcat.password}" path="/${webapp.name}" /> </target> <target name="tomcat-reload-webapp" description="Reload webapp on tomcat."> <reload url="${tomcat.manager.url}" username="${tomcat.username}" password="${tomcat.password}" path="/${webapp.name}" /> </target> <target name="tomcat-stop-webapp" description="Stop webapp on tomcat."> <stop url="${tomcat.manager.url}" username="${tomcat.username}" password="${tomcat.password}" path="/${webapp.name}" /> </target> <target name="tomcat-list-all-webapps" description="List all the webapps deployed on tomcat."> <list url="${tomcat.manager.url}" username="${tomcat.username}" password="${tomcat.password}" /> </target> <target name="tomcat-redeploy-webapp" description="Redeploy webapp on tomcat."> <antcall target="tomcat-undeploy-webapp" /> <antcall target="tomcat-deploy-webapp" /> </target> </project>

Posted by Sameer on November 25, 2007 at 03:15 AM MST #

Does anybody know if you could check if the application is already deployed? When you are calling undeploy and the webapp is not deployed, it throws an error...

Posted by Patrick on June 02, 2011 at 02:27 PM MDT #

Post a Comment:
  • HTML Syntax: Allowed