Home > Java > Building EJB3 applications with Maven 2

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.

  1. September 28th, 2008 at 02:15 | #1

    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. Rick
    October 14th, 2008 at 07:11 | #2

    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. Wytze
    October 20th, 2008 at 08:02 | #3

    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. Cristi
    November 3rd, 2008 at 15:17 | #4

    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. Wytze
    November 3rd, 2008 at 15:23 | #5

    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. November 5th, 2008 at 11:07 | #6

    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. Kazys
    November 9th, 2008 at 20:38 | #7

    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. michal
    February 13th, 2009 at 14:39 | #8

    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. PS
    February 20th, 2009 at 22:55 | #9

    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. Asif
    April 14th, 2009 at 13:36 | #10

    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. Wytze
    April 24th, 2009 at 08:43 | #11

    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. Maverick
    February 10th, 2010 at 11:32 | #12

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

    Best

  13. Wytze
    February 10th, 2010 at 12:17 | #13

    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.

  1. October 14th, 2009 at 17:21 | #1
  2. February 23rd, 2010 at 02:00 | #2

Time limit is exhausted. Please reload CAPTCHA.