Professional Documents
Culture Documents
What is Maven?
POM Demystified
What is POM?
Elements in POM
Project
modelVersion
groupId
artifactId
version
Configurations in POM
Dependencies
Plugins
Goals
Build Profiles
POM Composition
Plugin vs Goal
Plugin is an artifact.
Plugin is a collection of goals.
Goal is like an action in Plugin.
Analogy: If Plugin is a class then goals are methods within the class.
Syntax: mvn phase:goal
Putting it all together: Life Cycle, Phase, and Goal
Phase itself does nothing. It is just for defining the order/sequence in a life cycle.
Goal is the one that does the work.
When a phase is called, it will actually call a goal which is linked to that phase.
Explore more here
Maven Lifecycle, Phases and Goals
[Last Updated: Mar 14, 2017]
1. Goal is the single unit of task which does some real work. For example the compile goal (runs as mvn compiler:compile) which compiles the Java source.
All goals are provided by plugins, either by default plugins or by user defined plugins (configured in pom file).
2. Phase is a group of ordered goals or in other words: zero or more plugin goals are bound to a phase (either by default or by user).
For example the compile phase (runs as "mvn compile") consists only compiler:compile goal (plugin). Running a phase basically runs the plugins bound with it.
3. Build Lifecycle is a group of ordered phases. There are three built-in lifecycle: default, clean and site. Please see the list of phases attached with each lifecycle.
Build lifecycle cannot be executed, they are conceptual. if we execute a phase, For example mvn compile, all phases up to and including that phase
(i.e. validate >generate-sources> process-sources>generate-resources> process-resources> compile) will get executed. A phase with zero plugin goals does
nothing.
To understand the general idea behind above concepts, consider this diagram: If we execute phase 2 it will first run phase 1
(the two plugins bound to phase 1 will get executed) then phase 2 (the only one plugin goal bound with it will get executed).
If we run last phase all preceding phases will get executed first. Generally speaking, if we want to run some phase (or even goal/plugin), all pre-required task will run first.
Note Phase 3 does nothing cause no goals are bound to it. In maven default life-cycle too, there are number of phases which does nothing.
They are actually kind of place holders for the users plugins or different packaging types to bind goals.
Based on the packaging of a project (jar, war, ear, etc), different maven goals will be bound to different phases of the maven lifecycle.
Please check out the reference docs here. One way to list this information is to use maven plugin help:describe which we will explore in the next tutorial.
Difference between Plugin and Goal
Parent POM
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
Child POM
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
</plugins>
</build>
It is a part of the lifecycle for the POM packaging 'jar'. This lifecycle includes
the following phases:
* validate: Not defined
. . .
* compile: org.apache.maven.plugins:maven-compiler-plugin:3.1:compile
. . .
* test-compile: org.apache.maven.plugins:maven-compiler-plugin:3.1:testCompile
* process-test-classes: Not defined
* test: org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test
* package: org.apache.maven.plugins:maven-jar-plugin:2.4:jar
. . .
* install: org.apache.maven.plugins:maven-install-plugin:2.4:install
* deploy: org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy
*test: org.apache.maven.plugins:maven-surefire-plugin:2.10:test
[phasename]: [plugingroup-id]:[pluginqualifiedname]:[version]:[plugingoalname]
Plugin goal syntax for the above phase would be
$mvn surefire:test
mvn [pluginname]:[goalname]
Usage:
$mvn help:describe -Dplugin=compiler
Output Snippet:
[INFO] org.apache.maven.plugins:maven-compiler-plugin:3.3
compiler:compile
Description: Compiles application sources
compiler:help
Description: Display help information on maven-compiler-plugin.
Call mvn compiler:help -Ddetail=true -Dgoal=<goal-name> to display
parameter details.
compiler:testCompile
Description: Compiles application test sources.