Friday, 27 July 2012

How to Deploy a .war to Tomcat 7 via Maven?

In order to deploy a .war to Tomcat 7 during a maven build process, the cargo plugin is very useful.

One should add the following plugin in your pom.xml:
  <plugin>
    <groupId>org.codehaus.cargo</groupId>
    <artifactId>cargo-maven2-plugin</artifactId>
    <version>1.1.2</version>
    <configuration>
      <container>
        <containerId>tomcat7x</containerId>
        <type>remote</type>
      </container>
      <configuration>
        <type>runtime</type>
        <properties>
          <cargo.remote.uri>
            http://localhost:8080/manager/text
          </cargo.remote.uri>
          <cargo.remote.username>admin</cargo.remote.username>
          <cargo.remote.password>admin</cargo.remote.password>
        </properties>
      </configuration>
      <deployer>
        <type>remote</type>
        <deployables>
          <deployable>
            <groupId>com.my-group-id</groupId>
            <artifactId>my-artifact</artifactId>
            <type>war</type>
          </deployable>
        </deployables>
      </deployer>
    </configuration>
  </plugin>

The remote URI is configured to deploy the .war on a local Tomcat 7 installation listening to port 8080. The host could be a remote host too (one should use https instead for security).

The profile used to connect to maven must be configured with proper roles in the /conf/tomcat-users.xml file where Tomcat is installed, as following:
  <tomcat-users>
    <user name="admin" password="admin"
       roles="admin-gui,manager-gui,manager-script" />
    ...
  </tomcat-users>

Then you can use the following maven goals to deploy or redeploy your application:
  cargo:deployer-deploy
  cargo:deployer-redeploy
More Maven tips & tricks here.