How to deploy Maven based war file to Tomcat

In this tutorial, we will show you how to use Maven-Tomcat plugin to package and deploy a WAR file to Tomcat, both in Tomcat 6 and 7.

Libraries used :

  1. Maven 3
  2. Tomcat 6.0.37
  3. Tomcat 7.0.53
Tomcat 7
Deploy URL = http://localhost:8080/manager/text
Command = mvn tomcat7:deploy

Tomcat 6
Deploy URL = http://localhost:8080/manager/
Command = mvn tomcat6:deploy

1. Tomcat 7 Example

This example shows you how to package and deploy a WAR file on Tomcat 7.

1.1 Tomcat Authentication
Add an user with roles manager-gui and manager-script.

%TOMCAT7_PATH%/conf/tomcat-users.xml
<?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>

1.2 Maven Authentication
Add above Tomcat’s user in the Maven setting file, later Maven will use this user to login Tomcat server.

%MAVEN_PATH%/conf/settings.xml
<?xml version="1.0" encoding="UTF-8"?>
<settings ...>
	<servers>
 
		<server>
			<id>TomcatServer</id>
			<username>admin</username>
			<password>password</password>
		</server>
 
	</servers>
</settings>

1.3 Tomcat7 Maven Plugin
Declares a Maven Tomcat plugin.

pom.xml
	<plugin>
		<groupId>org.apache.tomcat.maven</groupId>
		<artifactId>tomcat7-maven-plugin</artifactId>
		<version>2.2</version>
		<configuration>
			<url>http://localhost:8080/manager/text</url>
			<server>TomcatServer</server>
			<path>/mkyongWebApp</path>
		</configuration>
	</plugin>

How it works?
During deployment, it tells Maven to deploy the WAR file to Tomcat server via “http://localhost:8080/manager/text” , on path “/mkyongWebApp“, using “TomcatServer” (in settings.xml) username and password for authentication.

1.4 Deploy to Tomcat
Commands to manipulate WAR file on Tomcat.

mvn tomcat7:deploy 
mvn tomcat7:undeploy 
mvn tomcat7:redeploy

Example

> mvn tomcat7:deploy
 
...
[INFO] Deploying war to http://localhost:8080/mkyongWebApp
Uploading: http://localhost:8080/manager/text/deploy?path=%2FmkyongWebApp&update=true
Uploaded: http://localhost:8080/manager/text/deploy?path=%2FmkyongWebApp&update=true (13925 KB at 35250.9 KB/sec)
 
[INFO] tomcatManager status code:200, ReasonPhrase:OK
[INFO] OK - Deployed application at context path /mkyongWebApp
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 8.507 s
[INFO] Finished at: 2014-08-05T11:35:25+08:00
[INFO] Final Memory: 28M/308M
[INFO] ------------------------------------------------------------------------
maven-war-file-deploy-tomcat7

2. Tomcat 6 Example

This example shows you how to package and deploy a WAR file on Tomcat 6. The steps are same with Tomcat 7, just the deploy url and command name are different.

2.1 Tomcat Authentication

%TOMCAT6_PATH%/conf/tomcat-users.xml
<?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>

2.2 Maven Authentication

%MAVEN_PATH%/conf/settings.xml
<?xml version="1.0" encoding="UTF-8"?>
<settings ...>
	<servers>
 
		<server>
			<id>TomcatServer</id>
			<username>admin</username>
			<password>password</password>
		</server>
 
	</servers>
</settings>

2.3 Tomcat6 Maven Plugin

pom.xml
	<plugin>
		<groupId>org.apache.tomcat.maven</groupId>
		<artifactId>tomcat6-maven-plugin</artifactId>
		<version>2.2</version>
		<configuration>
			<url>http://localhost:8080/manager</url>
			<server>TomcatServer</server>
			<path>/mkyongWebApp</path>
		</configuration>
	</plugin>

2.4 Deploy to Tomcat

mvn tomcat6:deploy 
mvn tomcat6:undeploy 
mvn tomcat6:redeploy

Example

> mvn tomcat6:deploy
 
