Archive

Posts Tagged ‘maven2 maven ejb3 jpa example pom.xml pom’

Building EJB3 applications with Maven 2

August 21st, 2008 13 comments

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.