Feed on Posts or Comments 11 March 2010

Java Wytze on 21 Aug 2008 07:52 am

Building EJB3 applications with Maven 2

Here is a guide to building ejb3 applications with maven2 (from scratch). We will not be using any maven archetypes/templates but do it by hand to get a project that is as clean as possible.

First create a directory that will contain all the modules the ear file consists of. It will contain all the basic info the other projects/modules need to inherit from.

Create the pom.xml file in the directory and update it with something like the following:

<?xml version="1.0" encoding="UTF-8"?>
<project
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<modelVersion>4.0.0</modelVersion>
	<groupId>your.group.id</groupId>
	<artifactId>your-artifact-name</artifactId>
	<packaging>pom</packaging>
	<name />
	<version>0.0.1-SNAPSHOT</version>
	<description />
	<modules>
		<module>ear</module>
		<module>war</module>
		<module>ejb-jar</module>
	</modules>
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.5</source>
					<target>1.5</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

Then create the subdirectories ( I named them ear, war and ejb-jar in this case ) for the modules.

ejb-jar pom.xml:

<?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">
	<parent>
		<artifactId>ejb-sample</artifactId>
		<groupId>your.group.id</groupId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<modelVersion>4.0.0</modelVersion>
	<groupId>your.group.id</groupId>
	<artifactId>ejb-jar</artifactId>
	<name />
	<version>0.0.1-SNAPSHOT</version>
	<packaging>ejb</packaging>
	<description />
	<dependencies>
		<dependency>
			<groupId>javax.ejb</groupId>
			<artifactId>ejb</artifactId>
			<version>3.0</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.persistence</groupId>
			<artifactId>persistence-api</artifactId>
			<version>1.0</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.testng</groupId>
			<artifactId>testng</artifactId>
			<version>5.7</version>
			<scope>test</scope>
		</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>

war pom.xml:

<?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">
	<parent>
		<artifactId>ejb-sample</artifactId>
		<groupId>your.group.id</groupId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<modelVersion>4.0.0</modelVersion>
	<groupId>your.group.id</groupId>
	<artifactId>war</artifactId>
	<packaging>war</packaging>
	<name />
	<version>0.0.1-SNAPSHOT</version>
	<description />
	<dependencies>
		<dependency>
			<groupId>your.group.id</groupId>
			<artifactId>ejb-jar</artifactId>
			<type>ejb</type>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
	</dependencies>
	<build>
		<finalName>yourWarName</finalName>
	</build>
</project>

ear pom.xml:

<?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">
	<parent>
		<artifactId>ejb-sample</artifactId>
		<groupId>your.group.id</groupId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<modelVersion>4.0.0</modelVersion>
	<groupId>your.group.id</groupId>
	<artifactId>ear</artifactId>
	<packaging>ear</packaging>
	<name />
	<version>0.0.1-SNAPSHOT</version>
	<description />
	<dependencies>
		<dependency>
			<groupId>your.group.id</groupId>
			<artifactId>ejb-jar</artifactId>
			<type>ejb</type>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
		<dependency>
			<groupId>your.group.id</groupId>
			<artifactId>war</artifactId>
			<type>war</type>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
	</dependencies>
	<pluginRepositories>
		<pluginRepository>
			<id>codehaus snapshot repository</id>
			<url>http://snapshots.repository.codehaus.org/</url>
			<releases>
				<enabled>true</enabled>
			</releases>
		</pluginRepository>
	</pluginRepositories>
	<build>
		<finalName>your-ear-name</finalName>
		<plugins>
			<plugin>
				<artifactId>maven-ear-plugin</artifactId>
				<configuration>
					<modules>
						<ejbModule>
							<groupId>your.group.id</groupId>
							<artifactId>ejb-jar</artifactId>
						</ejbModule>
						<webModule>
							<groupId>your.group.id</groupId>
							<artifactId>war</artifactId>
						</webModule>
					</modules>
					<jboss>
             			<version>4</version>
             			<loader-repository>your.group:archive=your-ear-name.ear</loader-repository>
           			</jboss>					
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.codehaus.cargo</groupId>
				<artifactId>cargo-maven2-plugin</artifactId>
				<version>0.3-SNAPSHOT</version>
				<configuration>
					<container>
						<containerId>jboss4x</containerId>
						<type>remote</type>
					</container>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

This is basically the project structure. I hope to create a downloadable archetype of this structure so you can start with this by running the mvn archetype plugin for easy use.

