Thursday, April 8, 2010

Extending an existing Web application WAR

Problem:

Extend an existing web application, reusing as much as possible including not just the source code, but the web resources such as JSPs, images, CSS styling, Javascript etc...

The simple way would be to branch or copy the existing application and import it into a new project, but this has a major drawback. What if there are bug fixes or changes made to the base web application? If we were to branch, then that would mean having to merge all the changes from the base web application with the new web application! I say GAH!

Solution:

If you're already using Maven as your build, then it's Maven WAR overlays to the rescue!

An article about Maven WAR overlays can be found here:
Reference: http://maven.apache.org/plugins/maven-war-plugin/examples/war-overlay.html

To automatically include all dependencies from the base WAR, I use the warpath plugin found here:
http://static.appfuse.org/maven-warpath-plugin/usage.html

You are effectively creating a WAR project that has a dependency on the base WAR project found in your maven repo. Thus, any changes made to the base WAR application can easily be picked up by simply synching up with your SVN code base and rebuilding your new WAR.

In your new web app project, when you make a build, maven will automatically merge the 2 WAR files (base WAR and new app WAR) together to create a new WAR file.

Below is a sample of the pom.xml file to enable this merging:

<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>au.org.australianphenomics</groupId>
  <artifactId>podd-apn-webapp</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>podd-apn-webapp</name>
  <url>http://maven.apache.org</url>
  
  <developers>
      <developer>
          <name>Philip Wu</name>
          <email>philip.wu@anu.edu.au</email>
      </developer>
  </developers>  
  

  
  
  <!-- ================ 
      Build 
   ==================== -->  
  <build>
      <finalName>podd-apn</finalName>
    <plugins>
         
     
        <plugin>
          <groupId>org.appfuse</groupId>
          <artifactId>maven-warpath-plugin</artifactId>
          <version>2.0.2</version>
          <extensions>true</extensions>
          <executions>
            <execution>
              <goals>
                <goal>add-classes</goal>
              </goals>
            </execution>
          </executions>
        </plugin>     
     
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.0</version>
            <configuration>           
                <dependentWarIncludes>**</dependentWarIncludes>   
            </configuration>
        </plugin>
        
    </plugins>  
         
    
  </build>
  
  <!-- ================ 
      Dependencies  
   ==================== -->  

      <dependencies>
       
        <dependency>
            <groupId>au.edu.uq.itee.maenad</groupId>
            <artifactId>podd-webapp</artifactId>
            <version>1.0</version>     
            <type>war</type>
        </dependency>
     
        <dependency>
            <groupId>au.edu.uq.itee.maenad</groupId>
            <artifactId>podd-webapp</artifactId>
            <version>1.0</version> 
            <type>warpath</type>        
        </dependency>          
  
    </dependencies>

  
  
  
  
</project>

As an 'Overlay' the web.xml files from the two WARs are not merged.
If you're looking to do just that, then you might want to take a look at:
http://cargo.codehaus.org/Merging+WAR+files

No comments:

Post a Comment