Run WAR file in /target directory of Maven project by Tomcat

20,361

Solution 1

There are a couple of ways to do this.

The tomcat maven plugin http://mojo.codehaus.org/tomcat-maven-plugin/ provides you a way to deploy directly to tomcat from maven. You must have configured your tomcat users first. See http://tomcat.apache.org/tomcat-7.0-doc/config/realm.html for instructions on how to configure your users.

For debugging you can start tomcat with the arguments specified at http://wiki.apache.org/tomcat/FAQ/Developing

The other way would be to configure tomcat to point to your war. See http://tomcat.apache.org/tomcat-7.0-doc/config/context.html particularly the docbase parameter.

EDIT 1 Based on the revised question http 403 is a forbidden this means you didn't authenticate correctly.

Change your tomcat-users.xml to look as follows:

You need to add a refrence to the username/password in your pom.xml. You settings.xml must contain (based on your configuration)

<server>
    <id>tomcat</id>
    <username>admin</username>
    <password></password>
</server>

Theres a good page http://www.avajava.com/tutorials/lessons/how-do-i-deploy-a-maven-web-application-to-tomcat.html that explains it more fully. However the information may be out of date.

Solution 2

I ran into exactly the same issue.

I suppose it is eclipse-tomcat plugin that is doing this. And I have no idea why it works differently in tomcat 7 opposed to tomcat 6 which I didn't have problem with.

Anyways, what I did was to have maven build into the /web-app/WEB-INF directory

<build>
    <directory>${basedir}/src/main/webapp/WEB-INF</directory>
...
</build>

After this config, eclipse:eclipse should yield a proper eclipse project/classpath files resources:resources shoudl work accordingly as well.

Share:
20,361
Đinh Hồng Châu
Author by

Đinh Hồng Châu

A young guy who loves to research what he doesn't know.

Updated on October 19, 2020

Comments

  • Đinh Hồng Châu
    Đinh Hồng Châu over 3 years

    I'm developing a web application with Java and Maven build system and the web server is Tomcat 7.0.12. After packaging whole project to a WAR file to \target directory by Maven build command, I have to copy it to the webapps folder of Tomcat home to run it. It's very inconvenient, especially when I modified some source files because I have to do all those things (build, copy to Tomcat, run it) again. I've research some articles about Maven, Tomcat, Eclipse on this problem, but there's no result. Could you please help me: 1. How to make Tomcat run the WAR file on target directory of project which is built by Maven command directly? No need to copy/paste the WAR file and restart Tomcat? 2. How can I configure the Tomcat to debug the web application on Eclipse? Thank you so much!

    BTW, I've read and tried to configure Tomcat, Maven and pom file many times. But I don't know what is the exact configuration, because there are so many advices! Could you provide a particular example of configuration for me? Here is my configuration files:

    Tomcat tomcat-users.xml

      <role rolename="manager-gui"/>
      <user username="admin" password="" roles="manager-gui"/>
      **<role rolename="manager"/>
      <user username="admin" password="" roles="manager"/>**
      <role rolename="admin-gui"/>
      <user username="admin" password="" roles="admin-gui"/>
    

    Maven settings.xml

    tomcat admin

    And the pom.xml file of project:

    <build>
          <finalName>my-project</finalName>
          <defaultGoal>package</defaultGoal>
          <plugins>
             <plugin>
           <groupId>org.codehaus.mojo</groupId>
           <artifactId>tomcat-maven-plugin</artifactId>
           <version>1.1</version>
           <configuration>
              <server>tomcat</server>  
                  <warFile> ${project.build.directory}/${project.build.finalName}.war</warFile>
           </configuration>
         </plugin>
          </plugins>
       // Other plugins
       </build>
    

    More details: If I run mvn tomcat:deploy before starting a Tomcat instance, the returned error is "Cannot invoke Tomcat manager: Connection refused: connect." Otherwise, if a Tomcat instance has been started before calling mvn tomcat:deploy, the error is "Cannot invoke Tomcat manager: Server returned HTTP response code: 403 for URL: http://localhost:8080/manager/deploy?path=%2Fmy-project&war=..."

  • Wes
    Wes about 13 years
    Updated to answer the revised question better.