15 Responses to “Building EJB3 applications with Maven 2”

  1. on 28 Sep 2008 at 2:15 am 1.Rick said …

    This looks wonderful! Thanks. I’m still stumped why an archetype doesn’t exist for this jee setup.

    I plan to make a helloworld app with JBoss using this setup. I’ll make sure to give you props when it’s done (or if you already have one, please contact me.)

    Thanks again for this post.

  2. on 14 Oct 2008 at 7:11 am 2.Rick said …

    I finally got around to creating a prototype EJB3/JPA simple skeleton application using your maven guide as a start. Thanks.

    Posted here:

    http://learntechnology.net/content/ejb/maven-ejb3.jsp

  3. on 20 Oct 2008 at 8:02 am 3.Wytze said …

    I’m glad this snippet has helped you. I’m still planning to make an archetype out of this. But somehow I’m always short on time. ;)

  4. on 03 Nov 2008 at 3:17 pm 4.Cristi said …

    Unfortunately there is no such javax.ejb version 3 dependency for maven in maven repository. I have tried creating an ejb3 application with maven that would have such a dependency and maven threw an error that it could not find the above dependency. But anyway, this is a great starting point for creating an ejb3 application with maven.

  5. on 03 Nov 2008 at 3:23 pm 5.Wytze said …

    You are correct about the ejb version 3 dependency. You need to download it manually from sun’s website and import it into your local maven repository.

    You can find it here:
    EJB Specs

    Or have a look at this link:
    Coping with Sun jars

  6. on 05 Nov 2008 at 11:07 am 6.Yagiz Erkan said …

    Hi,

    I think you can use the JBoss repository (http://repository.jboss.com/maven2) for the EJB dependency:

    javax.ejb
    ejb-api
    3.0

    For some more discussions about maven you can check out our blog as well (http://blog.decaresystems.ie). Out process architect blogs about Maven and our use of it.

  7. on 09 Nov 2008 at 8:38 pm 7.Kazys said …

    Configuration for maven-ejb-plugin should be changed like this:

    org.apache.maven.plugins
    maven-ejb-plugin

    3.0

    true

    Otherwise, JBoss throws java.lang.NoClassDefFoundError, when using 3rd party libraries.

  8. on 13 Feb 2009 at 2:39 pm 8.michal said …

    Check this out: http://code.google.com/p/javaee5-maven-archetype/

    archetype generating similar structure (in the war module jsf 1.2 is included).

  9. on 20 Feb 2009 at 10:55 pm 9.PS said …

    Is there a way I can generate stubs for the ejb (EJB 3) to be used by a client.
    Is there a maven 2 plugin for it? Has someone ever dealt with this? I do not see any way to do it.

  10. on 14 Apr 2009 at 1:36 pm 10.Asif said …

    ok guys…thanks for such a nice article clears up a lot of things…i have another question though…

    I followed the article and have 3 dirs ejb/war/ear… ear is getting generated nice and well (i have removed the jboss specific stuff i just want a clean ear).

    and after that I do a
    mvn eclipse:clean eclipse:m2eclipse
    on the root pom.xml and i get a nice eclipse ready project.

    but when i try to add the ear to the server (jboss server from within eclipse)I get a message saying that none of the projects can be added to the server.

    As far as i can figure … it seems you need to generate some sort of application descriptor from within eclipse to help eclipse recognize that out of those projects one is an ear project and should be added.

    any thoughts around this guys…

    Asif..

  11. on 24 Apr 2009 at 8:43 am 11.Wytze said …

    Sorry for my late response:

    The piece of xml in the maven-ear-plugin should take care of generating the required descriptor (in this case for jboss). For deploying you might also want to take a look at the maven cargo plugin.

  12. on 14 Oct 2009 at 5:21 pm 12.KulaDiamond » Blog Archive » something about apache-maven said …

    [...] you can learn more from : http://famvdploeg.com/blog/2008/08/building-ejb3-applications-with-maven-2/ [...]

  13. on 10 Feb 2010 at 11:32 am 13.Maverick said …

    Hi, thanks for the tutorial. One world, should the artifactId of the main pom not be ‘ejb-sample’ ?

    Best

  14. on 10 Feb 2010 at 12:17 pm 14.Wytze said …

    That is indeed correct.
    I would also like to note that with the coming of EJB 3.1 (Lite) creating this whole structure is no longer necessary.
    Next to that I would like to point out that Netbeans (6.8) can create these XML files for you when you create a new maven web project. Have a look at it if you like to see what it looks like.

  15. on 23 Feb 2010 at 2:00 am 15.Atanas Roussev – J2EE, Java Developer, UI Engineer, Resume – Vancouver » Blog Archive » Jump start J2EE EJB3 Maven project said …

    [...] for a Maven ready EJB3 ready to run project? I came across this post. I gathered all the good ideas and grouped them together in ejb3-maven Google Code [...]

Trackback This Post | Subscribe to the comments through RSS Feed

Leave a Reply