Maven

Java building tool, provide easy (with sensible defaults) and uniform build system, provide guidance to best practice.

Basics

After installation, check configuration with "mvn --version"

Maven Manages

  • Builds

  • Documentation

  • Reporting Dependencies

  • SCMs (software configuration management)

  • Releases

  • Distribution

  • Mailing list

Convention over Configuration:

  • sensible defaults are provided and created. developer only needs to place files accordingly and no need to configure in pom.xml

Standard Directory Layout

http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html

  • Resources: resources can be placed under {base}/src/main/resources and will be packed into JAR.

Reference

http://maven.apache.org/ref/current/

Project Coordinates & Repository

  • A project is unique identified by:

      • groupId - unique id of the organization or group that created the project, typical in form of fully qualified domain name (example: org.apache.maven.plugins)

      • artifactId - unique base name of the primary artifact (typically a JAR) being generated by the project; Secondary artifacts (such as source bundles) also use this base name; example: "myapp"

      • version - version of generated artifact

      • packaging - such as jar

  • Marven Space

    • Repositories

      • public

        • can maintain mirror

      • private

      • local

        • downloaded artifacts (also the POM) from public

        • "mvn install" install your own project into local repository

        • so to share dependencies among local projects

    • Repository format

      • /<groupId>/<artifactId>/<version>/<artifactId>-<version>.<packaging>

    • Coordination system

      • groupId, artifactId:version:packaging

To deploy project into a remote repository instead of the local one

Put the following into project POM

<distributionManagement>

<repository>

<id>mycompany-repository</id>

<name>MyCompany Repository</name>

<url>scp://repository.mycompany.com/repository/maven2</url>

</repository>

</distributionManagement>

And below into maven configuration

<servers>

<server>

<id>mycompany-repository</id>

<username>jvanzyl</username>

<!-- Default value is ~/.ssh/id_dsa -->

<privateKey>/path/to/identity</privateKey> (default is ~/.ssh/id_dsa)

<passphrase>my_key_passphrase</passphrase>

</server>

</servers>

To set up an Internal Repository

See: http://maven.apache.org/repository-management.html

See Introduction to Remote Repositories: http://maven.apache.org/guides/introduction/introduction-to-repositories.html

POM

Occurs at 3 levels: Project, Installation, User

Generally work out-of-box and no configuration is needed.

Possible configurations:

  • Local Repository is by default at ${user.home}/.em2/repository/

  • Proxy

  • Security and Deployment Settings (sensitive server information such as username and password, private key, etc)

  • Alternative (mirrors) for Repositories

pom.xml (Project Object Model) is essentially one-stop for finding anything related to a project.

  • Inheritance: all POMs inherit from a parent (defined or not)

  • Super POM: the base POM, contains defaults

  • Effective POM: configurations from super pom plus project configurations; used to execute relevant goal. "mvn help:effective-pom" to check

Plugins

  • a collection of one or more goals;

  • maven core does not do anything, plugins do

  • downloaded as needed from central repository

    • if version not specified latest one will be used

  • includes

  • simple core plugins (like Jar, Compiler, etc.)

  • specialized plugins: Hibernate, etc.

  • customized plugins

...

<build>

<plugins>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-compiler-plugin</artifactId>

<version>2.5.1</version>

<configuration>

<source>1.5</source>

<target>1.5</target>

</configuration>

</plugin>

</plugins>

</build>

...

List of plugins: http://maven.apache.org/plugins/

Guide to configuring plug-ins: http://maven.apache.org/guides/mini/guide-configuring-plugins.html

Goals

a specific task

  • configured via configuration properties (if differ from default) in POM

  • when executes, has access to the POM and look for parameters

  • expressed as "pluginId:goalId"

Build lifecycle

  • defined sequence of phases

    • phases are intentionally vague, can mean different things to different projects

    • goals can be attached to a phase

    • each phase can have 0-many goals

  • when Maven execute "mvn install" (install is a phase)

    • it executes all phases up to the install phase

    • in executing a phase it executes all goals bound to the phase

    • it's equivalent to executing a sequence of plugin goals but using phases makes life easier

    • so developer can jump from one project to another without knowing much about the details of each

Dependency Management

Dependency resolution sequence:

  • First looked up in local repository

  • Then, download any JARs and other dependencies from a central JAR repository

  • Need to define at least 4 things: groupId, artifactId, version, scope (compile, test, runtime).

  • From Id to path

    • "junit:junit:3.8.1" => /junit/junit/3.8.1/junit-3.8.1.jar

  • Transitive dependencies (dependency depends on dependency)

    • ability to exclude transitive dependencies

  • Scope

    • "test"

    • "compile"

    • "runtime"

    • "provided" - tells Maven that a dependency is needed for compilation but not be bundled with the output of a build (container provided libraries such as Servlet API JAR)

<dependencies>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>3.8.1</version>

<scope>test</scope>

</dependency>

</dependencies>

See introduction to dependency mechanism: http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html

Archetype

Archetype is project template

aimed to make Archetype mechanism additive

  • portions of a project be captured in an archetype

  • pieces or aspects of a project can be added to existing projects (incremental).

List of Maven Archetypes: maven.apache.org/archetype/maven-archetype-bundles/

Search Archetypes (central repository): search.maven.org

Build Lifecycle

Lifecycle: well defined sequence of phases, defines order in which goals are to be executed

3 built-in lifecycles: default, clean, site

Default lifecycle's build phases:

  • validate - validate site correct and all necessary information available

  • compile

  • test - test using suitable unit testing framework

  • package

  • integration-test - process and deploy the package if necessary, into an environment where integration tests can be run

  • verify - run any checks to verify the package

  • install - install into local repository for use as dependency in other projects

  • deploy - done in an integration or release environment, copy the final package to remote repository for sharing

Execution is in sequence. Execute "mvn [target]" runs the build phase, and any preceding one. Multiple target can be executed like "mvn clean install".

Further see http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html

Working on multiple project

Create a parent project pom.xml at parent directory of two projects placed in it. Contents as follows:

<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/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>com.mycompany.app</groupId>

<artifactId>app</artifactId>

<version>1.0-SNAPSHOT</version>

<packaging>pom</packaging>

<modules>

<module>my-app</module>

<module>my-webapp</module>

</modules>

</project>

The above tells maven to do operations over the set of projects.

Use dependency in sub projects to adjust relationship and include products of one project to another. (not shown)

Add <parent> element to sub projects to refer to parent project. Like below

<parent>

<groupId>com.mycompany.app</groupId>

<artifactId>app</artifactId>

<version>1.0-SNAPSHOT</version>

</parent>

Then work on the parent project.

Reference: https://maven.apache.org/guides/mini/guide-multiple-modules.html

Reactor sorting:

The following relationships are honored when sorting projects (so they build in correct order):

  • a project dependency on another module in the build

  • a plugin declaration where the plugin is another modules in the build

  • a plugin dependency on another module in the build

  • a build extension declaration on another module in the build

  • the order declared in the <modules> element (if no other rule applies)

There are certain command line options to customize the reactor's behavior (check reference).

Building Multi-module Projects

http://books.sonatype.com/mvnex-book/reference/multimodule-sect-building-multimodule.html

mvn clean install - from the parent project (clean is recommended)

once installed, run with mvn jetty:run (on child module)

Documentation

$mvn site

Executes "site" lifecycle, processing site content under src/site to generate reports under target/site

Site includes some default reports

  • Information about the project, dependencies, developers

  • Unit test report

  • Javadoc

  • configurable reports (configure in POM)

    • Clover - unit test coverage

    • JXR - cross-referenced source code for reviews

    • PMD - analyze source code for coding problems

    • JDepend - dependencies