Sunday, April 1, 2012

Maven - cargo plugin

As an example of a Maven plugin, let us use cargo.

Cargo is one of the popular maven plugins. It enables you to deploy your war to the server.

We will update the HelloWorldWARArch project that we created in this post to use cargo.

Update the pom.xml to looks like this:
 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>org.confucius</groupId>
<artifactId>HelloWorldWARArch</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Webapp demonstrating cargo plugin</name>
<url>http://www.confucius.org</url>

<pluginRepositories>
<pluginRepository>
<id>codehaus-releases</id>
<url>http://nexus.codehaus.org/releases/</url>
</pluginRepository>
</pluginRepositories>

<build>
<finalName>HelloWorldWARArch</finalName>
<plugins>
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>1.2.0</version>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>undeploy</goal>
<goal>deploy</goal>
</goals>
</execution>
</executions>
<configuration>
<container>
<containerId>tomcat7x</containerId>
<type>remote</type>
</container>
<deployer>
<type>remote</type>
<deployables>
<deployable>
<groupId>org.confucius</groupId>
<artifactId>HelloWorldWARArch</artifactId>
<type>war</type>
</deployable>
</deployables>
</deployer>
<configuration>
<type>runtime</type>
<properties>
<cargo.tomcat.manager.url>http://localhost:8080/manager</cargo.tomcat.manager.url>
<cargo.remote.username>admin</cargo.remote.username>
<cargo.remote.password>s3cret</cargo.remote.password>
</properties>
</configuration>
</configuration>
</plugin>
</plugins>
</build>
</project>

What are we doing here?

First, we are declaring the plugin repository from which Maven can get the cargo plugin.

Next, we include the cargo plugin in the build, specifying its groupId, artifactId and version.

After that, we tell Maven that this plugin must be executed in the 'install' phase of the lifecycle.

We also tell Maven to execute the undeploy and deploy goals of the cargo plugin.

'Undeploy' goal undeploys the HelloWorldWARArch.war that is currently on the server and 'deploy' goal deploys the latest HelloWorldWARArch.war to the server.

Next, we tell cargo that we are deploying to Tomcat 7.

Then we tell cargo to deploy the HelloWorldWARArch.war.

Finally, we tell cargo the credentials (username/password) to use when deploying.

Before we can execute the plugin, we need to update the admin roles on Tomcat so it will accept a deployment from cargo.

Open the tomcat-users.xml in your <tomcat-install-dir>/conf folder and update the admin user with a manager-script role, like this:
 <user name="admin" password="s3cret" roles="manager-gui,manager-script" />  


Restart Tomcat for the role to take effect.

Now, in your Eclipse HelloWorldWARArch project, update the file /src/main/webapp/index.jsp like this:
 <html>  
<body>
<h2>Hello World v2!</h2>
</body>
</html>


Now, in your Eclipse navigator view, R-click on the HelloWorldWARArch prohect and select Rus As --> Maven Install.

In the console, you will see messages from the cargo plugin.

If you point your browser to:
http://localhost:8080/HelloWorldWARArch/

you will see the message "Hello World v2!"

This tells you that cargo successfully deployed the new war to Tomcat.

Notes:
As you can see cargo is a very convenient plugin. It will automatically take care of deploying the new war to the server at the end of the build process.

But this has come at a configuration cost. You can see that the pom.xml has become a lot more complex. The more plugins you use, the more complex the pom.xml becomes.

No comments: