You are on page 1of 13

Maven - Build Connoisseur

Maven - What, Why, and When?

What is Maven?

 Maven is a project management tool for Java projects.


 It manages a project's build, reporting and documentation.

Why to Use Maven?

 Convention over Configuration - a quick project setup


 Project Modularization - standard directory structure
 Dependency Management - adds jars/libraries/dependencies automatically
 Plugin-based architecture - more powerful builds

When to Use Maven?

 Project Dependency tree is fairly deep and upgrades frequently.


 There is a need for streamlined continuous build, integration and testing.
 Build Portability is required to maintain consistency across different
environments.
 Quality Project Information is required for reporting/documentation purposes.

POM Demystified
What is POM?

 Project Object Model is the Maven project configuration file.


 It describes the project, declaratively.
 It defines what the project is all about and how it should be built.
 It is an XML file located in base directory of a project.

POM is composed of elements and configurations.

Elements in POM

 Project
 modelVersion
 groupId
 artifactId
 version

A combination of groupId, artifactId and version is called the GAV coordinates, which


uniquely identifies a project.

Configurations in POM

 Dependencies
 Plugins
 Goals
 Build Profiles

POM Composition

Life Cycles, Phases, and Goals


Lifecycl
e vs Phase, Plugin vs Goal
Life Cycle vs Phase

 Life Cycle is a collection of phase in a sequence.


 When you call a phase, it will also call all the phases before it.

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.

mvn clean is the same asmvn pre-clean clean:clean.

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.

Goals to phase binding depends on packaging type

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

1. A plugin can have multiple goals.


2. A plugin may not necessarily be bound to a phase, meaning a plugin can be standalone (tool). Check out Available Plugins to have an idea. For example 
help plugin is a tool plugin. Please see next tutorial for examples.
3. When a plugin goal is bound to a LifeCycle phase, it is basically to extend functionality not already found in that LifeCycle.
4. A LifeCycle phase can be bound to one or more goals of a plugin
5. A goal is also sometimes referred to Mojo
Life Cycle, Phases, Plugins, and Goals

Plugin Management in Multi-Module Project


 Manage plugin information used by child projects, as required.
 Parent POM defines configurations for various plugins used by different child projects.
 Each child project declares the plugins that it needs for the build.

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>

Standard Project Structure: Jar


Useful Maven Commands
General syntax:
mvn [options] <One or more Phases OR Goal Plugins[plugin options]>
Phase execution syntax:
mvn [phase-name]
Goal execution syntax:
mvn [plugin-name]:[goal-name]
ProTip:
Plugin Naming Convention
If group id is org.apache.maven.plugins then
maven-<plugin_name>-plugin
otherwise,
<plugin_name>-maven-plugin
Standard Project Structure: War

Useful Maven Commands


help:describe command
 Lists information of the current Maven project.
 Displays the list of phases in execution order in the following syntax
phase_name: plugin_group-
id:plugin_qualified_name:version:plugin_goal_name
> $mvn help:describe -Dcmd= compile
Output Snippet:

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

Decoding the above output format:


For the test phase below,

*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]

Useful Maven Commands


List all Goals in a plugin

mvn help:describe -Dplugin=[plugin_name]

Usage:
$mvn help:describe -Dplugin=compiler
Output Snippet:

[INFO] org.apache.maven.plugins:maven-compiler-plugin:3.3

Name: Apache Maven Compiler Plugin


Description: The Compiler Plugin is used to compile the sources of your project.
Group Id: org.apache.maven.plugins
Artifact Id: maven-compiler-plugin
Version: 3.3
Goal Prefix: compiler

This plugin has three goals:

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.

For more information, run 'mvn help:describe [...] -Ddetail'

Frequently Used Maven Commands


 mvn clean - Clean a project.
 mvn compile - Compile a project.
 mvn test - Run unit tests.
 mvn package - Build a package.
 mvn verify - Run integration test.
 mvn install - Install a package into local repository
 mvn -DskipTests=true install - Install an artifact into the local repository
and skip unit and integration test execution.
 mvn deploy - Deploy artifact into enterprise repository.
 mvn dependency:tree - Display project dependencies.
 mvn help:active-profiles - Display all profiles.

You might also like