Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Using

...

Maven 3 and Cargo to merge WAR files

If you have an application that you are building with maven2 with Maven 3 that you wish to merge two WAR files together, then you may wish to use the 'uberwar' mojo present in the cargo maven2 cargo Maven 3 plugin.

An "uberwar" is a war file constructed from 2 or more War files, where the deployment descriptors in files such as web.xml have been combined.

Setting up your project to output an uberwar

Create a maven2 a Maven 3 project whose output artifact is going to be the uberwar. Don't forget to add as dependencies the War files you're going to make the uberwar from.

Your packaging type should be uberwar:

Code Block
xml
xml

<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>com.mycorp</groupId>
  <artifactId>myproj</artifactId>
  <packaging>uberwar</packaging>
  <version>1.0-SNAPSHOT</version>
  <!-- etc, etc -->
  <dependencies>
    <dependency>
      <groupId>com.mycorp</groupId>
      <artifactId>baseWarFile</artifactId>
      <version>1.0</version>
      <type>war</type>
    </dependency>
    <dependency>
      <groupId>com.mycorp</groupId>
      <artifactId>funkyApp</artifactId>
      <version>1.0</version>
      <type>war</type>
    </dependency>
  </dependencies>
</project>

Next, you need to add the cargo maven plugin to your <plugins> section, and set it up with the path to a merging descriptor:

Code Block
xml
xml

<plugin>
  <groupId>org.codehaus.cargo</groupId>
  <artifactId>cargo-maven2maven3-plugin</artifactId>
  <extensions>true</extensions>
  <configuration>
    <descriptor>src/assemble/merge.xml</descriptor>
  </configuration>
</plugin>

...

Info
titleMaven Archiver

Note that plugin configuration also supports the <archive> element. For full list of supported values see maven-archiver reference page.
Example:


Code Block
xml
xml

<plugin>
  <groupId>org.codehaus.cargo</groupId>
  <artifactId>cargo-maven2maven3-plugin</artifactId>
  <extensions>true</extensions>
  <configuration>
    <descriptor>src/assemble/merge.xml</descriptor>
    <archive>
      <addMavenDescriptor>false</addMavenDescriptor>
      <manifest>
        <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
        <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
      </manifest>
    </archive>
  </configuration>
</plugin>


...

Here is a sample of the minimal merge descriptor:

Code Block
xml
xml

<?xml version="1.0"?>
<uberwar>
  <wars>
    <war>com.mycorp:baseWarFile</war>
    <war>com.mycorp:funkyApp</war>
  </wars>
</uberwar>

...

For example:

Code Block
xml
xml

<?xml version="1.0"?>
<uberwar>
  ...
  <merges>
    <merge>
      <document>WEB-INF/classes/properties.xml</document>
      <classname>org.codehaus.cargo.module.webapp.DocumentMerger</classname>
    </merge>
  </merges>
</uberwar>

...

The ability to merge web.xml is probably the most important piece of uberwar functionality. This is achieved by providing a special <merge> element, as shown in the example below:

Code Block
xml
xml

<?xml version="1.0"?>
<uberwar>
  ...
  <merges>
    ...
    <merge>
      <type>web.xml</type>
      <parameters>
        <default>
          <tag name="display-name">
            <strategy name="Overwrite"/>
          </tag>
          <tag name="welcome-file-list">
            <strategy name="NodeMerge">
              <welcome-file-list>
                <welcome-file>$left:welcome-file</welcome-file>
                <welcome-file>$right:welcome-file</welcome-file>
              </welcome-file-list>
            </strategy>
          </tag>
          <tag name="context-param">
            <strategy name="ChooseByName">
              <default>
                <strategy name="Preserve"/>
              </default>
              <choice name="contextConfigLocation">
                <strategy name="NodeMerge">
                  <context-param>
                    <param-name>$left:param-name</param-name>
                    <param-value>$left:param-value,$right:param-value</param-value>
                  </context-param>
                </strategy>
              </choice>
            </strategy>
          </tag>
        </default>
      </parameters>
    </merge>
  </merges>
</uberwar>

...

  • If you build as a part of a multiproject build, maven2  Maven 3 may incorrectly store your output war file in the repository with a .uberwar extension. It does not do this if the project is built on its own.
  • If you're experiencing issues with the plugin because Maven is telling you that some Xerces classes cannot be found, please also add xerces:xercesImpl to the plugin's dependencies. That error should not be happening as of Java 6.