tomcat7-maven-plugin – Tomcat Maven Plugin to Deploy WAR

Filed Under: Java

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="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://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="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://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;

  1. 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.
  2. I can’t use this approach to deploy the application into any remote server.

Tomcat7 Maven Plugin

tomcat7 maven plugin, tomcat maven plugin, 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.

  1. Adding Manager Roles and User in Tomcat 7: We need to add manager-gui and manager-script roles in tomcat-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" />
    
  2. 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="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
                              http://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.

  3. tomcat7-maven-plugin example

  4. 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>http://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>http://localhost:9090/manager</url>
    		<server>Tomcat</server>
    		<path>/MyWebapp</path>
    	</configuration>
    </plugin>
    
  5. 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 http://localhost:9090/MyWebapp  
    SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
    SLF4J: Defaulting to no-operation (NOP) logger implementation
    SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
    Uploading: http://localhost:9090/manager/text/deploy?path=%2FMyWebapp
    Uploaded: http://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.

Comments

  1. fred says:

    I’m using the configuration but the plugin is using a default url, what am i doint wrong?

    org.apache.tomcat.maven
    tomcat7-maven-plugin
    2.2

    https://localhost:8443/manager/text
    /${project.build.finalName}

  2. SARVESH says:

    I am following the same for deployment. But my environment is Ubuntu.
    I made changes in /etc/maven/settings.xml and added user in tomcat-users.xml.
    But when I fire mvn tomcat7:deploy from terminal, I get error following error.

    [INFO] ————————————————————————
    [INFO] BUILD FAILURE
    [INFO] ————————————————————————
    [INFO] Total time: 2.859s
    [INFO] Finished at: Wed Feb 03 12:43:29 IST 2016
    [INFO] Final Memory: 9M/108M
    [INFO] ————————————————————————
    [ERROR] No plugin found for prefix ‘tomcat7’ in the current project and in the plugin groups [org.apache.maven.plugins, org.codehaus.mojo] available from the repositories [local (/root/.m2/repository), central (http://repo.maven.apache.org/maven2)] -> [Help 1]
    org.apache.maven.plugin.prefix.NoPluginFoundForPrefixException: No plugin found for prefix ‘tomcat7’ in the current project and in the plugin groups [org.apache.maven.plugins, org.codehaus.mojo] available from the repositories [local (/root/.m2/repository), central (http://repo.maven.apache.org/maven2)]

  3. ghost_rider says:

    I have errors, when i try did like in tutorial. When i run tomcat manual: “Cannot invoke Tomcat manager: Software caused connection abort” and when i didn’t run it manual: “Cannot invoke Tomcat manager: Connection refused: connect”

  4. Raghu says:

    If I want to deploy on a remote machine how to deploy? How to know where to deploy? In other words, how to make sure CATALINA service is running on remote machine and that how to make the automation (in script) to confirm whether the jar/war can be deployed?

  5. Hitesh Patel says:

    Thanks a lot. It’s working for me as you suggested.

  6. Arasu says:

    Dear Pankaj,

    First i would like to thank for all your awesome examples..And then Can you briefly explain What is maven? Where it’s used?

    Thanks in advance.

  7. Marija says:

    Thank you very much! Very helpful tutorial!

  8. Khan says:

    Hey Pankaj , do you have detailed explination about maven, where to use?why and when and how ?

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages