Skip to end of metadata
Go to start of metadata
This procedure will help you create a new web application in Maven and deploy it to a Tomcat server in five simple steps.

Procedure

Prerequisites and Assumptions

  • Maven is already installed
  • A local instance of Tomcat is already installed and configured to run on port 8080
  • Optional - Eclipse is installed

Step One - Prepare the Tomcat Manager application

In order to deploy a web app to your Tomcat server, you will need to ensure that you can access the Tomcat Manager application at: http://localhost:8080/manager/html. Typically, you just need to ensure that your <tomcat>/conf/tomcat-users.xml file has the following defined:

In this case, we will be logging in to the Tomcat Manager app using:

username admin
password admin
Back to top

Step Two - Create a New Web App Using Maven

Next, we will use a Maven archetype to generate a new web application project.

Assuming that you will primarily use Eclipse as your IDE, you may wish to start the project within Eclipse. We'll start the project within Eclipse and then use Maven to fill the project in with the proper Maven web app structure.

  • In Eclipse create a new General project. New > Project... > General > Project
  • Project name: "sw" (name it what you wish, but remember to replace 'sw' with your project name in all instances where that appears in this document from here on out.)

The steps above just create a General project in your Eclipse workspace (e.g. a folder with a .project settings file inside of it).

Next, open a command prompt, cd into the 'sw' project directory, and then execute the following Maven command (all on one line):

You project will now have the following structure:

  • sw
    • webapp
      • src
        • main
          • resources
          • webapp
            • WEB-INF
              • web.xml
            • index.jsp
      • pom.xml
    • .project

In Eclipse, right-click on the project and select Refresh to see this stuff within Eclipse. (Note that the .project file will typically not appear within Eclipse, but it is there.)

Back to top

Step Three - Define Your Tomcat Server in Maven Settings

Open your Maven settings.xml file (e.g. C:\Documents and Settings\Administrator\.m2\settings.xml) and add a server 'myserver' with the credentials for logging into the Tomcat Manager application:

Back to top

Step Four - Point Your Pom to Your Tomcat Server

Open the pom.xml file in the 'sw' project and replace the <build> section so that it looks like this:

Here, we have added the Tomcat plugin for Maven. Note that the <configuration> section needs to point to the server you defined in settings.xml ('myserver'). The <finalName> and the <path> are used to tell the web context that you want to deploy to. In this case, we'll be able to access our application at http://localhost:8080/sw.

Back to top

Step Five - Build and Deploy the Web App

On a command prompt, you can now cd into the sw/webapp directory where the project pom exists. The execute the following command:

If you get a BUILD SUCCESSFUL message, you should then be able to access your web application at http://localhost:8080/sw/.

You should see "Hello World!" in your web browser (from the index.jsp page). Now you're all set and ready to start building the next killer web app!

If you try to deploy again with the same command, you will likely get a FAIL message because the application already exists at the path. For successive deployments, use the following command instead:

Next, you may want to enable log4j logging for your new application; check out How to setup Log4j in a web app - fast.

Share the Love

If you found this content useful, please let us know...

