Saturday, February 7, 2015

JAVA EE 6 WAR with MAVEN

Starting with Java EE 6 developers could use WARs instead of EARs. Developers can use WAR-module, which has a compile-scoped dependency to several <packaging>jar</packaging> typed modules. This article presented a simple way to packing multiply WAR modules into only one Web application.


1. Create pom-root(root project) and multiple WAR modules(web-1, web-2, and web-parent projects).


pom.xml of root project is applied to share dependencies to other web projects.

<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/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.blogshot.wittakarn</groupId>
 <artifactId>root</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>pom</packaging>
 <name>root</name>

 <properties>
  <endorsed.dir>
                    ${project.build.directory}/endorsed
                </endorsed.dir>
  <project.build.sourceEncoding>
                    UTF-8
                </project.build.sourceEncoding>

  <!-- Maven Build Plugin Properties -->
  <my.maven.compiler>3.0</my.maven.compiler>
  <my.maven.war>2.4</my.maven.war>

  <!-- Java Properties -->
  <my.java>1.7</my.java>

  <!-- Dependency Properties -->
  <my.org.primefaces>5.1</my.org.primefaces>
  <my.commons.fileupload>1.2.2</my.commons.fileupload>
  <my.commons.io>2.4</my.commons.io>
  <my.maven.resources>2.6</my.maven.resources>

  <!-- Scope provided -->
  <my.javaee.web.api>6.0</my.javaee.web.api>
  <my.mojarra>2.2.6</my.mojarra>

 </properties>

 <build>
 <plugins>
 <plugin>
  <groupId>
   org.apache.maven.plugins
  </groupId>
  <artifactId>
   maven-compiler-plugin
  </artifactId>
  <version>
   ${my.maven.compiler}
  </version>
  <configuration>
   <source>
    ${my.java}
   </source>
   <target>
    ${my.java}
   </target>
   <encoding>
    ${project.build.sourceEncoding}
   </encoding>
   <showDeprecation>
    true
   </showDeprecation>
  </configuration>
 </plugin>

 <plugin>
  <groupId>
   org.apache.maven.plugins
  </groupId>
  <artifactId>
   maven-war-plugin
  </artifactId>
  <version>${my.maven.war}</version>
  <configuration>
   <failOnMissingWebXml>
    false
   </failOnMissingWebXml>
   <attachClasses>true</attachClasses>
   <archiveClasses>true</archiveClasses>
   <archive>
    <manifest>
     <addDefaultImplementationEntries>
      true
     </addDefaultImplementationEntries>
     <addDefaultSpecificationEntries>
      true
     </addDefaultSpecificationEntries>
    </manifest>
   </archive>
  </configuration>
 </plugin>

 <plugin>
  <inherited>true</inherited>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-resources-plugin</artifactId>
  <version>${my.maven.resources}</version>
  <executions>
   <execution>
    <id>copy-resources</id>
    <phase>prepare-package</phase>
    <goals>
     <goal>copy-resources</goal>
    </goals>
    <configuration>
     <outputDirectory>
      ${basedir}/target/classes
     </outputDirectory>
     <resources>
     <resource>
     <directory>
      ${basedir}/src/main/webapp
     </directory>
     <includes>
      <include>**/*</include>
     </includes>
     <excludes>
      <exclude>WEB-INF/**</exclude>
     </excludes>
     <targetPath>
      /META-INF/resources
     </targetPath>
     </resource>

     <resource>
     <directory>
      src/main/resources
     </directory>
     <filtering>true</filtering>
     </resource>

     <resource>
     <directory>
     ${basedir}/src/main/webapp/WEB-INF
     </directory>
     <includes>
      <include>
       faces-config.xml
      </include>
     </includes>
     <targetPath>/META-INF</targetPath>
     </resource>
     </resources>
    </configuration>
   </execution>
  </executions>
 </plugin>
 </plugins>
 </build>

 <dependencies>
  <dependency>
   <groupId>javax</groupId>
   <artifactId>javaee-web-api</artifactId>
   <version>${my.javaee.web.api}</version>
   <scope>provided</scope>
  </dependency>

  <dependency>
   <groupId>commons-fileupload</groupId>
   <artifactId>commons-fileupload</artifactId>
   <version>${my.commons.fileupload}</version>
   <scope>compile</scope>
  </dependency>

  <dependency>
   <groupId>commons-io</groupId>
   <artifactId>commons-io</artifactId>
   <version>${my.commons.io}</version>
   <scope>compile</scope>
  </dependency>

  <dependency>
   <groupId>org.glassfish</groupId>
   <artifactId>javax.faces</artifactId>
   <version>${my.mojarra}</version>
   <scope>provided</scope>
  </dependency>

  <dependency>
   <groupId>org.primefaces</groupId>
   <artifactId>primefaces</artifactId>
   <version>${my.org.primefaces}</version>
   <scope>compile</scope>
  </dependency>

 </dependencies>
</project>

All of web projects(web-1, web-2, and web-parent projects) will reference to root project in order to get dependencies.

pom.xml of web-1 project.

<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/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

  

    <parent>

  <artifactId>root</artifactId>

  <groupId>com.blogshot.wittakarn</groupId>

  <version>0.0.1-SNAPSHOT</version>

 </parent>



    <groupId>com.blogshot.wittakarn.web</groupId>

    <artifactId>web-1</artifactId>

    <version>0.0.1-SNAPSHOT</version>

    <packaging>war</packaging>



    <name>web-1</name>

</project>



pom.xml of web-2 project.

<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/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

  

    <parent>

  <artifactId>root</artifactId>

  <groupId>com.blogshot.wittakarn</groupId>

  <version>0.0.1-SNAPSHOT</version>

 </parent>



    <groupId>com.blogshot.wittakarn.web</groupId>

    <artifactId>web-2</artifactId>

    <version>0.0.1-SNAPSHOT</version>

    <packaging>war</packaging>


    <name>web-2</name>

</project>


2. Including web-1 and web-2 projects inside web-parent project
Developers can include multiple WAR modules by packing as a jar inside another WAR module. In the case of web-1 and web-2 are sub modules of web-parent, develops just include dependency  of web-1 and web-2 as following.

<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/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

   

    <parent>

  <artifactId>root</artifactId>

  <groupId>com.blogshot.wittakarn</groupId>

  <version>0.0.1-SNAPSHOT</version>

 </parent>



    <groupId>com.blogshot.wittakarn.web</groupId>

    <artifactId>web-parent</artifactId>

    <version>0.0.1-SNAPSHOT</version>

    <packaging>war</packaging>



    <name>web-parent</name>

   

    <dependencies>

  <dependency>

   <groupId>com.blogshot.wittakarn.web</groupId>

   <artifactId>web-1</artifactId>

   <version>0.0.1-SNAPSHOT</version>

   <classifier>classes</classifier>

  </dependency>

  <dependency>

   <groupId>com.blogshot.wittakarn.web</groupId>

   <artifactId>web-2</artifactId>

   <version>0.0.1-SNAPSHOT</version>

   <classifier>classes</classifier>

  </dependency>

 </dependencies>



</project>


The dependency hierarchy is shown below.

Dependency Hierachy of web-parent

After developers build root, web-1, web-2, and web-parent respectively. The result of war will show as below figure.

Then the wab-parent-0.0.1-SNAPSHOT.war is ready to deploy on JAVA EE web application server such as Weblogic 12c, Glassfish 3.1.2.2 and so on.

No comments:

Post a Comment