Maven defines 4 items of a build process:
|
Lifecycles |
Three built-in lifecycles (aka build lifecycles): |
|---|---|
|
Phases |
Each lifecycle is made up of phases, e.g. for the |
|
An artifact that provides one or more goals Based on packaging type ( | |
|
Goals |
The task (action) that is executed. A plugin can have one or more goals. One or more goals need to be specified when configuring a plugin in a POM. Additionally, in case a plugin does not have a default phase defined, the specified goal(s) can be bound to a phase |
Maven can be invoked with:
- a phase (e.g
clean,package) <plugin-prefix>:<goal>(e.g.dependency:copy-dependencies)<plugin-group-id>:<plugin-artifact-id>[:<plugin-version>]:<goal>(e.g.org.apache.maven.plugins:maven-compiler-plugin:3.7.0:compile)
with one or more combinations of any or all:
mvn clean dependency:copy-dependencies package
More Details
maven contains 3 built-in life cycles:
- default (build): the main life cycle as it’s responsible for project deployment
- clean: to clean the project and remove all files generated by the previous build
- site: to create the project’s site documentation
Each life cycle consists of a sequence of phases:
- default build life cycle consists of 23 phases as it’s the main build lifecycle
- clean life cycle consists of 3 phases
- site lifecycle is made up of 4 phases
A phase represents a stage in the Maven build lifecycle
Here are some of the most important phases in the default build lifecycle:
- validate: check if all information necessary for the build is available
- compile: compile the source-code
- test-compile: compile the test-source-code
- test: run unit tests
- package: package compiled source-code into the distributable format (jar, war, …)
- integration-test: process and deploy the package if needed to run integration tests
- install: install the package to a local repository
- deploy: copy the package to the remote repository
For the full list of each lifecycle’s phases, check out the Maven Reference.
commands:
- Run lifecycle up to phase:
mvn $PHASE
Each phase is a sequence of goals
Here are some of the phases and default goals bound to them:
- compiler:compile – the compile goal from the compiler plugin is bound to the compile phase
- compiler:testCompile is bound to the test-compile phase
- surefire:test is bound to test phase
- install:install is bound to install phase
- jar:jar and war:war is bound to package phase
commands:
- List all goals bound to a phase and their plugins:
mvn help:describe -Dcmd=$PHASE
A plugin is a group of goals. However, these goals aren’t necessarily all bound to the same phase
commands:
- list all goals in a specific plugin:
mvn $PLUGIN:help - run a specific goal, without executing its entire phase (and the preceding phases):
mvn $PLUGIN:$GOAL
A plugin is a collection of goals also called MOJO (Maven Old Java Object).
Analogy: Plugin is a class and goals are methods within the class