Monday, November 9, 2015

Maven Project Structure for Java Enterprise Application.

Maven Project Structure for Java Enterprise Application.
Today, I thought of sharing  the practice I follow for Java Enterprise Application maven projects.
Developers get confused what could be the best practice for organising the maven projects to that every project is streamlined. In case of EAR project, it’s a combination of WARs, EJBs, Jars. So Structure would look something like this:



So, For achieving this, Let us put the top EAR in a POM project which will be parent of all the projects, so that when we build the parent POM it should build all the child projects and the EAR should be ready to be deployed.
Let us understand this by creating a sample example which includes all the above mentioned EAR artifacts.

1   1.     First create a Parent POM Project. If you are using eclipse with maven plugins to create maven project:




This will create a maven Project whose packaging type would be pom type.

<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>
    <groupId>com.integration</groupId>
    <artifactId>ApplicationParent</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <artifactId>ApplicationEAR</artifactId>
  <packaging>ear</packaging>
  <name>Application EAR</name>
  <dependencies>
       <dependency>
              <groupId>com.integration</groupId>
              <artifactId>ApplicationWAR</artifactId>
              <version>1.0-SNAPSHOT</version>
              <type>war</type>
       </dependency>
       <dependency>
              <groupId>com.integration</groupId>
              <artifactId>ApplicationJAR</artifactId>
              <version>1.0-SNAPSHOT</version>
       </dependency>
       <dependency>
              <groupId>com.integration</groupId>
              <artifactId>ApplicationEJB</artifactId>
              <version>1.0-SNAPSHOT</version>
              <type>ejb</type>
       </dependency>
  </dependencies>
  <build>
        <plugins>
            <plugin>
                <artifactId>maven-ear-plugin</artifactId>
                <configuration>
                    <modules>
                        <webModule>
                            <groupId>com.integration</groupId>
                            <artifactId>ApplicationWAR</artifactId>
                        </webModule>
                        <ejbModule>
                           <groupId>com.integration</groupId>
                            <artifactId>ApplicationEJB</artifactId>
                        </ejbModule>
                        <jarModule>
                           <groupId>com.integration</groupId>
                            <artifactId>ApplicationJAR</artifactId>
                        </jarModule>
                    </modules>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

2     2.   Now create the War Project.



Its Packaging type would be WAR.           
<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>
    <groupId>com.integration</groupId>
    <artifactId>ApplicationParent</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <artifactId>ApplicationWAR</artifactId>
  <packaging>war</packaging>
  <name>Application WAR</name>
  <dependencies>
       <dependency>
              <groupId>com.integration</groupId>
              <artifactId>ApplicationJAR</artifactId>
              <version>1.0-SNAPSHOT</version>
       </dependency>
       <dependency>
              <groupId>com.integration</groupId>
              <artifactId>ApplicationEJB</artifactId>
              <version>1.0-SNAPSHOT</version>
              <type>ejb</type>
       </dependency>
  </dependencies>
</project>

           3. Create an EJB Project.



Its Packaging type would be EJB.
<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>
              <groupId>com.integration</groupId>
              <artifactId>ApplicationParent</artifactId>
              <version>1.0-SNAPSHOT</version>
       </parent>
       <artifactId>ApplicationEJB</artifactId>
       <packaging>ejb</packaging>
       <name>Application EJB</name>
       <dependencies>
              <dependency>
                     <groupId>javax</groupId>
                     <artifactId>javaee-api</artifactId>
                     <version>7.0</version>
              </dependency>
              <dependency>
                     <groupId>com.integration</groupId>
                     <artifactId>ApplicationJAR</artifactId>
                     <version>1.0-SNAPSHOT</version>
              </dependency>
       </dependencies>
       <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-ejb-plugin</artifactId>
            <configuration>
                <ejbVersion>3.0</ejbVersion>
            </configuration>
        </plugin>
    </plugins>
</build>
</project>

4. Create an JAR Project.



Its Packaging type would be JAR.
<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>
    <groupId>com.integration</groupId>
    <artifactId>ApplicationParent</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <artifactId>ApplicationJAR</artifactId>
  <name>Application JAR</name>
</project>
From Eclipse UI, You can go to overview tab of ApplicationParent pom.xml file and add the modules which needs to be built.



The overall Project Structure would look like this:






Step to run the maven install command. Open command prompt, go to ApplicationParent project locatrion and run ‘mvn clean install



This should give beautiful build status on the command prompt screen.




You can have the sample example in my github repo:


Suggestions and feedbacks most welcome!!!!

CHEERS!!!!