Skip to end of metadata
Go to start of metadata

About

This procedure will help you create a new web application using the m2eclipse Apache Maven plugin for Eclipse, and then deploy and run it on the Tomcat server defined in your Eclipse IDE.

Procedure

Prerequisites and Assumptions

This document assumes that the following software packages have already been installed and properly configured on your system:

Procedure

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

In Eclipse, create a new Maven Project. File > New > Other... > Maven Project.

 

 

In this first screen shown above, you can typically accept the defaults and click Next >.

 

 

Find and select the maven archetype called maven-archetype-webapp, as shown above, and click Next >.

 

 

Complete the fields shown above as appropriate to your own project. The Artifact Id is going to be your project's name in Eclipse (and typically the app context in the URL to your web app). Typically, the Group Id and Artifact Id combine to make the base package.

Click Finish.

 

You should then end up with the following project structure in the Eclipse Package Explorer view:

At this point, you may notice that you cannot add the web app project to the Tomcat server that's in the Eclipse IDE's Servers view when you right-click on the server and select Add and Remove..., so let's fix that.

Right-click on the project in the Eclipse Project Explorer view and select Properties. When the Properties dialog appears, select the Project Facets item.

 

Click the link to Convert the project to faceted form as shown above.

 

Check the Dynamic Web Module facet, as shown above.

You'll then see that a yellow info box shows, which says "Further configuration available...". Click that.

 

Make sure the Content directory is set to src/main/webapp as shown above and click OK. Click any remaining Apply/OK buttons to apply and close out the project Properties dialog.

Now, you should be able to right-click on your Tomcat server in the Servers view of the IDE, choose Add and Remove... and add the project.

You can also verify this by right-clicking on the server and select Start to start it up.

Once started, access http://localhost:8080/yourAppContext in the browser and you should see the Hello World! JSP page that was created in the Maven Web App archetype.

 

Enterprise Solution Architecture
Cody Burleson is an Enterprise Web Architect, entrepreneur, and writer. He designs and builds solutions with his team at Base22 using IBM WebSphere Portal, IBM Web Content Manager, IBM Connections, and Java/J2EE. He is a tireless student of information technology who loves to invent things, improve things, and share what he learns. You can find more at his blog, codyburleson.com.

 

10 Comments

  1. 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. Anonymous

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

    Where do I get one and what should it contain?

    1. 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. 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. 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. 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. Anonymous

    thank you very much

  4. 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. 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. 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>