Nicozan-Blog

Knowledge Seeking

Cargo Maven Plugin – Functional Testing March 1, 2011

Filed under: Java — nicozan @ 3:10 pm
Tags: , ,

Introduction
The main idea of this post is to help you and give you a set of steps to create en embedded web-app container where you can put different compiled modules inside it and run integration tests.

Container JAVA API:

Cargo has a Java API that allow as to start/stop/configure Java Containers and deploy modules into them. The objects related to this API are:

* Container: is the top level interface wrapping a real physical container. Cargo supports Local and Remote containers. (In this explanation we’ll be using Local Containers). A Container is composed of a Configuration.
* Configuration: tells the Cargo how the container is to configured (logging, security, data sources, location where to put deployables, etc). A Configuration can be configured to install Deployables before the containers is started.
* Deployer: it is used to hot-deploy Deployables. Hot-deploy means when you deploy a Deployable into a running container.
* Deployables: are archives (WAR,EAR,etc) that can be deployed into the container.

MAVEN 2 Cargo Plugin:
The plugin wraps the Cargo JAVA API and from various types of use cases of it, we’ll explain how to start and stop a container, including deploying some deployables as it starts.
As we said before we’ll run an embedded container.The most basic container you can use in cargo is an Embedded Jetty 4x/5x/6x Container. The following maven 2 plugin definition will configure an embedded Jetty 6x container. Note the use of the embedded type element to specify an embedded container.

[...]
<build>
  <plugins>
     <plugin>
         <groupId>org.codehaus.cargo</groupId>
         <artifactId>cargo-maven2-plugin</artifactId>
         <version>1.0.6</version>
         <configuration>
             <container>
                   <containerId>jetty6x</containerId>
                   <type>embedded</type>
             </container>
         </configuration>
     </plugin>
  </plugins>
</build>
[...]

Before we continue please remember that the code snippet from above goes in your pom.xml and that if you don’t specify the value the embedded container we’ll be Jetty 5.X.

Now it is time to start deploying our deployables. The deployables are archives (i.e. EAR and WAR) that can be deployed in the container. The code snippet below shows how to deploy deployables in an embedded container, in this case jetty:

<dependencies>
[...]
   <dependency>
       <groupId>com.wordpress.nicozan</groupId>
       <artifactId>my-war</artifactId>
       <version>1.0.0</version>
       <packaging>war</packaging>
    </dependency>
[...]
</dependencies>
[...]
<build>
  <plugins>
     <plugin>
        <groupId>org.codehaus.cargo</groupId>
        <artifactId>cargo-maven2-plugin</artifactId>
        <version>1.0.6</version>
        <configuration>
             <container>
                   <containerId>jetty6x</containerId>
                   <type>embedded</type>
             </container>
            <configuration>
                      <type>embedded</type>
                      [...]
                      <deployables>
                            <deployable>
                                  <groupId>com.wordpress.nicozan</groupId>
                                   <artifactId>my-war</artifactId>
                                   <type>war</type>
                             </deployable>
                      </deployables>
            </configuration>
       </configuration>
     </plugin>
  </plugins>
</build>
[...]

Okay, let’s talk about the tags we have seen above. As we can see, we have specified the that represents our . You might have noticed that the dependency tag and the deployable tag have almost the same properties. The property that they don’t share is the version. This is because cargo gets the version information from the dependency declaration at the moment it has to deploy the deployable.

When do we use Cargo?

Imagine that you have a Web App,in Java obviously using the maven archetype. This Web Application is divided in two modules: Client and Server. The hierarchical structure of the project would be like this:
MyWebApp (pom.xml)
—> Client-Module (pom.xml)
—> Server-Module (pom.xml)
Up to now we see a very common structure of a project, but cargo empowers the use of an extra module to start the Functional Testing. We’ll add the module Deployer to our project structure.
MyWebApp (pom.xml)
—> Client-Module (pom.xml)
—> Server-Module (pom.xml)
—> Deployer-Module (pom.xml)
In the Deployer-Module you have to modify its pom.xml and write a configuration similar to the one written in code snippets above, it should be something like this:

  
<dependencies>
[...]
<dependency>
  <groupId>com.wordpress.nicozan</groupId>
  <artifactId>Client-Module</artifactId>
  <version>1.0</version>
  <packaging>war</packaging>
</dependency>
<dependency>
  <groupId>com.wordpress.nicozan</groupId>
  <artifactId>Server-Module</artifactId>
  <version>1.0</version>
  <packaging>war</packaging>
</dependency>
[...]
</dependencies>
[...]
<build>
  <plugins>
    <plugin> 
         <groupId>org.codehaus.cargo</groupId>
         <artifactId>cargo-maven2-plugin</artifactId>
         <configuration>
             <container>
                   <containerId>jetty6x</containerId>
                   <type>embedded</type>
             </container>
             <configuration>
                  <type>embedded</type>
                  [...]
                  <deployables>
                            <deployable>
                                  <groupId>com.wordpress.nicozan</groupId>
                                   <artifactId>Client-Module</artifactId>
                                   <type>war</type>
                             </deployable>
                    <deployable>
                                  <groupId>com.wordpress.nicozan</groupId>
                                   <artifactId>Server-Module</artifactId>
                                   <type>war</type>
                             </deployable>
                  </deployables>
             </configuration>
         </configuration>
    </plugin>
  </plugins>
</build>
[...]

Hope this helps you to understand the main idea of cargo. If you are interested in improving your ways of building a web app, you can have a look at Leandro’s post that explains how to create web app using SmartGwt.

Advertisements
 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s