Skip to main content
This revision made November 15, 2012 00:37, by ievans

Converting Ant Applications To Maven


This document describes the process of converting an Ant-based example application to Maven.

These instructions assume you have the following software installed and configured:

  • NetBeans 7.2
  • Subversion 1.7 (or equivalent TortoiseSVN version)

Quick Overview of Conversion Steps


The high-level steps for conversion are:

  1. Remove the example from the trunk using svn rm.
  2. Commit the change using svn commit.
  3. Create a blank Maven project based on the appropriate Maven archetype.
  4. Copy the source and supporting files to the correct Maven directory.
  5. Edit the project's pom.xml to include the correct dependencies.
  6. Commit the change using svn commit.

Creating a Blank Maven Project


Maven projects are typically created based on a Maven archetype, a project template that is customized for the type of application you're creating. For example, a standard Java EE 6 web application would have an archetype that creates Facelets files and templates and source packages for the backing components. The structure of this project differs from the structure of an EJB JAR project, which would use a different archetype.

There are hundreds of archetypes available in the central Maven repository, so choosing the correct archetype on which to base your project is important. We will eventually create a set of Java EE Tutorial-specific archetypes to simplify project creation for Java EE 7 example applications. Until that time, use the archetypes listed in the following table.

Archetype name Group ID Version Description
javax.faces-war-archetype javax.faces 2.1 JSF web applications
ear-javaee6 org.codehaus.mojo.archetypes 1.5 EAR applications.
ejb-javaee6 org.codehaus.mojo.archetypes 1.5 EJB JAR applications.
appclient-javaee6 org.codehaus.mojo.archetypes 1.0 Application clients.
jersey-quickstart-webapp org.glassfish.jersey.archetypes 2.0-m5 (or the latest milestone release) JAX-RS 2.0 web applications

Creating a Blank Maven Project in NetBeans

To create a Maven project in NetBeans based on an archetype, follow these instructions:

  1. Select File then New Project and select the Maven category, then Project from Archetype, and click Next.
  2. Enter the archetype name in the Search box, then select the correct version of the archetype under Known Archetypes, and click Next.
  3. Enter the application name under Project Name, the appropriate group ID for the example application under Group Id, the correct version number under Version, the package name for the application, and click Finish.

Example of Creating a Blank Maven Web Application Project in NetBeans

  1. Select File then New Project and select the Maven category, then Project from Archetype, and click Next.
  2. Enter javax.faces in the Search box, then select javax.faces-war-archetype under Known Archetypes, and click Next.
  3. Enter webtest under Project Name, org.glassfish.javaeetutorial.web under Group Id, 7.0.0-SNAPSHOT under Version, javaeetutorial.web.webtest under package name, and click Finish.

Special Steps for EAR Projects

Maven EAR projects can't have nested modules within them, unlike how we set up EAR projects before. Follow these steps to set up EAR projects:

  1. Remove the example from the trunk using svn rm.
  2. Commit the change using svn commit.
  3. Create a new POM project for the example.
    1. In NetBeans, select New Project, then select Maven, then POM Project, then Next.
    2. Set the project name to the name of the example. For example, for the EJB cart example, the name is cart.
    3. Set the project location to the technology area. For example, for the EJB cart example, the location is javaeetutorial/examples/ejb.
    4. Set the groupId to org.glassfish.javaeetutorial, the version to 7.0-SNAPSHOT, the package to the appropriate package, and click Finish.
  4. Create the projects for the EAR modules, with the location set to the POM project folder. For example, for the EJB cart example, the location is javaeetutorial/examples/ejb/cart.
  5. Create the EAR project, with the location set to the POM project folder. For example, for the EJB cart example, the location is javaeetutorial/examples/ejb/cart.
  6. Set the dependencies of the EAR project to the module projects, and add the appropriate <module> definitions for the modules. See javaeetutorial/examples/ejb/cart/cart-ear/pom.xml for an example.

Copying the Source and Supporting Files to the Maven Project


The layout of a Maven project differs from the layout of our Ant-based projects, which used the Blue Prints conventions. The following table shows the equivalent Maven locations.

File Maven layout Blue Prints layout
Java source packages src/main/java src/java
Web root src/main/webapp web
JAR deployment descriptors src/main/resources/META-INF/ src/conf
JUnit source files src/test test

Copy the source files from the javaee-tutorial-6 branch to the new Maven location in the trunk. If the package name has changed, you can use NetBeans to fix the package names and imports:

  1. Create the correct package directories in the project.
  2. Open the source file in NetBeans.
  3. At the package definition, click the error glyph to the left of the editor and select Change Package Declaration to <package name>.
  4. Fix the imports in the source files with Ctrl-Shift-I.
Difference compared to previous revision
== Quick Overview of Conversion Steps The high-level steps for conversion are: # Remove the example from the trunk using svn rm. # Commit the change using svn commit. # Create a blank Maven project based on the appropriate Maven archetype. # Copy the source and supporting files to the correct Maven directory. # Edit the project's pom.xml to include the correct dependencies. # Commit the change using svn commit. == Creating a Blank Maven Project Maven projects are typically created based on a Maven ''archetype,'' a project template that is customized for the type of application you're creating. For example, a standard Java EE 6 web application would have an archetype that creates Facelets files and templates and source packages for the backing components. The structure of this project differs from the structure of an EJB JAR project, which would use a different archetype. ... # Enter javax.faces in the Search box, then select javax.faces-war-archetype under Known Archetypes, and click Next. # Enter webtest under Project Name, org.glassfish.javaeetutorial.web under Group Id, 7.0.0-SNAPSHOT under Version, javaeetutorial.web.webtest under package name, and click Finish. === Special Steps for EAR Projects Maven EAR projects can't have nested modules within them, unlike how we set up EAR projects before. Follow these steps to set up EAR projects: # Remove the example from the trunk using svn rm. # Commit the change using svn commit. # Create a new POM project for the example. ## In NetBeans, select New Project, then select Maven, then POM Project, then Next. ## Set the project name to the name of the example. For example, for the EJB cart example, the name is cart. ## Set the project location to the technology area. For example, for the EJB cart example, the location is javaeetutorial/examples/ejb. ## Set the groupId to org.glassfish.javaeetutorial, the version to 7.0-SNAPSHOT, the package to the [[MavenArtifacts|appropriate package]], and click Finish. # Create the projects for the EAR modules, with the location set to the POM project folder. For example, for the EJB cart example, the location is javaeetutorial/examples/ejb/cart. # Create the EAR project, with the location set to the POM project folder. For example, for the EJB cart example, the location is javaeetutorial/examples/ejb/cart. # Set the dependencies of the EAR project to the module projects, and add the apropriate iki>nowiki> definitions for the modules. See javaeetutorial/examples/ejb/cart/cart-ear/pom.xml for an example. == Copying the Source and Supporting Files to the Maven Project The layout of a Maven project differs from the layout of our Ant-based projects, which used the Blue Prints conventions. The following table shows the equivalent Maven locations. ... | JUnit source files | src/test | test |} Copy the source files from the javaee-tutorial-6 branch to the new Maven location in the trunk. If the package name has changed, you can use NetBeans to fix the package names and imports: # Create the correct package directories in the project. # Open the source file in NetBeans. # At the package definition, click the error glyph to the left of the editor and select Change Package Declaration to . # Fix the imports in the source files with Ctrl-Shift-I.
 
 
Close
loading
Please Confirm
Close