Maven

Installation

  • Make sure %JAVA_HOME% is set (points to the root of the JDK)
  • Add the Maven bin/ folder to %PATH%

The repository will be in C:\Users\%username%\.m2\repository

Basic commands

Create a project

mvn archetype:generate \
    -DgroupId=ch.frattino \
    -DartifactId=<name of your artifact> \
    -DarchetypeArtifactId=maven-archetype-quickstart \
    -DinteractiveMode=false 

Generate Eclipse project files

mvn eclipse:eclipse

In case you add a new dependency, rerun this.

To get the source code too :

mvn eclipse:eclipse -DdownloadSources

To get the source code and the JavaDoc :

mvn eclipse:eclipse -DdownloadSources -DdownloadJavadocs

Build and package a project

mvn package

Dependencies

Find a library

For example, let's search for log4j. Google this :

site:www.ibiblio.org log4j

First result, open maven-metadata.xml. You'll find that :

<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<versioning>
 <latest>1.2.17</latest>
 ...
</versioning>

Add a dependency

<dependencies>
  ...
  <dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
    <scope>compile</scope>
   </dependency>
</dependencies>

Deploy to local repository

mvn install

Deploy to remote repository

Resources and variables

Add resources

Just add them to src/main/resources or src/test/resources.

More infos

Filter resources

To use variables in your static resources :

<build>
  <resources>
    <resource>
      <directory>src/main/resources</directory>
      <filtering>true</filtering>
    </resource>
  </resources>
</build>

To load variables from a filter (.properties file) :

<build>
  <filters>
    <filter>src/main/filters/filter.properties</filter>
  </filters>
  ..
</build>

To define properties in your POM :

<properties>
  <any.property.in.pom>hello</any.property.in.pom>
</properties>

Examples of variables :

Variable Description Example
${pom.name} Application name maven-test
${pom.version} Application version 1.0-SNAPSHOT
${pom.build.finalName} Full build name maven-test-1.0-SNAPSHOT
${settings.localRepository} User local repository C:\\Users\\cfr\\.m2\\repository
${java.version} Java property (here : Java version) 1.8.0_66
${user.home} Java property (here : User home) C:\\Users\\cfr
${any.property.in.filter} Any property from filter
${any.property.in.pom} Any property from POM file hello
${any.property.in.cmd} Property from command line
mvn process-resources -Dany.property.in.cmd=foobar
foobar

If you just want to test the filtering of resources, just run

mvn process-resources

and look into target/classes.

More infos

JavaDoc

So that JavaDoc is generate when you run “mvn site”, add this in your POM :

<reporting>
 <plugins>
  <plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-javadoc-plugin</artifactId>
   <version>2.10.3</version>
   <configuration>
   </configuration>
  </plugin>
 </plugins>
</reporting>

The Javadoc will be visible under Project Reports > JavaDocs

Build more than one projects at once

Terminology

Archetype

In Maven, an archetype is a template of a project which is combined with some user input to produce a working Maven project that has been tailored to the user's requirements.

Phases

  • validate: validate the project is correct and all necessary information is available
  • compile: compile the source code of the project
  • test: test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
  • package: take the compiled code and package it in its distributable format, such as a JAR.
  • 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 is valid and meets quality criteria
  • install: install the package into the local repository, for use as a dependency in other projects locally
  • deploy: done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.
  • clean: cleans up artifacts created by prior builds
  • site: generates site documentation for this project
Print/export