Labels:
maven maven Delete
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.
  1. Mar 25, 2010

    Anonymous

    Hi,
    placing the elements <username> and <password> in the <server> section in settings.xml
    doesn't work in my environment. I have to put them in the project's pom.xml.

    <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>tomcat-maven-plugin</artifactId>
    <configuration>
    <username>admin</username>
    <password>admin</password>
    </configuration>
    </plugin>

    Otherwise, I get an Tomcat authenfication failure.
    My environment: Java 1.5.0, Maven 2.2.1, Tomcat 5.5.28

    Greetings

  2. Mar 30, 2010

    Anonymous

    This topic ASSUMES you have the m2/settings.xml file.

    Where do I get one and what should it contain?

    1. Mar 31, 2010

      As described by the Maven documentation for settings.xml, your basic settings.xml file looks like this. You can start with this, in fact, even though many of the sections will remain empty...

      Now, at minimum, you will want to have access to a public maven repository, so you'll want to add one to the repositories section. The standard public Maven 2 repository is http://repo1.maven.org/maven2/

      The repositories go within the profiles section. Here is the example provided by Maven documentation:

      1. Apr 24, 2010

        Anonymous

        When I do thad I only get this message:

        [ERROR] BUILD FAILURE
        [INFO] ------------------------------------------------------------------------
        [INFO] A required plugin was not found: Plugin could not be found - check that the goal name is correct: Unable to download the artifact from any repository

        Try downloading the file manually from the project website.

        Then, install it using the command:
        mvn install:install-file -DgroupId=org.codehaus.mojo -DartifactId=tomcat-maven-plugin -Dversion=1.0-SNAPSHOT -Dpackaging=maven-plugin -Dfile=/path/to/file

        Alternatively, if you host your own repository you can deploy the file there:
        mvn deploy:deploy-file -DgroupId=org.codehaus.mojo -DartifactId=tomcat-maven-plugin -Dversion=1.0-SNAPSHOT -Dpackaging=maven-plugin -Dfile=/path/to/file -Durl=[url] -DrepositoryId=[id]

        org.codehaus.mojo:tomcat-maven-plugin:maven-plugin:1.0-SNAPSHOT

        from the specified remote repositories:
        central (http://repo1.maven.org/maven2)

        org.codehaus.mojo:tomcat-maven-plugin:maven-plugin:1.0-SNAPSHOT

        from the specified remote repositories:
        central (http://repo1.maven.org/maven2)

        1. Mar 23, 2011

          Anonymous

          Double check your <plugins> entries. Specifically, your <plugin> for tomcat-mave-plugin should be exactly what is listed above. The only part specific to your project/machine is in the <configuration> section.

      2. Jun 07, 2010

        Anonymous

        You don't want to recommend that people put <offline/> in their settings.xml file. That will put them in offline mode permanently.

  3. Dec 04, 2010

    Anonymous

    thank you very much

  4. Mar 29, 2011

    Anonymous

    Hello guys, I'm getting an exception and I can't make it work, i dunno what i'm doing wrong. Those are my files -->
    My pom.xml is:
    <project>
    ...

    <build>
    <finalName>servidor-identidades-webapp</finalName>
    <plugins>
    <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>tomcat-maven-plugin</artifactId>
    <version>1.1</version>
    <configuration>
    <server>myserver</server>
    <url>http://localhost:8080/manager</url>
    <path>/servidor-identidades-webapp</path>
    <warSourceDirectory>C:/Users/fulanis/workspace2/web/target/servidor-identidades-webapp.jar</warSourceDirectory>
    </configuration>
    </plugin>
    </plugins>
    </build>
    </project>

    my settings.xml:
    <settings>
    ...
    <servers>
    <server>
    <id>myserver</id>
    <username>admin</username>
    <password>admin</password>
    </server>
    </servers>
    </settings>

    and my tomcat-users.xml:

    <tomcat-users>

    <role rolename="manager-gui"/>
    <role rolename="manager"/>
    <role rolename="admin" />
    <user username="admin" password="admin" roles="manager,admin,manager-gui" />
    </tomcat-users>

    Hello I am getting the following exception:
    [ERROR] Failed to execute goal org.codehaus.mojo:tomcat-maven-plugin:1.1:deploy (default-cli) on project servidor-identidades-webapp: Cannot invoke Tomcat manager: Server returned HTTP response code: 403 for URL: http://localhost:8080/manager/deploy?path=%2Fservidor-identidades-webapp&war= -> [Help 1]
    org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.codehaus.mojo:tomcat-maven-plugin:1.1:deploy (default-cli) on project servidor-identidades-webapp: 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:319)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:534)
    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:107)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
    ... 19 more
    Caused by: java.io.IOException: Server returned HTTP response code: 403 for URL: http://localhost:8080/manager/deploy?path=%2Fservidor-identidades-webapp&war=
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1436)
    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

    1. Apr 05, 2011

      Anonymous

      I just ran into the same problem and after two hours of searching was able to solve it.

      If you're using Tomcat 7, you need to modify the deployment url in your pom.xml file. Instead of being <url>http://localhost:8080/manager</url> it should be <url>http://localhost:8080/manager/text</url>

      My guess is that in previous versions of Tomcat the appropriate deployment url was http://server.domain:port/manager/deploy but has now been changed to http://server.domain:port/manager/deploy/text

      Details of how to use the manager for deployment in Tomcat 7 can be found at:
      http://tomcat.apache.org/tomcat-7.0-doc/manager-howto.html#Supported_Manager_Commands

  5. May 08, 2011

    Anonymous

    I am using Tomcat 6.0.32 and I faced an issue (401/403 error) while using the approach mentioned in this article. I fixed my problem by updating my tomcat-users.xml to grant "manager-script" access to the deploying user as shown below:

    <tomcat-users>
    <role rolename="manager-gui"/>
    <role rolename="manager-script"/>
    <user username="tomcat" password="s3cret" roles="manager-gui, manager-script"/>
    </tomcat-users>