Maven is the build tool of choice for many people due to the simplicity and flexibility of use. The real strength of Maven is vetted when handling real complex projetcs consisting of tens of modules, each requiring elaborate build requirements.
I had faced an uphill task of Mavenising one of our EAR projects. As is the typical case, our project consisted of a EJB module and a WAR module, package together in a EAR module. It was a bit complex as we had to deploy the same application in JBoss and Glassfish. I will write some of that experience here.
Let us consider an enterprise application having a structure as show below:
I will briefly explain what the modules stand for:
I am using Netbeans (6.8) and Glassfish for convinience. But you can pretty much use anything.
I have kept things pretty simple. This is how the Remote interface looks like (its a Stateless Session Bean):
@Remote
public interface MySessionBeanRemote {
String sayHello();
}
And this is how I access it from the servlet:
...
MySessionBeanRemote remoteBean = InitialContext.doLookup(MySessionBeanRemote.class.getName());
...
We will use the maven-ejb-plugin for this. This is how the pom looks like:
4.0.0
com.swayam.eardemo
swayam-ejb
1.0.0
ejb
swayam-ejb
${basedir}/src
org.apache.maven.plugins
maven-jar-plugin
maven-ejb-plugin
3.0
true
org.apache.maven.plugins
maven-compiler-plugin
1.6
javax
javaee-api
6.0
provided
com.swayam.eardemo
swayam-shared
1.0.0
Note that the packaging is ejb and not jar.
4.0.0
com.swayam.eardemo
swayam-war
1.0.0
war
swayam-war
maven.java.net
Java.net Maven2 Repository
http://download.java.net/maven/2
swayam-war
${basedir}/src/java
org.apache.maven.plugins
maven-compiler-plugin
1.6
maven-war-plugin
2.0
${basedir}/web
CVS/**
WEB-INF/lib/*
com.swayam.eardemo
swayam-shared
1.0.0
javax.servlet
servlet-api
2.4
provided
4.0.0
com.swayam.eardemo
swayam-ear
1.0.0
ear
swayam-ear
maven.java.net
Java.net Maven2 Repository
http://download.java.net/maven/2
swayam-ear
org.apache.maven.plugins
maven-compiler-plugin
1.6
maven-resources-plugin
2.3
UTF-8
maven-ear-plugin
2.4.1
com.swayam.eardemo
swayam-war
swayam-war.war
/swayam-war
com.swayam.eardemo
swayam-ejb
swayam-ejb.jar
com.swayam.eardemo
swayam-war
${swayam-version}
war
com.swayam.eardemo
swayam-ejb
${swayam-version}
ejb
1.0.0
The most important thing here is the type tag inside the dependency tag. Without this, it will not work.
Its often very cumbersome to build these modules one by one manually when one of them changes. This is more so in a development environment. So, I will conclude with one pom for building all the modules at one go. This pom is present in the swayam-ear-builder module.
4.0.0
com.swayam.eardemo
swayam-ear-builder
pom
1.0.0
swayam-ear-builder
../swayam-ear
../swayam-ejb
../swayam-war
../swayam-shared
Note that the path of the modules are relative to the pom.
You can build the EAR project and deploy it on Glassfish or JBoss. Once successfully deployed, you can open http://localhost:8080/swayam-war/EjbInvoker This is how it looks like: