Today we will look into the Tomcat Maven Plugin (tomcat7-maven-plugin) that helps us in deploying our WAR files to Tomcat easily.
Tomcat Maven Deploy
Recently I was developing a web application and after doing the build, I used to copy the generated WAR file to Tomcat webapps directory. It was time-consuming, boring and sometimes I used to miss the updated WAR file to copy and notice it later on.
Then I decided to look for some configurations through which I can configure Maven build to deploy the generated WAR file into tomcat automatically.
My first approach was to use Maven plugins to copy the generated WAR file to tomcat deploy directory.
For example, below was the pom.xml file for my simple web application, I have removed all the unwanted stuff to keep it clear.
<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>MyWebapp</groupId>
<artifactId>MyWebapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
I used maven-antrun-plugin
to add another task for the integration-test
phase where I am copying the WAR file to my local tomcat directory.
<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>MyWebapp</groupId>
<artifactId>MyWebapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<deployFolder>/Users/pankaj/Dev_Softwares/apache-tomcat-7.0.32/webapps</deployFolder>
</properties>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>integration-test</phase>
<configuration>
<tasks>
<copy file="target/${pom.build.finalName}.war" tofile="${deployFolder}/${pom.build.finalName}.war" />
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<finalName>${project.artifactId}</finalName>
</build>
</project>
The change was really simple, create a property to define the tomcat deploy directory and add finalName configuration to skip version details from the WAR file.
mvn clean integration-test package – it will generate the WAR file and deploy into tomcat too.
mvn clean package – It will just build the project, will not try to deploy it into tomcat container.
Above approach solved my problem, but there were two shortcomings;
- Since tomcat deploy folder is hardcoded in the pom.xml, if I change tomcat server location then I would have to change it here too. Also because it’s my user configuration, I can’t check in this code into repository.
- I can’t use this approach to deploy the application into any remote server.
Tomcat7 Maven Plugin
Because of above shortcomings, I looked online for better solutions and found that there is a plugin already for this job – Tomcat Maven Plugin.
We can use this plugin with some configuration settings to deploy our application into Tomcat server using their management console API.
Tomcat Maven Plugin
Before we can use Apache Tomcat Maven Plugin, we need to perform some configuration settings. Let’s look at these configurations one by one. Note that I am using Tomcat-7, if you are using any other version then some configurations might need to be changed accordingly.
-
- Adding Manager Roles and User in Tomcat 7: We need to add
manager-gui
andmanager-script
roles intomcat-users.xml
file and create a user with these roles. Just add below lines to your tomcat-users.xml file and restart the server.TOMCAT-HOME/conf/tomcat-users.xml
<role rolename="manager-gui" /> <role rolename="manager-script" /> <user username="tomcat" password="tomcat" roles="manager-gui,manager-script" />
- Adding server to Maven settings.xml file: Next step is to add a server in maven settings.xml file, as shown below.
$HOME/.m2/settings.xml
<settings xmlns="https://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd"> <servers> <server> <id>Tomcat</id> <username>tomcat</username> <password>tomcat</password> </server> </servers> </settings>
Notice that username and password should be same as configured in the earlier step.
- Adding Manager Roles and User in Tomcat 7: We need to add
tomcat7-maven-plugin example
-
- Maven Project pom.xml changes: Final step is to configure Tomcat Maven Plugin in the web application pom.xml file. Just add below lines to your pom.xml plugins section.
<plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <url>https://localhost:9090/manager/text</url> <server>Tomcat</server> <path>/MyWebapp</path> </configuration> </plugin>
tomcat6-maven-plugin example
If you are using Tomcat-6, below configurations should be used instead.
<plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat6-maven-plugin</artifactId> <version>2.2</version> <configuration> <url>https://localhost:9090/manager</url> <server>Tomcat</server> <path>/MyWebapp</path> </configuration> </plugin>
- Maven Project pom.xml changes: Final step is to configure Tomcat Maven Plugin in the web application pom.xml file. Just add below lines to your pom.xml plugins section.
That’s it, now we can use below commands to deploy, undeploy, redeploy our application through maven command.
Tomcat 7 Maven Plugin Deploy Commands
- mvn tomcat7:deploy
- mvn tomcat7:undeploy
- mvn tomcat7:redeploy
Tomcat 6 Maven Plugin Deploy Commands
For Tomcat-6, you should use below commands.
- mvn tomcat6:deploy
- mvn tomcat6:undeploy
- mvn tomcat6:redeploy
Below is the sample output produced by the deploy command for tomcat7-maven-plugin.
pankaj:MyWebapp pankaj$ mvn tomcat7:deploy
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building MyWebapp 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> tomcat7-maven-plugin:2.2:deploy (default-cli) @ MyWebapp >>>
[INFO]
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ MyWebapp ---
[debug] execute contextualize
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/pankaj/Documents/workspace/j2ee/MyWebapp/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ MyWebapp ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ MyWebapp ---
[debug] execute contextualize
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/pankaj/Documents/workspace/j2ee/MyWebapp/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ MyWebapp ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.10:test (default-test) @ MyWebapp ---
[INFO] No tests to run.
[INFO] Surefire report directory: /Users/pankaj/Documents/workspace/j2ee/MyWebapp/target/surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Results :
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-war-plugin:2.3:war (default-war) @ MyWebapp ---
[INFO] Packaging webapp
[INFO] Assembling webapp [MyWebapp] in [/Users/pankaj/Documents/workspace/j2ee/MyWebapp/target/MyWebapp-0.0.1-SNAPSHOT]
[INFO] Processing war project
[INFO] Copying webapp resources [/Users/pankaj/Documents/workspace/j2ee/MyWebapp/WebContent]
[INFO] Webapp assembled in [26 msecs]
[INFO] Building war: /Users/pankaj/Documents/workspace/j2ee/MyWebapp/target/MyWebapp-0.0.1-SNAPSHOT.war
[INFO]
[INFO] <<< tomcat7-maven-plugin:2.2:deploy (default-cli) @ MyWebapp <<<
[INFO]
[INFO] --- tomcat7-maven-plugin:2.2:deploy (default-cli) @ MyWebapp ---
[INFO] Deploying war to https://localhost:9090/MyWebapp
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See https://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Uploading: https://localhost:9090/manager/text/deploy?path=%2FMyWebapp
Uploaded: https://localhost:9090/manager/text/deploy?path=%2FMyWebapp (2 KB)
[INFO] tomcatManager status code:200, ReasonPhrase:OK
[INFO] OK - Deployed application at context path /MyWebapp
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.185s
[INFO] Finished at: Mon Sep 15 18:07:00 IST 2014
[INFO] Final Memory: 13M/159M
[INFO] ------------------------------------------------------------------------
You can go to your Tomcat Manager App and confirm that application is deployed or you can directly access the application in the browser to confirm it. That’s all for tomcat maven plugin and tomcat7-maven-plugin example.