The older versions of the Java Enterprise Edition (JEE) specification made it difficult for developers to write, test and build applications. This problem has been resolved with the latest JEE specifications (tutorials version 5 and version 6) and has made life easier for developers. Applications are created using regular Java classes, annotations and the occasional XML file. Integrated Development Environments (IDE) make JEE applications quick to develop and test with push button deployments to local containers. Now that the development phase is easier, the focus needs to shift to continuous integration and producing artifacts for deployment to production and testing servers. Luckily there are plugins available for the Maven build tool that allow the creation of artifacts that can be deployed independent of the IDE.

The example project used will be a Maven multiple module project consisting of a parent project, an Enterprise Java Beans (EJB) project, a library project and an assembly project. Each project provides a basic example of what a typical JEE application will need.

Starting with the parent project, create a directory named jee-maven and create a file named pom.xml. Here are the contents of the pom.xml file:

<?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/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>jee.example</groupId>
	<artifactId>jeemvn-parent</artifactId>
	<packaging>pom</packaging>
	<version>1.0</version>
	<name>JEE-Maven</name>
</project>

Test the build by running the command (assuming Maven has been installed properly): mvn clean install. The build should indicate success if everything is correct.

Building the parent project doesn’t do very much other than ensure that the project can be cleaned and installed. Some projects need to be created so that there is something to build. Create three directories, the first directory named assembly, the second directory named common and the last directory named echoservice. The assembly project will be used to create the deployment artifact, the common project will represent a library that can be shared across other projects and the echoservice project will be a simple EJB project.

In the common project, create the pom.xml file with these contents:

<?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/xsd/maven-4.0.0.xsd">
	<parent>
		<groupId>jee.example</groupId>
		<artifactId>jeemvn-parent</artifactId>
		<version>1.0</version>
	</parent>
	<modelVersion>4.0.0</modelVersion>
	<name>JEE-Maven - Common</name>
	<artifactId>common</artifactId>
</project>

Create a directory named src/main/java/ to hold the example code that will be added a little later. Test the build by running the command: mvn clean install. During the build, Maven will create a new directory named target and in it a jar file named common-1.0.jar.

Create a pom.xml file in the echoservice directory with the following contents:

<?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/xsd/maven-4.0.0.xsd">
        <parent>
                <groupId>jee.example</groupId>
                <artifactId>jeemvn-parent</artifactId>
                <version>1.0</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
        <name>JEE-Maven - Echo Service</name>
        <artifactId>echoservice</artifactId>
	<dependencies>
		<dependency>
			<groupId>javax</groupId>
			<artifactId>javaee-api</artifactId>
			<version>6.0</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>jee.example</groupId>
			<artifactId>common</artifactId>
			<version>1.0</version>
		</dependency>
	</dependencies>
</project>

Test the build by running the command: mvn clean install. The build should succeed and a jar file named echoservice-1.0.jar will be created in the target directory. Notice the dependencies section. The first dependency makes JEE 6 available to the build and the second makes the newly created common project available. Now, create a directory named src/main/java/ to hold the source files.

The final step is to make the parent project aware of the children. This is done by simply adding the modules tag in the parent pom.xml file. This is how the file should look once the changes are made:

<?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/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>jee.example</groupId>
	<artifactId>jeemvn-parent</artifactId>
	<packaging>pom</packaging>
	<version>1.0</version>
	<name>JEE-Maven</name>
        <modules>
                <module>common</module>
                <module>echoservice</module>
        </modules>
</project>

Now, when the command mvn clean install is executed, the output will indicate that all three projects have been built successfully. Here is a snippet of the output:

[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] 
[INFO] JEE-Maven ......................................... SUCCESS [1.637s]
[INFO] JEE-Maven - Common ................................ SUCCESS [3.663s]
[INFO] JEE-Maven - Echo Service .......................... SUCCESS [0.614s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS

Now that the Maven configuration files are completed, the project can be imported into an IDE. Netbeans and Intellij can open the parent pom.xml file like a project file and allow coding immediately. Once the project has been imported into an IDE, create a new package in the common project named com.example.domain. Create a class named Echo that looks like this:

package com.example.domain;

import java.io.Serializable;

public class Echo implements Serializable
{
	private String value;

	public String getValue()
	{
		return value;
	}

	public void setValue(String value)
	{
		this.value = value;
	}
}

Execute the build from the jee-maven directory by executing the command mvn clean install and ensure that the build is still successful.

Next create an EJB in the echoservice project. Start by creating a new package named com.example.service. Then create a new java class named EchoServiceBean and add the required EJB annotation. The code for the bean should look like this:

package com.example.service;

import com.example.domain.Echo;

import javax.ejb.Stateless;

@Stateless
public class EchoServiceBean
{
	public String echo(Echo echo)
	{
		return "Hello " + echo.getValue();
	}
}

Execute the build from the jee-maven directory using mvn clean install and ensure that the build is successful.

That concludes the build phase of the project, but it is still not deployable. The assembly project will now need to be created to package the jar files into a deployable Enterprise Archive (ear). Create a new file named pom.xml under the assembly directory with the following contents:

<?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/xsd/maven-4.0.0.xsd">
	<parent>
		<groupId>jee.example</groupId>
		<artifactId>jeemvn-parent</artifactId>
		<version>1.0</version>
	</parent>

	<modelVersion>4.0.0</modelVersion>
	<artifactId>echo-service</artifactId>
	<packaging>ear</packaging>
	<name>Echo Service Assembly</name>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-ear-plugin</artifactId>
				<version>2.5</version>
				<!-- configuring the ear plugin -->
				<configuration>
					<version>5</version>
					<modules>
						<ejbModule>
							<groupId>jee.example</groupId>
							<artifactId>echoservice</artifactId>
						</ejbModule>
						<jarModule>
							<groupId>jee.example</groupId>
							<artifactId>common</artifactId>
							<bundleDir>lib</bundleDir>
						</jarModule>
					</modules>
				</configuration>
			</plugin>
		</plugins>
	</build>

	<dependencies>
		<dependency>
			<groupId>jee.example</groupId>
			<artifactId>common</artifactId>
			<version>1.0</version>
			<type>jar</type>
		</dependency>
		<dependency>
			<groupId>jee.example</groupId>
			<artifactId>echoservice</artifactId>
			<version>1.0</version>
			<type>ejb</type>
		</dependency>
	</dependencies>
</project>

This file is slightly different from the other pom.xml files. The dependency tags now have a new type property that specifies whether this is a jar or ejb. Notice the use of the maven-ear-plugin. This plugin allows for the creation of the deployable artifact. Observe that the jarModule uses a bundleDir tag which puts the common library in a lib folder. More information about all the available options can be found here: maven-ear-plugin home.

About these ads