Make Tomcat Part of Your Maven Build Process
Join the DZone community and get the full member experience.Join For Free
- compile the source code
- perform unit tests
- build WAR file
- automatically deploy WAR file
- run integration tests
This blog covers using version 1 of the tomcat-maven-plugin details of which are available from codehause. Since I started using this plugin some time ago, the project has moved under the Apache umbrella of the Tomcat project, where version 2 is currently under development and is currently available as a beta release.
Setting up the Maven Tomcat plugin consists of three steps.
- Setup a Tomcat manager account.
- Add the manager credentials to your settings.xml
- Add the Maven Tomcat plugin to your POM file.
Setup a Tomcat Manager AccountSetting up a Tomcat manager account is a prerequisite when it comes to using the Tomcat Maven Plugin. There’s been a huge amount written about this on the web, so I’m not going to go into too much detail, except to say that the config varies between Tomcat versions. In Tomcat 7 you need to update your Tomcat configuration’s tomcat_users.xml file adding something like this:1
<?xml version="1.0" encoding="UTF-8"?> <tomcat-users> <role rolename="manager-gui"/> <role rolename="manager-script"/> <user username="admin" password="password" roles="manager-gui, manager-script"/> </tomcat-users>
Assuming that you’re running locally with default security settings, then you can test this out by typing: http://localhost:8080/manager in to your browser. You’ll then be presented with the default security challenge dialogue and you’ll be able to login with your new credentials...
…and see your server’s manager page.
Add the manager credentials to your settings.xmlThe next step is to tell Maven about your Tomcat manager’s user name and password. To do this you need to add something like this:
<server> <id>myserver</id> <username>admin</username> <password>password</password> </server>
...to the <servers> section your Maven settings.xml file. Obviously the username and password must match those in your tomcat-users.xml file. Note the id tag, you’ll see this again in the next section.
Add the Maven Tomcat plug in to your POM fileThis final step gets to the heart of the matter. The Maven Tomcat Plugin is configured by adding the following XML to the <plugins> section of your project’s POM file:
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>tomcat-maven-plugin</artifactId> <version>1.1</version> <configuration> <server>myserver</server> <url>http://localhost:8080/manager/text</url> </configuration> </plugin>
This is really straight forward; however, there are a couple of points to note. Firstly, in the <configuration> section, you’ll see the server id of myserver. This must match the server id that you added to your settings.xml file in the previous section. Secondly, the server’s url setting seems, for no given reason, to have to end with "/manager/text", for example: "http://localhost:8080/manager/text". I guess that this is explained somewhere, but I can’t find it - all I know is that it just works.
You can now start your server and run a Maven build using the "mvn install tomcat:deploy" command. Once installed, switch to the "tomcat:redeploy" goal.
1 The main difference between Tomcat 7 and previous versions is that in Tomcat 7 the manager role has been split into several distinct sub-roles. Therefore in Tomcat 7 you need to specify roles like manager-gui etc, whereas in previous versions the role of manager would suffice.
Opinions expressed by DZone contributors are their own.