Home > maven, tomcat > Running tomcat7 with maven2/3

Running tomcat7 with maven2/3

Here I will describe how to setup Maven with Tomcat 7. Also I will show a small Ant script for deploying simple view changes like changes in css, html, js etc.  –  changes that don’t require a rebuild of the war file.

I could let the tomcat-maven-plugin run tomcat for me using the

mvn tomcat:run

, but I already have a tomcat installed, that I want to use. I need to tell the plugin about it in the pom.xml:

<plugin>
	<groupId>org.codehaus.mojo</groupId>
	<artifactId>tomcat-maven-plugin</artifactId>
	<version>1.1</version>
	<configuration>
		<server>local_tomcat7</server>
		<url>http://localhost:8080/manager/text</url>
		<path>/appname</path>
	</configuration>
</plugin>

Maven looks for ‘local_tomcat7’ in ~/.m2/setting.xml, where the username and password for the tomcat manager can be found in the servers section:

<servers>
	<server>
		<id>local_tomcat7</id>
		<username>admin</username>
		<password>mypw</password>
	</server>
</servers>

Last we need to create the manager user in tomcat. This is done in …/apache-tomcat-7.0.12/conf/tomcat-users.xml:

<tomcat-users>
	<user name="admin" password="mypw" roles="manager-script,manager-gui" />
</tomcat-users>

Now the setup is ready for running, deploying and redeploying on tomcat via the tomcat manager.
When working on the view part of the application, it is not really necessary to build a new war file and redeploy it via the manager – its a bit slow. Lets say I only made changes in a java-script file, then I really only need to copy that file directly to …\apache-tomcat-7.0.12\webapps\appname. For this I’m using a small ant script – build.xml:

<?xml version="1.0"?>
<project name="appname" basedir=".">
    <property name="appserver.home" value="C:/java/apache/apache-tomcat-7.0.12"/>
    <property name="deploy.path" value="${appserver.home}/webapps"/>
    <property name="web.dir" value="src/main/webapp"/>
    <property name="name" value="appname"/>
    <target name="copyFilesToServer" description="Deploy files">
        <copy todir="${deploy.path}/${name}" preservelastmodified="true">
            <fileset dir="${web.dir}">
                <include name="**/*"/>
            </fileset>
        </copy>
    </target>
</project>

, with this script it is very fast to deploy simple changes in the view.

Advertisements
  1. Kristian Mandrup
    May 30, 2011 at 10:18

    Hey, really nice post. Just what I needed! Tomcat 7 rules 🙂

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: