CI Tools and Best Practices in the Cloud

Continuous Integration

Subscribe to Continuous Integration: eMailAlertsEmail Alerts newslettersWeekly Newsletters
Get Continuous Integration: homepageHomepage mobileMobile rssRSS facebookFacebook twitterTwitter linkedinLinkedIn

Continuous Integration Authors: Karthick Viswanathan, XebiaLabs Blog, Liz McMillan, Mehdi Daoudi, Automic Blog

Related Topics: Java EE Journal, Apache Web Server Journal

J2EE Journal: Article

An Introduction to Maven - Part II

How to use Maven in product development

We'll use the built-in maven-archetype-quickstart arechetype to create a directory structure representing a typical Java project. Open a command prompt and change the directory to the working directory where the project directory should be created. At the command prompt, execute this Maven command:

mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=
maven-archetype-quickstart -DgroupId=com.somecompany -DartifactId=EmployeeInfo -Dversion=1.0

On the command we provided the Maven coordinates for our project; com.somecompany as the group ID, EmployeeInfo as artifact ID, and 1.0 as the artifact version. Executing the command above creates this directory structure as shown in Figure 10.

A default POM file is created for our project as shown below. The directory structures src/main/java/ and /src/test/java are created with directories for the default package. Note that Maven Archetype uses a group ID for default package names unless a -DpackageName property is explicitly specified on the command. Note that by default Archetype includes Junit as a dependency and creates in the main source directory and a Junit test case in the test source directory:

<project xmlns="" xmlns:xsi=""

Importing a Maven Project into Eclipse
The following steps show how to import a Maven project into Eclipse:

1.  The first thing would be to create Eclipse-specific project files based on Maven's POM information so that Maven project can be imported into Eclipse. This is fairly easy to do using Maven's Eclipse plug-in. At the command prompt change the directory to the EmployeeInfo directory and run the Maven command to execute the Eclipse plug-in with eclipse goal as shown in Figure 11. Executing the Eclipse plug-in with an eclipse goal will result in creating the .class-path and .project files that Eclipse uses.
2.  Import the newly created project into Eclipse. Click on File > Import to open the Eclipse import wizard and select Existing Projects into Workspace as shown in Figure 12.
3.  In the next window browse through the directory to select the EmployeeInfo project directory to be imported. Optionally, you can select the Copy projects into workspace checkbox if you have to copy the project directory (see Figure 13).
4.  Eclipse will import and open the project in the explorer package as shown in Figure 14. Note that Eclipse displays a project failure flag indicating that the project isn't Maven-enabled yet
5.  The next step would be to enable Maven for the project by right clicking on the project and selecting Maven > enable option as shown in the Figure 15.
6.  Once the project is Maven-enabled, Maven tasks can be done from Eclipse. The first thing would be to update the Maven source directories using the Maven option as shown in Figure 16. Enabling Maven and updating the Maven sources will set the Maven class-path to be used according to Eclipse and will update all the source directories including the test source directories.
7.  Now that we have a parent directory structure laid out for the EmployeeInfo project, we have to make a slight change to the POM so it can start accepting other modules as children. By default the POM file has a jar packaging type. The packaging type for a parent POM that can contain other Maven modules should be set to pom. By changing the packaging type to pom, there will be no specific artifact coming out of the parent POM and so the src directory, as it is, can be removed. However, specifying the group ID and version in the parent POM will let all sub-modules inherit such attributes and make the parent POM a central place to specify such attributes. Once the packaging type is changed to pom and the source directories deleted, Maven source directories should be updated using the Maven option shown in Figure 16.
8.  The project in Eclipse should look like Figure 17. Note that by default Maven uses a target/classes directory to store main Java classes and a target/test-classes directory to store test Java classes.

Creating Project Modules
Now that we have the parent project it's time to create the individual module projects inheriting the POM from the parent project.

Setting Up a Castor-based xmlBinding Module
Since an xmlBinding module generates a JAR artifact, we need a simple JAR project directory structure. To create this, go to the EmployeeInfo directory on the command prompt and execute the following Maven command:

mvn archetype:create -DgroupId=com.somecompany -DartifactId=xmlBinding -Dversion=1.0

This creates a simple project structure with src/main/java and src/test/java directories containing a sample Java class and its Junit test case respectively. Now refresh the EmployeeInfo project in Eclipse and update the Maven source directories using the Maven option in the right-click menu. Eclipse will add the xmlBinding directory to the project and show both xmlBinding/src/main/java and xmlBinding/src/test/java as Eclipse Java source directories as shown in Figure 18. Since we intend to generate XML-bound Java classes using Castor, the xmlBinding/src/main/java and xmlBinding/src/test/java default Java source directories can be deleted.

Let 's compare the xmlBinding POM file with the parent EmployeeInfo POM file (see Table 2).

More Stories By Murali Kashaboina

Murali Kashaboina leads Enterprise Architecture at United Airlines, Inc. He has 15+ years of enterprise software development experience utilizing a broad range of technologies, including JEE, CORBA, Tuxedo, and Web services. Murali previously published articles in WLDJ and SilverStream Developer Center. He has master’s degree in mechanical engineering from the University of Dayton, Ohio.

More Stories By Geeth Narayanan

Geeth Narayanan is a senior architect at Ecommerce Technology, United Airlines, Inc. He has 10 years of experience in the IT industry, specializing in solutions using Java EE technologies. Geeth has master's degree in electrical engineering from the University of Toledo, Ohio.

Comments (2) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.

Most Recent Comments
Brad 10/05/07 04:58:49 PM EDT

On page 2 the Figure 11 cuts off the command that is used.

I think the command is supposed to be "mvn eclipse:eclipse" to generate Eclipse descriptor.

Nazir Khan 09/27/07 05:37:33 AM EDT

This is an amazing intro to maven 2, it has been very beautifully crafted, which starts telling about the internals of maven and how it works on different scenarios and then taking to practicalities of it. Hats off to the guys who made this wonderful work.