...
[INFO] Deploying war to http://localhost:8080/mkyongWebApp
Uploading: http://localhost:8080/manager/deploy?path=%2FmkyongWebApp
Uploaded: http://localhost:8080/manager/deploy?path=%2FmkyongWebApp (13925 KB at 32995.5 KB/sec)
 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 22.652 s
[INFO] Finished at: 2014-08-05T12:18:54+08:00
[INFO] Final Memory: 30M/308M
[INFO] ------------------------------------------------------------------------

References

  1. Apache Tomcat 7 Manager App HOW-TO
  2. Apache Tomcat 6 Manager App HOW-TO
  3. Tomcat Maven Plugin
  4. Tomcat Maven Plugin – Context Goals
Tags :

About the Author

mkyong
Founder of Mkyong.com and HostingCompass.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

  • Pingback: water ionizer loans()

  • Pingback: alkaline water brands()

  • Pingback: alkaline water machine()

  • Pingback: car parking()

  • Pingback: car parking()

  • Pingback: youporn()

  • Pingback: laan penge()

  • Pingback: TVPackages.net()

  • Pingback: DIRECTV Satellite Television()

  • Pingback: kangen water()

  • Pingback: alkaline()

  • Pingback: alkaline water()

  • Pingback: watch movies online()

  • Pingback: free movie downloads()

  • Pingback: watch movies online free()

  • Pingback: Blue Coaster33()

  • Pingback: How to quickly build a REST webservice using STS 3.3.0, Maven | ramibatal()

  • Savvy

    Hey Thanks for above tutorial.
    I want to generate a valid WAR file from my github repo.
    Is it possible using maven? How would you do it?
    Can I use same logic as you mentioned above, only using github credentials instead of tomcat?
    Plz guide me ASAP.
    Thanks

  • Alan Bartlett

    Awesome work.
    Very easy to implement and works like a charm. Thanks for the tip.

  • Guruprasad Zapate

    Hi Mkyong,
    Thanks for the great post.Keep posting stuff like this:)
    I have a question that how to configure tomcat path to use maven tomcat plugin .I have searched a lot on web but couldn’t find suggestions that help me to get out of this problem.
    Please can you provide any suggestion how to configure tomcat path to use this plugin.

  • Chathu

    Hi,

    I use tomcat-7.0.42, and I manually copied maven build war file into server.
    But it do not work. It gives 404 error when hit the url.

    • Dzmitry

      As I see in this topic configuration for tomcat6. Fotr tomcat7 you shud specify url as http://127.0.0.1:8080/manager/html

      • change

        me too

    • Sergio Rosales

      For Tomcat 7 just change the user role in tomcat-users.xml and please paste this instead of the example information and it will works.

  • Bhavuk

    I’ve successfully configured Bonita to Tomcat. However, when I’m trying to deploy my “.war” file on the server, the following error appears

    FAIL – Application at context path /MyProcess–1.0 could not be started

    how can I resolve this??

  • http://www.subit.at Boky

    I also had following problem:

    Cannot invoke Tomcat manager: Server returned HTTP response code: 403 for URL

    while using Tomcat 7. The solution was using maven goal “tomcat7:deploy”. For details see http://tomcat.apache.org/maven-plugin-2.0/tomcat7-maven-plugin/plugin-info.html

  • Pingback: Configurar maven para hacer deploy en Tomcat | Jesús L.C.()

  • nitesh

    [ERROR] Failed to execute goal org.codehaus.mojo:tomcat-maven-plugin:1.1:redeploy (default-cli) on project cms-sql: Cannot invoke Tomcat manager: Server returned HTTP response code: 403 for URL: http://10.70.4.200:9090/manager/deploy?path=%2FCMSService&war=&update=true -> [Help 1]
    org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.codehaus.mojo:tomcat-maven-plugin:1.1:redeploy (default-cli) on project cms-sql: Cannot invoke Tomcat manager

    • NV

      I had trouble deploying as I had incorrectly set the roles in my tomcat users file.
      I fixed it using:

      POM.xml

      org.codehaus.mojo
      tomcat-maven-plugin

      http://127.0.0.1:8080/manager/html
      TomcatServer
      /[yourApp]
      admin
      password

      tomcat_users.xml

      • NV

        I had trouble deploying as I had incorrectly set the roles in my tomcat users file.
        I fixed it using:

        Tomcat-users xml

         
        &lt;tomcat-users&gt;
          &lt;role rolename=&quot;manager&quot;/&gt;
          &lt;role rolename=&quot;admin&quot;/&gt;
          &lt;role rolename=&quot;manager-script&quot;/&gt;
          &lt;role rolename=&quot;manager-gui&quot;/&gt;
          &lt;user username=&quot;admin&quot; password=&quot;password&quot; roles=&quot;admin,manager,manager-script,manager-gui&quot;/&gt;
        &lt;/tomcat-users&gt;
         
        POM.xml
         
        &lt;plugin&gt;
        	&lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
        	&lt;artifactId&gt;tomcat-maven-plugin&lt;/artifactId&gt;
        	&lt;configuration&gt;
        		&lt;url&gt;http://127.0.0.1:8080/manager/html&lt;/url&gt;
        		&lt;server&gt;TomcatServer&lt;/server&gt;
        		&lt;path&gt;/[your app]&lt;/path&gt;
        		&lt;username&gt;admin&lt;/username&gt;
        		&lt;password&gt;password&lt;/password&gt;
        	&lt;/configuration&gt;
        &lt;/plugin&gt;
        • soc

          Work for me only with

           &lt;url&gt;http://127.0.0.1:8080/manager/text&lt;/url&gt;
          • Shasik

            Thanks both.

          • Balaji

            Ahh, I read this comment only after fixing the code myself to make it run on tomcat7 and with the updated tomcat7-maven-plugin…

  • nitesh

    hi
    below warning is coming while deploying the war file with mvn tomcat:deploy
    [WARNING] Unrecognised tag: ‘server’ (position: START_TAG seen … | variables for plugins in the POM.\n |\n |–>\n\n… @183:9) @ /usr/local/apache-maven/conf/settings.xml, line 183, column 9

  • Aditya

    I was also getting same error
    [ERROR] Failed to execute goal org.codehaus.mojo:tomcat-maven-plugin:1.1:exploded (default-cli) on project Struts-Redirect: Cannot invoke Tomcat manager: http://127.0.0.1
    :9091/manager/deploy?path=%2FStruts-Redirect&war=file%3A%2FE%3A%2Fstufff%2F111111111struts%2FStruts2-Hello-World-Example%2FStruts2Example%2Ftarget%2FStruts-Redirect%2F ->
    [Help 1]

    Paste below lines in manager.xml present at $CATALINA_BASE/conf/[enginename]/[hostname] folder ($CATALINA_BASE\conf\Catalina\localhost\manager.xml)

    For more information refer to Manager App HOW-TO @
    http://tomcat.apache.org/tomcat-6.0-doc/manager-howto.html

  • TestConfig

    If it doesn’t work put your settings.xml file under the location m2/settings.xml file.
    Hope this helps.

  • area

    how can i make war file from maven project?

    • http://www.mkyong.com mkyong

      mvn war:war or mvn package to run the full building life cycles

  • Anand Singh

    Thank you so much mkyong.

  • http://[email protected] Rami

    Hi Mr. Mkyong,
    Thank you very much for this handy explanation.
    I have a question related to the usage of proxy server.
    To access to my remote server I have to pass by a proxy server.
    How can I deal with this issue?
    Thank you very much
    Best Regards

  • Gary

    Thanks MKYong and Santosh! This helped so much!

  • andy

    hii mr mkyong

    how to integration struts2 , spring3 and hibernate 3 using maven
    thank you mr mkyong

  • andy

    hii mr mkyong how to integration struts2,hibernate3,spring3 using maven
    thamk you mr mkyong

  • santosh

    When i am trying to deploy the project using “mvn tomcat:deploy” I am getting the error below. I have made the changes as specified by you. Can you please help me out.

    [ERROR] Failed to execute goal org.codehaus.mojo:tomcat-maven-plugin:1.1:exploded (default-cli) on project Struts-Redirect: Cannot invoke Tomcat manager: http://127.0.0.1
    :9091/manager/deploy?path=%2FStruts-Redirect&amp;amp;war=file%3A%2FE%3A%2Fstufff%2F111111111struts%2FStruts2-Hello-World-Example%2FStruts2Example%2Ftarget%2FStruts-Redirect%2F -&amp;gt;
     [Help 1]
    org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.codehaus.mojo:tomcat-maven-plugin:1.1:exploded (default-cli) on project Struts-Redirect
    : Cannot invoke Tomcat manager
            at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:217)
            at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
            at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
           //...
            at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)
    Caused by: org.apache.maven.plugin.MojoExecutionException: Cannot invoke Tomcat manager
            at org.codehaus.mojo.tomcat.AbstractCatalinaMojo.execute(AbstractCatalinaMojo.java:149)
            at org.codehaus.mojo.tomcat.AbstractWarCatalinaMojo.execute(AbstractWarCatalinaMojo.java:70)
            at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:107)
            at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
            ... 19 more
    Caused by: java.io.FileNotFoundException: http://127.0.0.1:9091/manager/deploy?path=%2FStruts-Redirect&amp;amp;war=file%3A%2FE%3A%2Fstufff%2F111111111struts%2FStruts2-Hello-World
    -Example%2FStruts2Example%2Ftarget%2FStruts-Redirect%2F
            at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1434)
            at org.codehaus.mojo.tomcat.TomcatManager.invoke(TomcatManager.java:604)
            at org.codehaus.mojo.tomcat.TomcatManager.deployImpl(TomcatManager.java:662)
            at org.codehaus.mojo.tomcat.TomcatManager.deploy(TomcatManager.java:245)
            at org.codehaus.mojo.tomcat.AbstractDeployMojo.deployWar(AbstractDeployMojo.java:175)
            at org.codehaus.mojo.tomcat.AbstractDeployMojo.invokeManager(AbstractDeployMojo.java:85)
            at org.codehaus.mojo.tomcat.AbstractCatalinaMojo.execute(AbstractCatalinaMojo.java:141)
            ... 22 more
    • http://www.mkyong.com mkyong

      Do you see war file in your project build folder?

      • santosh

        Yes.
        The war file is there.
        The error show above is after maven starts deploying the war.

        I searched on google also but didn’t find any solution for this.

        • http://www.mkyong.com mkyong

          How about if you copy the Maven generated war file into Tomcat folder manually?

          • santosh

            Hi,
            I am using tomcat 7.0.6.The following changes worked for me.

            1)File : tomcat-users.xml

            &lt;role rolename=&quot;manager-gui&quot;/&gt;
            &lt;user username=&quot;tomcat&quot; password=&quot;tomcat&quot; roles=&quot;manager-gui&quot;/&gt;

            2)maven: settings.xml

            &lt;server&gt;
            	&lt;id&gt;TomcatServer&lt;/id&gt;
            	&lt;username&gt;tomcat&lt;/username&gt;
            	&lt;password&gt;tomcat&lt;/password&gt;
            &lt;/server&gt;

            3)pom.xml

            &lt;configuration&gt;
            	&lt;url&gt;http://127.0.0.1:8080/manager/html&lt;/url&gt;
            	&lt;server&gt;TomcatServer&lt;/server&gt;
            	&lt;path&gt;/myWebapp&lt;/path&gt;
            &lt;/configuration&gt;
          • King George

            Hi mkyong,

            I am getting following error while running the code.

            [INFO] Deploying war to http://localhost:8080/ShoppingCartWeb
            [DEBUG] No server specified for authentication - using defaults
            [INFO] ------------------------------------------------------------------------
            [INFO] BUILD FAILURE
            [INFO] ------------------------------------------------------------------------
            [INFO] Total time: 22.905s
            [INFO] Finished at: Fri Aug 17 16:25:45 BST 2012
            [INFO] Final Memory: 6M/16M
            [INFO] ------------------------------------------------------------------------
            [ERROR] Failed to execute goal org.codehaus.mojo:tomcat-maven-plugin:1.1:deploy (default-cli) on project ShoppingCartWeb: Cannot invoke Tomcat manager: Unexpected end of file from server -&gt; [Help 1]
            org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.codehaus.mojo:tomcat-maven-plugin:1.1:deploy (default-cli) on project ShoppingCartWeb: Cannot invoke Tomcat manager
                    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:217)
                    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
                    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
                    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
                    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
                    at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
                    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
                    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:320)
                    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
                    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
                    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
                    at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
                    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                    at java.lang.reflect.Method.invoke(Method.java:597)
                    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
                    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
                    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)
                    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)
            Caused by: org.apache.maven.plugin.MojoExecutionException: Cannot invoke Tomcat manager
                    at org.codehaus.mojo.tomcat.AbstractCatalinaMojo.execute(AbstractCatalinaMojo.java:149)
                    at org.codehaus.mojo.tomcat.AbstractWarCatalinaMojo.execute(AbstractWarCatalinaMojo.java:70)
                    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)
                    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
                    ... 19 more
            Caused by: java.net.SocketException: Unexpected end of file from server
                    at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:781)
                    at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:640)
                    at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:778)
                    at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:640)
                    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1195)
                    at org.codehaus.mojo.tomcat.TomcatManager.invoke(TomcatManager.java:604)
                    at org.codehaus.mojo.tomcat.TomcatManager.deployImpl(TomcatManager.java:662)
                    at org.codehaus.mojo.tomcat.TomcatManager.deploy(TomcatManager.java:295)
                    at org.codehaus.mojo.tomcat.AbstractDeployWarMojo.deployWar(AbstractDeployWarMojo.java:85)
                    at org.codehaus.mojo.tomcat.AbstractDeployMojo.invokeManager(AbstractDeployMojo.java:85)
                    at org.codehaus.mojo.tomcat.AbstractCatalinaMojo.execute(AbstractCatalinaMojo.java:141)
                    ... 22 more
            [ERROR]
            [ERROR]

            my pom.xml is

            &lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
              xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd&quot;&gt;
              &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
              &lt;groupId&gt;com.boogies.shoppingcartweb&lt;/groupId&gt;
              &lt;artifactId&gt;ShoppingCartWeb&lt;/artifactId&gt;
              &lt;packaging&gt;war&lt;/packaging&gt;
              &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
              &lt;name&gt;ShoppingCartWeb Maven Webapp&lt;/name&gt;
              &lt;url&gt;http://maven.apache.org&lt;/url&gt;
              &lt;dependencies&gt;
                &lt;dependency&gt;
                  &lt;groupId&gt;junit&lt;/groupId&gt;
                  &lt;artifactId&gt;junit&lt;/artifactId&gt;
                  &lt;version&gt;3.8.1&lt;/version&gt;
                  &lt;scope&gt;test&lt;/scope&gt;
                &lt;/dependency&gt;
              &lt;/dependencies&gt;
              &lt;build&gt;
                &lt;finalName&gt;ShoppingCartWeb&lt;/finalName&gt;
                 &lt;plugins&gt; 
                &lt;plugin&gt;
            	&lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
            	&lt;artifactId&gt;tomcat-maven-plugin&lt;/artifactId&gt;
            	&lt;configuration&gt;
            		&lt;url&gt;http://127.0.0.1:8080/manager&lt;/url&gt;
            		&lt;server&gt;TomcatServer&lt;/server&gt;
            		&lt;path&gt;/ShoppingCartWeb&lt;/path&gt;
            	&lt;/configuration&gt;
            	&lt;/plugin&gt;
                    &lt;/plugins&gt;
              &lt;/build&gt;
            &lt;/project&gt;

            and

            settings.xml contains server information

             &lt;server&gt;
                    &lt;id&gt;TomcatServer&lt;/id&gt;
            		&lt;username&gt;admin&lt;/username&gt;
            		&lt;password&gt;password&lt;/password&gt;
                &lt;/server&gt;

            How this tomcat plugin works? How tomcat plugin contact to tomcat server.DO i need to start tomcat server manually for that?

            Thanks in advance,
            King George