Thursday, November 21, 2013

How to create an EAR file using Maven?

In this post we will see how to create EAR (Enterprise ARchive) using Apache Maven. And we will also see how this pom.xml flow is created!!

What is an Enterprise Application?
An Enterprise Application is a assembling of various resources (WAR, JAR, and RAR) into one deployable unit. And that’s finally packaged as .ear file.



image

How to apply maven to Enterprise Applications?

It’s much easier to create and understand the creation process of JAR, WAR using maven build tool. But creation of Enterprise application is bit confusing stuff and yet bit interesting too with the use of “Reactor” plug-in!
Just hang on with us for a while to know how to create EAR file using Maven build management tool.


Implementation part?
 
1] Consider the following scenario; in my cricket project I have developed three different types of Applications
  • myearproject-dhoni-web –> we need to bundle this as a separate WAR file using it’s own pom.xml
  • myearproject-dravid-web –> we need to bundle this as a separate WAR file using it’s own pom.xml
  • myearproject-sachin-web –> we need to bundle this as a separate WAR file using it’s own pom.xml
2] Once this WAR files generation completed for all the 3 modules, finally we need put it all together and generate EAR file. Refer this following diagram for reference.

clip_image004



How to achieve project aggregation using maven?
3] Here we have a separate pom.xml file for individual projects which would perform WAR file generation. And finally we need to create special project called “multi-module project” in maven terms. It will not create any JAR or WAR artifacts, it just aggregate all other projects as “Modules”.

clip_image006

Multi-Module Project POM file:

<?xml version="1.0" encoding="UTF-8"?>
<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.mycompany</groupId>
  <artifactId>myearproject</artifactId>
  <packaging>pom</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>myearproject Maven Enterprise Application</name>
  <url>http://maven.apache.org</url>
  <modules>
    <module>myearproject-ear</module>
    <module>myearproject-sachin-web</module>
    <module>myearproject-dravid-web</module>
    <module>myearproject-dhoni-web</module>
  </modules>
</project>

Module I: myearproject-dhoni-web
<?xml version="1.0" encoding="UTF-8"?>
<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>
    <parent>
        <artifactId>myearproject</artifactId>
        <groupId>com.mycompany</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <groupId>com.mycompany</groupId>
    <artifactId>myearproject-dhoni-web</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>myearproject-dhoni-web</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>6.0</version>
            <scope>provided</scope>
        </dependency>
</project>

Module II: myearproject-dravid-web

<?xml version="1.0" encoding="UTF-8"?>
<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>
    <parent>
        <artifactId>myearproject</artifactId>
        <groupId>com.mycompany</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <groupId>com.mycompany</groupId>
    <artifactId>myearproject-dravid-web</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>myearproject-dravid-web</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>6.0</version>
            <scope>provided</scope>
        </dependency>
</project>

Module III: myearproject-sachin-web

<?xml version="1.0" encoding="UTF-8"?>
<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>
    <parent>
        <artifactId>myearproject</artifactId>
        <groupId>com.mycompany</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <groupId>com.mycompany</groupId>
    <artifactId>myearproject-sachin-web</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>myearproject-sachin-web</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>6.0</version>
            <scope>provided</scope>
        </dependency>
</project>

That’s it! Please refer the following links for detailed introspection about the multi-module projects.

References:

http://docs.codehaus.org/display/MAVENUSER/Multi-modules+projects

http://maven.apache.org/guides/mini/guide-multiple-modules.html

No comments:

Post a Comment