Wednesday, October 14, 2015

Maven Build Lifecycle in a nutshell

Maven is a build automation tool mainly used in java projects.When it comes to maven, Build lifecycles are very important topic.
There are 3 out of the box maven lifecycles know as default, clean and site. Out of these three, most of the time we are using default and clean.
A build lifecycle is made up of number of predefined phases or in other words stages.
A Phase is made up of goals. A goal represents a specif task and they are defined within associated plugin.
These plugin goals can explicitly execute with mvn.
A goal doesn't always need to be associated with a phase. Therefore a goal not bound to any build phase could be executed via direct invocation.

For the clear understanding please find the mvn command provided below:
mvn clean dependency:copy-dependency
  • clean - build phase (Once you execute this command all the build phases up to this build phase will get invoked. In other words, pre-clean and clean will get executed)
See Lifecycle references for more details.
  • dependency:copy-dependency - the goal 'copy-dependency' in 'maven-dependency-plugin' plugin.

There are 2 ways of using these build lifecycles.

1. Packaging (e.g.: war/jar/ear)
The packaging type defined in pom.xml will cause to run list of predefined goals bound to build phases of default lifecycle.
See Plugin Bindings for more details.

2. Plugin
By specifying plugins explicitly in your pom file, you can incorporate goals to specific phases of a build lifecycle.
- A plugin can have one or more goals.
- Each goal can be invoked in multiple phases.
Goal is bound to a lifecycle phase and if there are multiple goals bound to a particular phase, they will be invoked according to the oder in which they have defined.  

mojo - Maven + Pojo
- Mojo is an executable goal in maven.

Reference: https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html