Deploy to Tomcat with maven

Previously, I used maven to generate and package a jersey webapp. Today, I’ll extend our project to deploy the war file to Tomcat. This basically amounts to configuring Tomcat to accept connections from maven and configuring maven to use a specific Tomcat instance.

For our sample project, I’ll use a Tomcat instance on Nitrous.io. If you’re also using Nitrous, then you’ll probably need to start by installing tomcat on your instance:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$ parts install tomcat
=> Extracting archive...
=> Installing...
=> Activating...
=> Installed tomcat 7.0.53
To start the Apache Tomcat server:
$ parts start tomcat

To stop the Apache Tomcat server:
$ parts stop tomcat

Apache Tomcat config is located at:
$ /home/action/.parts/packages/tomcat/7.0.53/conf

Default webapps at:
$ /home/action/.parts/packages/tomcat/7.0.53/webapps

Default port: 8080

Next, you’ll need to enable access to the tomcat manager application. Edit the conf/tomcat-users.xml to enable the manager-text manager-script role and assign a user to that role (use a more complex password).

1
2
3
4
5
6
7
8
9
10
11
$ tail .parts/packages/tomcat/7.0.53/conf/tomcat-users.xml
<!--
<role rolename="tomcat"/>
<role rolename="role1"/>
<user username="tomcat" password="tomcat" roles="tomcat"/>
<user username="both" password="tomcat" roles="tomcat,role1"/>
<user username="role1" password="tomcat" roles="role1"/>
-->
<role rolename="manager-script"/>
<user username="maven" password="password" roles="manager-script"/>
</tomcat-users>

Update your maven settings file with the corresponding username and password. Since the settings file is external to your project, your credentials shouldn’t leak out when you distribute your project.

1
2
3
4
5
6
7
8
9
10
11
12
$ cat ~/.m2/settings.xml
<settings>
<localRepository>/MyApps/Developer/MavenRepo</localRepository>

<servers>
<server>
<id>nitrous</id>
<username>maven</username>
<password>password</password>
</server>
</servers>
</settings>

Add a plugin to your pom.xml for tomcat7. Note the configuration lines for the:

  • url [of your tomcat instance]
  • server [defined in your maven settings file]
  • path [of your webapp]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
$ git diff
diff --git a/pom.xml b/pom.xml
index ef884e8..db07a75 100644
--- a/pom.xml
+++ b/pom.xml
@@ -22,6 +22,17 @@
<target>1.7</target>
</configuration>
</plugin>
+
+ <plugin>
+ <groupId>org.apache.tomcat.maven</groupId>
+ <artifactId>tomcat7-maven-plugin</artifactId>
+ <version>2.2</version>
+ <configuration>
+ <url>http://xxxxxx-nnnnnn.use1-2.nitrousbox.com:8080/manager/text</url>
+ <server>nitrous</server>
+ <path>/jersey-json</path>
+ </configuration>
+ </plugin>
</plugins>
</build>

Now maven can deploy your war with tomcat7:deploy (use tomcat7:redeploy to redeploy).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
$ mvn tomcat7:deploy
[INFO] Scanning for projects...
[INFO]
[INFO]
...

[INFO] Webapp assembled in [106 msecs]
[INFO] Building war: /Projects/webapps/tutorial/jersey-gson/target/jersey-gson.war
[INFO] WEB-INF/web.xml already added, skipping
[INFO]
[INFO] <<< tomcat7-maven-plugin:2.2:deploy (default-cli) < package @ jersey-gson <<<
[INFO]
[INFO] --- tomcat7-maven-plugin:2.2:deploy (default-cli) @ jersey-gson ---
[INFO] Deploying war to http://xxxxxx-nnnnnn.use1-2.nitrousbox.com:8080/jersey-json
Uploading: http://xxxxxx-nnnnnn.use1-2.nitrousbox.com:8080/manager/text/deploy?path=%2Fjersey-json
Uploaded: http://xxxxxx-nnnnnn.use1-2.nitrousbox.com:8080/manager/text/deploy?path=%2Fjersey-json (3609 KB at 660.9 KB/sec)

[INFO] tomcatManager status code:200, ReasonPhrase:OK
[INFO] OK - Deployed application at context path /jersey-json
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 22.585 s
[INFO] Finished at: 2014-09-21T17:23:14-04:00
[INFO] Final Memory: 19M/245M
[INFO] ------------------------------------------------------------------------

And a quick test to verify that the application has been successfully deployed.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ curl http://xxxxxx-nnnnnn.use1-2.nitrousbox.com:8080/jersey-json/
<html>
<body>
<h2>Jersey RESTful Web Application!</h2>
<p><a href="webapi/myresource">Jersey resource</a>
<p>Visit <a href="http://jersey.java.net">Project Jersey website</a>
for more information on Jersey!
</body>
</html>
$ ack --nopager url-pattern --xml
src/main/webapp/WEB-INF/web.xml
16: <url-pattern>/webapi/*</url-pattern>
$ ack --nopager -w @Path
src/main/java/com/ideoplex/tutorial/MyResource.java
11:@Path("myresource")
$ curl http://xxxxxx-nnnnnn.use1-2.nitrousbox.com:8080/jersey-json/webapi/myresource
Got it!%

View the jersey-gson project on github.

27 Sep: Corrected tomcat role.