Time for action – installing Maven

In this section, we will install and use Maven to build a simple Java project to ensure that the tool is configured appropriately. The first time it runs, it will cache many JAR files from the Central repository into a folder ${user.home}/.m2/repository; for subsequent runs it will be much faster.

  1. Go to http://maven.apache.org/ and download the Maven 3.0.5.zip package (for Windows) or Maven 3.0.5.tgz (for Mac OS X/Linux).
  2. Unzip/untar the package and install Maven at a convenient directory, referred to in these instructions as MAVEN_HOME.
  3. Either add MAVEN_HOME/bin to the PATH environment variable or specify the full path to the Maven executable JAR. Run mvn --version and if all works well, a version message should be printed out.
  4. To create a new Maven project, run mvn archetype:generate

    -DarchetypeGroupId=org.apache.maven.archetypes

    -DarchetypeArtifactId=maven-archetype-quickstart

    -DarchetypeVersion=1.1

  5. When prompted for the groupId, enter com.packtpub.e4.
  6. When prompted for the artifactId, enter com.packtpub.e4.tycho.
  7. When prompted for the version and package, hit Enter to take the defaults of 1.0-SNAPSHOT and com.packtpub.e4 respectively.
  8. Finally, hit Enter to create the project:
    Define value for property 'groupId': : com.packtpub.e4       
    Define value for property 'artifactId': : com.packtpub.e4.tycho
    Define value for property 'version':  1.0-SNAPSHOT: : 
    Define value for property 'package':  com.packtpub.e4: : 
    Confirm properties configuration:
    groupId: com.packtpub.e4
    artifactId: com.packtpub.e4.tycho
    version: 1.0-SNAPSHOT
    package: com.packtpub.e4
     Y: : Y 
  9. Change into the com.packtpub.e4.tycho directory created and run mvn package to run the tests and create the package:
    [INFO] Scanning for projects
    [INFO]
    [INFO] Building com.packtpub.e4.tycho 1.0-SNAPSHOT
    [INFO]
    
     T E S T S
    
    Running com.packtpub.e4.AppTest
    Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.009 sec
    
    Results :
    
    Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
    
    [INFO]
    [INFO] Building jar: com.packtpub.e4.tycho-1.0-SNAPSHOT.jar
    [INFO] 
    [INFO] BUILD SUCCESS
    [INFO] 
    [INFO] Total time: 1.977s
    [INFO] Final Memory: 15M/136M

What just happened?

The Maven launcher knows how to connect to the Central repository and download additional plug-ins. When it is launched for the first time, it will download a set of plug-ins, which in turn have dependencies on other JAR files that will be resolved automatically prior to project building.

Fortunately, these are cached in the local Maven repository (~/.m2/repository by default), so this is done only once. The repository can be cleaned or removed; the next time Maven runs, it will download any needed plug-ins again.

When mvn archetype:generate is executed, a sample Java project is created. This creates a pom.xml file with the groupId, artifactId, and version given, and sets it up for a Java project.

When mvn package is executed, the operation depends on the packaging type of the project. This will be jar if not specified, and the default package operation for jar is to run the compile, then the run test, and finally create the JAR file of the package.

The maven quickstart plugin is a useful way of creating a pom.xml file with known good values, and a way of verifying connectivity to the outside before moving ahead with the Eclipse-specific Tycho builds. If there's a problem with these steps, check the troubleshooting guides at http://maven.apache.org/users/ for assistance.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset