Maven crash course

Apache Maven basics

Basic concepts

Introduction

  • Automatizes project construction process
  • Alternative to ANT (older) or Gradle (newer)
  • Maven repository stores maven packages on a remote mirror
Command Action
mvn -v Get Maven version info
mvn archetype:generate -Dgroup=org.test -Dartifact=test-maven -DarchetyoeArtifactId=maven archetyoe-quickstart -DinteractiveMode=false create a project from a group, with a name and with and using an archetype
mvn <phase> Execute a phase
mvn <phase> -P <profile> Execute a phase for a certain profile

How does it work?

graph LR;
A[Java project]
B[Spring library];
C[Servlet library];
D[pom.xml];
E((compile and build));
F(MAVEN install);
G[Java apps]
H(Maven local repository)
I(Maven central repository)
A --> E;
E --> G;
E --> F;
F --> E;
H --> F;
I --> F;
B --> D;
C --> D;
D --> F;

Lifecycles

  • Lyfecyle: flow to build software
  • Phase: step in in the lifecylce flow
  • Goal: granular task in a phase
  • plugin: goal logic group

pom.xml content

  • Content subsets

    • POM relationaships
    • General product information
    • Build settings
    • Build environment
  • Goals and phases (can be configured via plugins)

    • Goal: what should Maven do?
    • Phase: execute a phase and those before it
Phase Action
clean Removes the artifacts from previous compilations
validate Validates elements needed
compile Compiles the code
test Launch unit tests
site Generates the documentation (e.g. Javadoc)
package Deploys and execution and test it
integration.test Deploys and execution and test it
verify Quality validations (e.g. SONAR rules)
install Install on local repository to solve dependencies
deploy Install on integration or release, to some remote repository
  • Dependencies and scopes
    • Packages may reference other packages
    • The scope explains in which phase is that dependecy needed
    • Dependencies can be excluded via <exclusions><exclusion></exclusion></exclusions>
Scope Action
compile Needed for compilephase. Includes the packages
test Needed for testphase. Doesn’t include the packages
provided Needed for compile phase, but doesn’t add them on execution phase
system Needed for compile phase, added as path
import Includes an artifact with pom format
  • Build plugins
Plugin Action
surefire Launches the unit tests
checkstyle Checks the source style
clover Evaluates the code coverture
enforcer Verifies environment settings
assembly Creates ZIP files and other packages with their dependencies (JARs)
  • Properties
Property Description
${env.PATH} OS environment var
${project.groupId} Project group identifier
${project.artifactDir} Project group identifier
${project.baseId} Path of the xml file (and base project)
${settingst.localRepository} Path of the local user repository
${java.home} Java system attribute
${java.vendor} JRE provider
${my.somevar} Java system attribute
  • Profiles configuration: provides Maven configuration which can be activated via command line (or triggered automatically)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    <project>
    <profile>
    <id>YourProfile</id>
    <dependencies>
    <dependency>
    <groupId>com.yourcompany</groupId>
    <artifactId>yourlib</artifactId>
    </dependency>
    </dependencies>
    </profile>
    </project>
  • Reporting configuration

    1
    2
    3
    4
    5
    6
    7
    <reporting>
    <plugins>
    <plugin>
    <artifactId>maven-javadoc-pluging</artifactId>
    </plugin>
    </plugins>
    </reporting>

POM basic example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<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">
<!-- POM relationaships -->
<modelVersion>4.0.0</modelVersion>
<groupId>com.everis</groupId>
<artifactId>test-maven</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>test-maven</name>
<url>http://maven.apache.org</url>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
<exclusions>
<exclusion>
<artifactId>hamcrest-core</artifactId>
<groupId>org.hamcrest</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<repositories>
<repository>
<id>Central</id>
<url>http://mvnrepository.com</url>
</repository>
</repositories>
</project>

Advanced projects

Modular projects

A parent project may contain multiple modules

  • On a module project you need to set the parent data

    1
    2
    3
    4
    5
    6
    <parent>
    <groupId>org.test.mygroup</groupId>
    <artifactId>my-parent</artifactId>
    <version>2.0</version>
    <relativePath>../my-parent</relativePath>
    </parent>
  • On a parent, you need to specify the modules in the right compilation order

    1
    2
    3
    4
    <modules>
    <module>util</module>
    <module>app</module>
    </modules>