Maven Lifecycle Phases and Goals
The previous chapter explained the concepts of Maven Lifecycle and Phases. Now, let’s understand the link between phases and Plugin Goals.
Phases and Plugin Goals
Lifecycle phases don’t come with any functionality; to carry out a task they rely on plugins. For example, the compile phase doesn’t actually compile the Java source, instead it delegated the job to compile goal of maven-compiler-plugin which invokes the rusty old javac to compile the project.
When a lifecycle phase is executed Maven binds specific plugin goals to lifecycle phases depending on the project and packaging type. For example, when we run mvn package
in a Java Project, the plugins that are plugged into phases are as shown in the figure.
To compile phase, Maven binds compile goal of maven-compiler-plugin. Similarly, to test phase, it binds test goal of maven-surefire-plugin and so on.
What happens at package phase is bit interesting. In a Java Project, Maven binds jar goal of maven-jar-plugin. However, when we run the same command in a webapp project, Maven binds war goal of maven-war-plugin.
Which plugin and goal binds to phases depends on project and also, package (jar, war etc.,) type. The command mvn help:describe -Dcmd=<phase>
is a useful command not only to list the lifecycle phases but also to know the bounded goal and plugin version. Run the following command from the project directory that contains the pom.xml.
$ mvn help:describe -Dcmd=clean
'clean' is a lifecycle with the following phases:
pre-clean: Not defined
clean: org.apache.maven.plugins:maven-clean-plugin:2.5:clean
post-clean: Not defined
The help:describe load the appropriate lifecycle based on phase, outputs the list of phases that will be executed for that lifecycle. It also shows which plugin:goal is bounded to each phase. As we can see in the screenshot, no goals are bounded to pre-clean and post-clean phases. For clean phase, clean goal of maven-clean-plugin (plugin version 2.5) is attached.
Let’s run the same command for deploy phase. As deploy phase is part of default lifecycle, Maven loads that lifecycle and outputs its phases and attached plugin:goals. The output list is trimmed for brevity.
$ mvn help:describe -Dcmd=deploy
It is a part of the lifecycle for the POM packaging `jar`.
This lifecycle includes the following phases:
generate-resources: Not defined
process-resources: maven-resources-plugin:2.6:resources
compile: maven-compiler-plugin:3.1:compile
process-classes: Not defined
process-test-resources maven-resources-plugin:2.6:testResources
test-compile: maven-compiler-plugin:3.1:testCompile
test: maven-surefire-plugin:2.12.4:test
package: maven-jar-plugin:2.4:jar
verify: Not defined
install: maven-install-plugin:2.4:install
deploy: maven-deploy-plugin:2.7:deploy
Invoke Plugin Goal
The mvn
command accepts, apart from the options, only two things - goal or phase.
We have already seen the usage of phase in commands such as mvn compile
etc., It is also possible to directly call a plugin goal. Above, we ran mvn help:describe -Dcmd=deploy
which is an example of directly calling a goal which executes describe goal of maven-help-plugin.
While both - calling a phase or goal - ultimately uses some plugins to carry out tasks, there is a subtle difference between them. When a phase is invoked using mvn command, all preceding phases are stepped through sequentially one after another and the attached plugin goals are executed, but when a goal is called only that goal is executed.
Let’s Summarize
Lifecycle, Phase, Plugin and Goals are the core of Maven and lets’ go through the concepts learned so far:
Maven comes with three lifecycles - default, clean and site.
each lifecycle is made up of lifecycle phases and in all, there are 28 phases - default 21, clean 3 and site 4.
lifecycle phases by themselves doesn’t have any capabilities to accomplish some task and they rely on plugins to carryout the task.
when a lifecycle phase is invoked using mvn command, all preceding phases are executed sequentially one after another, but when a goal is called only that goal is executed.
depending on project and packaging type, Maven binds various plugin goals to lifecycle phases. The plugin goal carryout the task delegated to them.