Time for action – installing Maven

This step 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 Jars from Central 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 latest Maven zip (Windows) or Maven tgz (for macOS/Linux).
  2. Unzip/untar the install into a convenient directory, referred to in these instructions as MAVEN_HOME.
  3. Either add MAVEN_HOME/bin to the PATH or specify the full path to the Maven executable; run mvn –version, and a version message should be printed out. Maven requires a JDK (not just a JRE), which should be installed by following the instructions from the Java site.
  4. Run mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.1 (all on one line) to create a new Maven project.

    Note

    Maven can also be run with Eclipse; if the m2e Eclipse tools are installed, File | New | Project… | Maven | Maven Project will show a wizard that allows an archetype to be chosen and the details entered graphically:

    Time for action – installing Maven
  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, press Enter to take the defaults of 1.0-SNAPSHOT and com.packtpub.e4 respectively.
  8. Finally, press 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] 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 initially launches, it will download a set of plug-ins, which in turn have dependencies on other Jars that will be resolved automatically prior to the project building.

Fortunately, these are cached in the local Maven repository (which defaults to ${user.home}/.m2/repository), so this is done only once. The repository can be cleaned or removed; the next time Maven runs, it will download any required 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 default to jar if not specified, and the default package operation for jar is to run the compile, then run test, and finally create the Jar file of the package.

The Maven quickstart archetype 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 out 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
18.222.35.21