Maven lifecycle and plugins

Last week I used the mvn tomcat7:redeploy command to Deploy to Tomcat with maven. This command literally invokes the redeploy action of the tomcat7 plugin. Today, I’ll tie the tomcat7 plugin into the maven lifecycle.

The maven default build lifecycle has eight phases:

  • validate
  • compile
  • test
  • package
  • integration-test
  • verify
  • install
  • deploy

I’ll bind the tomcat7:redeploy action to the install lifecycle phase. This is as simple as adding an new executions element to the plugin configuration. As shown below, the executions element includes an execution element that associates the deploy redeploy plugin goal to the install maven phase.

I’ve also updated the plugin configuration to set the path from the build.finalName.

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
$ git diff                                                                                            Amber  6:25
diff --git a/pom.xml b/pom.xml
index 7e41e9d..0fe5b04 100644
--- a/pom.xml
+++ b/pom.xml
@@ -27,10 +27,19 @@
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
+ <executions>
+ <execution>
+ <id>tomcat7-redeploy</id>
+ <phase>install</phase>
+ <goals>
+ <goal>redeploy</goal>
+ </goals>
+ </execution>
+ </executions>
<configuration>
<url>http://xxxxxx-nnnnnn.use1-2.nitrousbox.com:8080/manager/text</url>
<server>nitrous</server>
- <path>/jersey-json</path>
+ <path>/${project.build.finalName}</path>
</configuration>
</plugin>
</plugins>

With that done, I can now deploy the webapp to tomcat with the mvn install command:

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

[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 (tomcat7-redeploy) < package @ jersey-gson <<<
[INFO]
[INFO] --- tomcat7-maven-plugin:2.2:deploy (tomcat7-redeploy) @ jersey-gson ---
[INFO] Deploying war to http://xxxxxx-nnnnnn.use1-2.nitrousbox.com:8080/jersey-gson
Uploading: http://xxxxxx-nnnnnn.use1-2.nitrousbox.com:8080/manager/text/deploy?path=%2Fjersey-gson
Uploaded: http://xxxxxx-nnnnnn.use1-2.nitrousbox.com:8080/manager/text/deploy?path=%2Fjersey-gson (3609 KB at 672.5 KB/sec)

[INFO] tomcatManager status code:200, ReasonPhrase:OK
[INFO] OK - Deployed application at context path /jersey-gson
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 30.661 s
[INFO] Finished at: 2014-09-27T20:55:06-04:00
[INFO] Final Memory: 15M/245M
[INFO] ------------------------------------------------------------------------

Sep 28: corrected tomcat7 plugin goal from deploy to redeploy