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:  

Customizing the POM

Maven 2 learns about your project via the pom.xml file. The file generated by the Archetype for NumOps is shown in Listing 8:


Listing 8. The Archetype-generated POM - pom.xml
                    
<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">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.ibm.devworks</groupId>
  <artifactId>NumOps</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>Maven Quick Start Archetype</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

Note how the Archetype has defined the module's coordinates, defined the type as a JAR archive, and also specified JUnit as a dependency during the test phase (via the <scope> tag). To customize this pom.xml file for the new project, make the minor modifications highlighted in Listing 9:


Listing 9. Customizing the generated pom.xml for the NumOps project

Listing 9. Customizing the generated pom.xml for the NumOps project
                    
                    <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">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.ibm.devworks</groupId>
  <artifactId>NumOps</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>Intro to Maven 2 Example 1</name>
                      <url>http://www.ibm.com/java</url>
                       <build>
                        <plugins>
                          <plugin>
                            <artifactId>maven-compiler-plugin</artifactId>
                            <configuration>
                              <source>1.5</source>
                              <target>1.5</target>
                            </configuration>
                           </plugin>
                        </plugins>
                      </build>
        <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

The additional <build> tag is necessary to override the source and target the Java code level. By default, JDK 1.4 is assumed, but your code uses generics and requires JDK 5.0 compilation.

Compiling the customized project

You can now compile the NumOps project using the mvn compile command. This command causes the Maven 2 engine to march through the build life cycle to the compile phase, executing mojos along the way. You should see the report of a successful build, creating three class files in the target tree (shown in Listing 10). This can take a little while if it is the first time you run it because some dependencies might need to be downloaded from the central repository over the Internet.


Listing 10. Output from mvn compile on the NumOps project
                    
[INFO] Scanning for projects...
[INFO] -------------------------------------------------------------------------
---
[INFO] Building Intro to Maven 2 Example 1
[INFO]    task-segment: [compile]
[INFO] -------------------------------------------------------------------------
---
[INFO] [resources:resources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:compile]
Compiling 3 source files to C:\temp\maven\NumOps\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1 second
[INFO] Finished at: Sat Dec 02 22:52:16 EST 2006
[INFO] Final Memory: 3M/7M
[INFO] ------------------------------------------------------------------------


Adding a unit test

Development best practices require unit tests on all code modules. Maven 2 created a placeholder AppTest.java unit test for you. Now rename the file to NumOpsTest.java and make the highlighted changes shown in Listing 11 to the generated unit test. You can also copy the unit test source code from the source code download (see Download).


Listing 11. Adding the NumOpsTest unit test to the project
                    
package com.ibm.devworks;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
 * Unit test for simple App.
 */
public class NumOpsTest 
    extends TestCase
{
    /**
     * Create the test case
     *
     * @param testName name of the test case
     */
    public NumOpsTest( String testName )
    {
        super( testName );
    }

    ...

    public void testNumOps()
                        {
                        NumOps nops = new NumOps();
                        assertTrue( nops.size() == 1);
                            assertTrue( 
                             nops.getOp(0).getDesc().equals("plus"));
                            assertTrue( nops.getOp(0).op(2,1) == 3);
                    
                        }
}

You can now run all the mojos up to the test phase using the mvn test command.

Maven 2 compiles the source and the unit test. It then runs the tests, reporting on the number of successes, failures, and errors, as shown in Listing 12:


Listing 12. Executing mvn test to compile the project and run unit tests
                    
[INFO] Scanning for projects...
[INFO] -------------------------------------------------------------------------
---
[INFO] Building Intro to Maven 2 Example 1
[INFO]    task-segment: [test]
[INFO] -------------------------------------------------------------------------
---
[INFO] [resources:resources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:compile]
[INFO] Nothing to compile - all classes are up to date
[INFO] [resources:testResources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:testCompile]
Compiling 1 source file to C:\temp\maven\NumOps\target\test-classes
[INFO] [surefire:test]
[INFO] Surefire report directory: C:\temp\maven\NumOps\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.ibm.devworks.NumOpsTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.031 sec

Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2 seconds
[INFO] Finished at: Sat Dec 02 23:04:27 EST 2006
[INFO] Final Memory: 3M/6M
[INFO] ------------------------------------------------------------------------

8 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=