Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your profile (name, country/region, and company) is displayed to the public and will accompany any content you post. You may update your IBM account at any time.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

developerWorks Community:

  • Close [x]

Introduction to Apache Maven 2

Sing Li (westmakaha@yahoo.com), Author, Wrox Press
Photo of Sing Li
Sing Li is a consultant and an active author with more than two decades of industry experience. He has contributed to Professional Apache Geronimo, Beginning JavaServer Pages, Professional Apache Tomcat 5, Pro JSP - Third Edition, Early Adopter JXTA, Professional Jini, Beginning J2ME: From Novice to Professional, Third Edition, Professional Apache Geronimo, and numerous other books. Sing also writes for technical magazines and participates in open source communities. He is an evangelist of the open source, VOIP, and P2P movements. You can reach Sing at westmakaha@yahoo.com.

Summary:  Modern software projects are no longer solely monolithic creations of single local project teams. With the increased availability of robust, enterprise-grade open source components, today's software projects require dynamic collaboration among project teams and often depend on a mix of globally created and maintained components. Now in its second generation, the Apache Maven build system -- unlike legacy build tools created before the Internet-enabled era of global software development -- was designed from the ground up to take on these modern challenges. This tutorial gets you started with Maven 2.

Date:  19 Dec 2006
Level:  Intermediate

Activity:  152623 views
Comments:  

Hands-on Maven 2 : Your first Maven 2 project

In the first hands-on example, you'll see how you can build simple projects using Maven 2 with minimal effort. Maven 2's built-in knowledge about Java projects eliminates tedious configuration that may be necessary with other build tools.

A class handling numeric operations

The example uses a class that handles numeric operations. The source code for the main class, called NumOps, is shown in Listing 4:


Listing 4. The NumOps class
                    
package com.ibm.devworks;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class NumOps {
  private List <Operation> ops = new ArrayList 
<Operation>(); 
  public  NumOps() {
    ops.add( new AddOps());
  }
  public Operation getOp(int i)
   { Operation retval; 
   if (i > ops.size())  
      { retval = null; 
   } else { 
     retval = (Operation) ops.get(i);
   } return retval; 
  }
  public int size() { 
    return ops.size(); 
  }
  public static void main( String[] args ) {
    NumOps nop = new NumOps(); 
    for (int i=0;  i < nop.size(); i++) { 
      System.out.println( "2 " + 
        nop.getOp(i).getDesc() + 
        " 1 is " + 
        nop.getOp(i).op(2,1) ); 
    }
  }
}

The NumOps class manages a set of objects capable of performing numeric operations on two integers. The main method creates a NumOps instance and then calls each of the objects managed by NumOps, calling its getDesc() method and op() method respectively. All of the objects managed by NumOps implement the Operation interface, defined in Operation.java and shown in Listing 5:


Listing 5. The Operation interface
                    
package com.ibm.devworks;

public interface Operation {
   int op(int a, int b);
   String getDesc();
}

The only operation defined in this initial example is an AddOps class, shown in Listing 6:


Listing 6. The AddOps class
                    
package com.ibm.devworks;

public class AddOps implements Operation {
   public int op(int a, int b) {
      return a+b;
   }
   public String getDesc() {
      return "plus";
   }
}

When you execute the NumOps class, it prints the following output:

 2 plus 1 is 3


Using Archetype to create the initial project

To create everything you need for a simple Java project that can be built using Maven, you can use the Archetype plug-in, which comes standard with Maven 2. Unlike the build-phase plug-ins, the Archetype plug-in runs outside of a Maven project build life-cycle and is used to create Maven projects. Issue the following command (type all of the command on one line) from the directory that you want to contain the NumOps project:

mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes 
  -DgroupId=com.ibm.devworks -DartifactId=NumOps

The command provides the Archetype plug-in with the coordinates of your module: com.ibm.devworks/NumOps/1.0-SNAPSHOT. You don't need to specify the version in this case because the Archetype plug-in always defaults to 1.0-SNAPSHOT. This command creates a starter pom.xml file for the project, along with the conventional Maven 2 directory structure. You'll find the code in this tutorial's source-code download under the example1 directory (see Download).

The output should be similar to Listing 7:


Listing 7. Using Maven Archetype to create the NumOps project
                    

[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'archetype'.
[INFO] -------------------------------------------------------------------------
---
[INFO] Building Maven Default Project
[INFO]    task-segment: [archetype:create] (aggregator-style)
[INFO] -------------------------------------------------------------------------
---
[INFO] Setting property: classpath.resource.loader.class => 'org.codehaus.plexus

...

[INFO] [archetype:create]
[INFO] Defaulting package to group ID: com.ibm.devworks
[INFO] -------------------------------------------------------------------------
---
[INFO] Using following parameters for creating Archetype: maven-archetype-quicks
tart:RELEASE
[INFO] -------------------------------------------------------------------------
---
[INFO] Parameter: groupId, Value: com.ibm.devworks
[INFO] Parameter: packageName, Value: com.ibm.devworks
[INFO] Parameter: basedir, Value: C:\temp\maven
[INFO] Parameter: package, Value: com.ibm.devworks
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: artifactId, Value: NumOps
[INFO] ********************* End of debug info from resources from generated POM
 ***********************
[INFO] Archetype created in dir: C:\temp\maven\NumOps
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1 second
[INFO] Finished at: Sat Dec 02 22:04:02 EST 2006
[INFO] Final Memory: 4M/8M
[INFO] ------------------------------------------------------------------------



The Archetype plug-in creates a directory tree, a pom.xml file, and a placeholder App.java application. It also creates a directory tree for unit-test source code and a placeholder AppTest.java unit test. This project is ready to go. Figure 6 shows the directory and files created by the Archetype plug-in:


Figure 6. Archetype-generated directory and files
Archetype-generated directory and files

All you need to do is to move the NumOps.java, Operation.java, and AddOps.java files into the location where App.java is and remove App.java. In the next section, you'll make some changes to customize the generated pom.xml.

7 of 15 | Previous | Next

Comments



static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Java technology, Open source
ArticleID=184162
TutorialTitle=Introduction to Apache Maven 2
publish-date=12192006
author1-email=westmakaha@yahoo.com
author1-email-cc=