Lifecycle, Phases, goals, and Plugins?!
Maven Lifecycle
Maven is a build automation tool at heart. It has three built-in lifecycle that clearly define how the project will be build. There are three built-in build lifecycles:
- default: handles the actual project compilation and deployment
- clean: handles project cleaning, removing the target folder
- site: handles the creation of project site documentation
You "can" add more lifecycle but then that go against the point of creating those three built-in build lifecycles. Those three built-in ones should be the standard of all the projects, you can create your own plugins and then hook it into the phases within the lifecycle.
Adding more lifecycle would take away the ease of reproducing the build and maintaining it.
Each of the lifecycle consists of phases. So you have life cycles like default, it consists of phases, and each phases consists of goals.
Phases in default
Not going to list them all but here are some of the phases in order:
- validate
- compile
- test
- package
- verify
- install
- deploy
When you execute a phase all the goals tied to the goal will be executed. In addition, when you execute a phase all the previous steps will be executed. For example, executing mvn install
will execute, validate, compile, test, package, verify, and install in order.
There is no way to execute the "lifecycle", to execute the lifecycle you would just specify the last phase to run through the entire phases. In this case running mvn deploy
will in a sense run the entire lifecycle
Maven phases
Let's clear something up, all the available goals are provided by plugins. The compile phase's goals are provided by the "maven-compiler-plugin", the package phase's goal are provided by the "maven-jar-plugin", you get the idea.
Official maven plugin like compile, package have standard name of "maven-<name>-plugin", whereas non-official maven plugin have "<name>-maven-plugin" naming convention.
The implication is that when you want to invoke the goal directly without invoking the phase if you are using official maven plugin is just "<name>:<goal>" for example, mvn compiler:compile
. You don't need to do mvn maven-compiler-plugin:compile
. However, unofficial maven plugins then you would need to provide the entire plugin name to invoke the goal.
A build phase doesn't necessarily need to have any goal bound to it. If it has no goal bound to it, it will simply not execute any goal.
However, the same goal can be bounded to multiple different build phases, so that goal will just be executed multiple times when the phase is executed.
Furthermore, not all the goals from a plugin are bind a phase. For example, the maven-compiler-plguin
have two goals, compile / testCompile
. However, only the compile
goal is bound to the compile phase. You can add testCompile
to the phase by adding an <executions>
section in your plugin tag, will go more over that in the next section.
Maven goals (Mojos)
Goals are the actual task that are executed, they help building and manage the project. Phases and lifecycle are essentially just abstraction of goals.
Goals may or may not not bound to a phase, those that are tied to a phase will be executed when you run the phase. Those are not bound to a phase, you can invoke them directly. By using the syntax discussed above.
To bound a plugin's goal to a phase if they are not bound by default, or you would like to bound to a different phase, i.e. adding jar plugin's jar goal to the compile phase. Whenever you run mvn copmile
it will also package it as a jar, as oppose to whenever you run mvn package
if you don't want to run test phase before it. What you would do is to add a <executions>
section in your pom.xml file.