0% found this document useful (1 vote)
711 views11 pages

Maven Build Connoisiure

The document provides an overview of a course on mastering Maven features. It discusses topics that will be covered like build profiles, properties, plugins, dependencies, multi-module projects, and troubleshooting. It also summarizes key Maven concepts like what Maven is used for, when it should be used, the project object model (POM) file, lifecycles and phases, and plugins and goals. Profile configuration and activation are also summarized.

Uploaded by

Parijat Saurabh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
711 views11 pages

Maven Build Connoisiure

The document provides an overview of a course on mastering Maven features. It discusses topics that will be covered like build profiles, properties, plugins, dependencies, multi-module projects, and troubleshooting. It also summarizes key Maven concepts like what Maven is used for, when it should be used, the project object model (POM) file, lifecycles and phases, and plugins and goals. Profile configuration and activation are also summarized.

Uploaded by

Parijat Saurabh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Maven Build Connoisseur - Course Overview

This course ensures that you fathom the below Maven features.

 How to configure Build Profiles and activate them?


 How to play with properties and Resource Filtering?
 How to employ essential Maven Plugins?
 Dependency Mechanism in Maven - its scope, conflicts, and resolution
 Maven multi-module project development
 Couple of ad-hocs - Building assemblies and troubleshooting Maven

Having skimmed the basic concepts in the Maven Coalescing Pipeline, let's do a recap before comprehending the depths of Maven.

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


 A Maven life cycle consists of a sequence of phases, and each phase consists of a sequence of goals.

Lifecycle 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
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>


8 of 15

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.

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.

Types of build profiles

Build Profile Type Defined in


Per project pom.xml
Per User/Developer Maven settings.xml (%USER_HOME%/.m2/settings.xml)
Global Maven global settings.xml (%M2_HOME%/conf/settings.xml)
Profile Configuration in Pom.xml

 Profiles are configured within <profiles></profiles> under element <project>.


 Each profile is defined within <profile></profile> under element <profiles>.
 Each profile is identified by a unique profile Id.
 A profile is allowed to override the below elements of a Maven build.

Elements allowed in a Profile Configuration:

<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

Ways in which build profiles of Maven can be activated/triggered:

 Explicitly using commands


 Maven settings
 Based on environment variables
 Operating system settings
 Present/missing files

Profile Activation - Explicit


Configure profiles in Pom.xml

. . .
<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 :

[INFO] --- maven-antrun-plugin:1.1:run (id.compile) @ maven-test ---


[INFO] Executing tasks
[echo] Connected to DB :jdbc:mysql://localhost:3306/dev
$mvn compile -Pprod
O/P :

[INFO] --- maven-antrun-plugin:1.1:run (id.compile) @ maven-test ---


[INFO] Executing tasks
[echo] Connected to DB :jdbc:mysql://live01:3306/prod

Profile Activation - Maven Settings


Activate profile in Maven settings.xml file available at %USER_HOME%/.m2 directory as follows:

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

Profile Activation - Environment Variables

 Provide the profile activation details through environment variables


 Create an environment variable 'env' and set its value as 'test'
 In pom.xml, configure profile 'test' to be activated, if 'env' property value is set as 'test'
<profile>
<id>test</id>
<activation>
<property>
<name>env</name>
<value>test</value>
</property>
</activation>
</profile>
$mvn compile
Note: Profile test will be activated automatically without explicitly activating it using -P option

Profile Activation – OS settings


Pom.xml

...
<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 Activation –Present/Missing Files


Profile1 will be activated if file1.properties is present in src/main/resources folder.

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

List Active Build Profiles


 Maven profiles can be defined in one or more of the below files.
1. pom.xml

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

Active Profiles for Project 'My Project':

The following profiles are active:


- my-settings-profile (source: settings.xml)
- my-external-profile (source: profiles.xml)
- my-internal-profile (source: pom.xml)

You might also like