Maven Build Connoisiure
Maven Build Connoisiure
This course ensures that you fathom the below Maven features.
Having skimmed the basic concepts in the Maven Coalescing Pipeline, let's do a recap before comprehending the depths of Maven.
What is Maven?
POM Demystified
What is POM?
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
A Maven life cycle consists of a sequence of phases, and each phase consists of a sequence of goals.
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
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.
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>
8 of 15
It is a part of the lifecycle for the POM packaging 'jar'. This lifecycle includes
the following phases:
*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
[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.
mvn -DskipTests=true install - Install an artifact into the local repository and skip unit and integration test execution.
What is a Profile?
Set of configurable values applied to Maven Build.
Allows customizing builds for a specific environment.
Enables portability between different build environments.
<project>
<profiles>
<profile>
<id>...</id>
<build>
<defaultGoal>...</defaultGoal>
<finalName>...</finalName>
<resources>...</resources>
<testResources>...</testResources>
<plugins>...</plugins>
</build>
<reporting>...</reporting>
<modules>...</modules>
<dependencies>...</dependencies>
<dependencyManagement>...</dependencyManagement>
<distributionManagement>...</distributionManagement>
<repositories>...</repositories>
<pluginRepositories>...</pluginRepositories>
<properties>...</properties>
</profile>
</profiles>
</project>
Profile Activation
. . .
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.1</version>
<executions> <execution>
<id>id.compile</id>
<phase>compile</phase>
<goals><goal>run</goal></goals>
<configuration>
<tasks>
<echo>Connected to DB : ${db.url}</echo>
</tasks>
</configuration>
</execution></executions>
</plugin>
. . .
<profiles>
<profile>
<id>dev</id>
<properties>
<db.url>jdbc:mysql://localhost:3306/dev</db.url>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<db.url>jdbc:mysql://live01:3306/prod</db.url>
</properties>
</profile>
</profiles>
Activate profile explicitly using -P option
$mvn compile -Pdev
O/P :
<settings xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/settings-1.0.0.xsd">
<mirrors>
<mirror>
<id>com.testorg.companyname</id>
<name>Internal Artifactory Maven repository</name>
<url>http://repo1.maven.org/maven2/</url>
<mirrorOf>*</mirrorOf>
</mirror>
</mirrors>
<activeProfiles>
<activeProfile>dev</activeProfile>
</activeProfiles>
</settings>
...
<execution>
<id>id.compile</id>
<phase>compile</phase>
<goals> <goal>run</goal></goals>
<configuration>
<tasks>
<echo>${my.os}</echo>
</tasks>
</configuration>
</execution>
...
<profiles>
<profile>
<id>unix</id>
<activation>
<os>
<family>unix</family>
</os>
</activation>
<properties>
<my.os>I am on Ubuntu System</my.os>
</properties>
</profile>
<profile>
<id>windows</id>
<activation>
<os>
<family>windows</family>
</os>
</activation>
<properties>
<my.os>I am on Windows System</my.os>
</properties>
</profile>
</profiles>
...
If you are running Windows OS, profile windows will be activated automatically.
$mvn compile - [echo] I am on Windows System
If you execute from Ubuntu system, profile unix will be activated automatically.
$mvn compile - [echo] I am on Ubuntu System
<profile>
<id>profile1</id>
<activation>
<file>
<exists>src/main/resources/file1.properties</exists>
</file>
</profile>
Profile2 will be activated if file1.properties is not present in src/main/resources folder.
<profile>
<id>profile2</id>
<activation>
<file>
<missing>src/main/resources/file1.properties</missing>
</file>
</profile>
2. `profiles.xml`
3.
4. `~/.m2/settings.xml`
5.
6. ${M2_HOME}/conf/settings.xml
Multiple profiles can be activated at a time.
$mvn -P profile1,profile2 package
Maven Help plugin's active-profiles goal lists all the active profiles and where they have been defined.
$ mvn help:active-profiles