This article shows how to structure a J2EE application as Maven project.
As expected by J2EE standard, an enterprise application should be “packaged” in an EAR file (Enterprise Archive). This file can include other packages like WAR and JAR.
Again as expected by J2EE standard, WAR package should contain presentation stuff like JSPs, Servlets, etc. JAR packages instead can contains other component like EJBs.
Summarizing, the structure of a J2EE application should looks like this:
Now, let’s see how to reproduce that in a Maven project. Our Maven project is made by a three modules as showed below:
Maven provides a lot of plugins, useful for several purposes. The ones used for our scope are the following:
As expected by J2EE standard, an enterprise application should be “packaged” in an EAR file (Enterprise Archive). This file can include other packages like WAR and JAR.
Again as expected by J2EE standard, WAR package should contain presentation stuff like JSPs, Servlets, etc. JAR packages instead can contains other component like EJBs.
Summarizing, the structure of a J2EE application should looks like this:
EAR
\_ WAR - Presentation layer (JSPs, Servlets, ...)
\_ JAR - Business logic layer (EJBs, ...)
\_ JAR - Prsistence layer (Entity beans, ...)
Now, let’s see how to reproduce that in a Maven project. Our Maven project is made by a three modules as showed below:
jeeapp (parent project)
\_ jeeapp-ear (aggregator)
\_ jeeapp-web (WAR)
\_ jeeapp-business (EJB)
\_ jeeapp-persistence (JAR)
Maven provides a lot of plugins, useful for several purposes. The ones used for our scope are the following:
- maven-ear-plugin
- maven-ejb-plugin
Parent pom
<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>org.madbit.jeeapp</groupId> <artifactId>jeeapp-parent</artifactId> <version>1.0.0</version> <packaging>pom</packaging> <modules> <module>jeeapp-ear</module> <module>jeeapp-persistence</module> <module>jeeapp-business</module> <module>jeeapp-web</module> </modules> <dependencyManagement> <dependencies> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>6.0</version> <scope>provided</scope> </dependency> </dependencies> </dependencyManagement> </project>
EAR pom
This module is responsible for build the project and aggregate everything in a single ear package.<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>org.madbit.jeeapp</groupId> <artifactId>jeeapp-ear</artifactId> <version>1.0.0</version> <packaging>ear</packaging> <parent> <artifactId>jeeapp-parent</artifactId> <groupId>org.madbit.jeeapp</groupId> <version>1.0.0</version> </parent> <dependencies> <!-- web and ejb modules --> <dependency> <groupId>org.madbit.jeeapp</groupId> <artifactId>jeeapp-persistence</artifactId> <version>1.0.0</version> <type>jar</type> </dependency> <dependency> <groupId>org.madbit.jeeapp</groupId> <artifactId>jeeapp-business</artifactId> <version>1.0.0</version> <type>ejb</type> </dependency> <dependency> <groupId>org.madbit.jeeapp</groupId> <artifactId>jeeapp-web</artifactId> <version>1.0.0</version> <type>war</type> </dependency> </dependencies> <build> <finalName>jeeapp</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.5.1</version> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-ear-plugin</artifactId> <version>2.8</version> <configuration> <applicationName>jeeapp</applicationName> <displayName>jeeapp</displayName> <modules> <webModule> <artifactId>jeeapp-web</artifactId> <groupId>org.madbit.jeeapp</groupId> <contextRoot>/jeeapp</contextRoot> </webModule> <ejbModule> <groupId>org.madbit.jeeapp</groupId> <artifactId>jeeapp-business</artifactId> </ejbModule> </modules> <generateApplicationXml>true</generateApplicationXml> <!-- The following setting builds the EAR file in a format suitable for Glassfish deployment. With this we don't have to copy the libs into Glassfish's appserver lib directory --> <defaultLibBundleDir>lib</defaultLibBundleDir> </configuration> </plugin> </plugins> </build> </project>
WAR pom
<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>org.madbit.jeeapp</groupId> <artifactId>jeeapp-web</artifactId> <version>1.0.0</version> <packaging>war</packaging> <parent> <artifactId>jeeapp-parent</artifactId> <groupId>org.madbit.jeeapp</groupId> <version>1.0.0</version> </parent> <dependencies> ... <dependency> <groupId>org.madbit.jeeapp</groupId> <artifactId>jeeapp-business</artifactId> <version>1.0.0</version> <scope>provided</scope> </dependency> </dependencies> </project>
EJB pom
<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>org.madbit.jeeapp</groupId> <artifactId>jeeapp-business</artifactId> <packaging>ejb</packaging> <version>1.0.0</version> <parent> <artifactId>jeeapp-parent</artifactId> <groupId>org.madbit.jeeapp</groupId> <version>1.0.0</version> </parent> ... <dependencies> ... <dependency> <groupId>org.madbit.jeeapp</groupId> <artifactId>jeeapp-persistence</artifactId> <version>1.0.0</version> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-ejb-plugin</artifactId> <version>2.3</version> <configuration> <ejbVersion>3.1</ejbVersion> </configuration> </plugin> </plugins> </build> </project>
JAR pom
<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>org.madbit.jeeapp</groupId> <artifactId>jeeapp-persistence</artifactId> <packaging>jar</packaging> <version>1.0.0</version> <parent> <artifactId>jeeapp-parent</artifactId> <groupId>org.madbit.jeeapp</groupId> <version>1.0.0</version> </parent> ... <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.3</version> </plugin> </plugins> </build> </project>