Wednesday, April 22, 2015

Install Maven generated WAR file to Tomcat 7/8

How to Install WAR file to Tomcat

  1. Update ~/.m2/settings.xml
<settings>
 <servers>
  <server>
   <id>dev-tomcat</id>
   <username>tomcat</username>
   <password>password</password>
  </server>
 </servers>
</settings>
  1. Add the following to pom.xml
<build>
   .....
    <finalName>my-webapp</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.1</version>
            <configuration>
                <url>http://localhost:8080/manager/html</url>
                <server>dev-tomcat</server>
                <path>/${project.build.finalName}</path>
            </configuration>
        </plugin>
    </plugins>
</build>

Here dev-server refers to the <server><id>dev-tomcat</id> defined in ~/.m2/settings.xml

Alternatively you can add the <username> and <password> under <configuration> in the pom.xml

  1. A tomcat user must be defined in <CATALINA_HOME>/conf/tomcat-users.xml with the role manager-script
 <role rolename="manager-script"/>
 <user username="tomcat" password="tomcat" roles="manager-script"/>
````

4) To deploy to tomcat run: ```mvn tomcat7:deploy```

5) If your maven project has multiple modules such as below:

````
my-maven-project
    |--my-app
        -- pom.xml
    |--my-webapp
        -- pom.xml
    -- pom.xml
````
The outermost pom.xml file should have at least the plugin reference:
````xml
<plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.0</version>
</plugin>
````
While the ```my-webapp/pom.xml``` producing the WAR file should have ``<configuration>`` element containing url, server and path child elements:
````xml
<configuration>
    <update>true</update>
	<url>http://localhost:8080/manager/text</url>
	<server>dev-tomcat</server>             
    <path>/${project.build.finalName}</path>
</configuration>
````
view raw readme.md hosted with ❤ by GitHub

No comments:

Post a Comment