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 compile phase. Includes the packages |
test |
Needed for test phase. 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 | <project xmlns="http://maven.apache.org/POM/4.0.0" |
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>