You are on page 1of 23

DevOps 506: Intermediate

Maven
Persistent University
Module 5: Custom Builds
Key learning points

• Customizing the Lifecycle

• Key Lifecycle Phases

• Properties and Profiles

• Filtering Resources

• Site Generation and Reporting


Key Lifecycle Phases

• Clean Lifecycle (clean)

- pre-clean

- clean

- post-clean

4
Key Lifecycle Phases contd

• Default Lifecycle
- validate
- initialize
- generate-sources
- process-sources
- generate-resources
- process-resources
- compile
- process-classes
- generate-test-sources
- process-test-sources
- generate-test-resources
- process-test-resources
- test-compile
- process-test-classes
- test
- prepare-package
- package
- pre-integration-test
- integration-test
- post-integration-test
- verify
- install
- deploy
5
Key Lifecycle Phases contd

• Site Lifecycle (site)

- pre-site

- site

- post-site

- site-deploy

6
Customizing Lifecycle

• Reasons:

- The 3 fixed build "lifecycles" called clean, default and site suffice for the most common Maven artifact type, jar.

- However, for application server integration frameworks, it is NOT.

• Ways to customize the Maven build lifecycle

- Using (only) profiles

- Using "standalone" plugins

- Adding additional dedicated sub projects

- Using custom project (pom.xml) files

- Using a custom lifecycle extension

7
Customizing Lifecycle contd

• Two XML descriptors are required in addition to a custom plugin/Mojo to achieve this.

- 1) lifecycle.xml - specifies the lifecycle, phase and goal to be used when overriding the default lifecycle

- 2) components.xml - specifies the lifecycle mapping and phases that will be executed

- 3) A Mojo (Maven POJO) which extends AbstractMojo class and implements an execute() method. The Mojo is the
basis for our custom plugin.

• Ref:

- Maven: The Complete Reference

- https://developer.jboss.org/wiki/CreatingACustomLifecycleInMaven

8
Site Generation and Reporting

• Maven can be used to create a project site capturing information relevant to both the end users and developers.

• Maven can generate reports on Unit tests, Code coupling , Code quality etc

• Maven can be used to generate API documents.

9
Creating Project Site

• mvn site – Builds a Maven site

• mvn site:run – Builds a Maven site and shows the site in a browser

• Also see mvn site:deploy- Used to deploy the generated site using Wagon supported protocol to the site URL specified in the
<distributionManagement> section of the POM.

• Ref: https://maven.apache.org/plugins/maven-site-plugin/

10
Generating JUnit Reports

• Use Surefire Report Plugin to generate a report of the JUnit tests execution.

• Add the maven-surefire-report-plugin to pom.xml to include unit test report in the project site.

11
Generating JUnit reports

12
Generating JavaDoc

• JavaDoc plugin to generate javadocs for the project.

• Generate JavaDoc as part of the site generation.

13
Generating JavaDoc

14
Maven Profiles

• Profiles in Maven supports portability between different built environment.

• Profiles are specified in pom.xml files

• A "profiles" section in the pom.xml file can contain one or more profile definitions.

• To define two profiles called "development" and "production" simply enter the following fragment:

To build for the production/development environment you can run:


mvn -Pproduction package
mvn -Pdevelopment package

15
Maven Properties

• A property is always surrounded by ${ and }.

• There are some implicit properties available in any Maven project, these implicit properties are:

- project.*: Maven Project Object Model (POM). You can use the project.* prefix to reference values in a
Maven POM.

- settings.*: Maven Settings. You use the settings.* prefix to reference values from your Maven Settings in
~/.m2/settings.xml.

- env.*: Environment variables like PATH and M2_HOME can be referenced using the env.* prefix.

- System Properties: Any property which can be retrieved from the System.getProperty() method can be
referenced as a Maven property.

16
Maven Properties contd

• User-defined Properties:

- In addition to the implicit properties provided by the POM, Maven Settings, environment variables, and the Java System
properties, you have the ability to define your own arbitrary properties. Properties can be defined in a POM or in a
Profile.

- User-defined Properties in a Profile in a POM


<project>
...
<profiles>
<profile>
<id>some-profile</id>
<properties>
<arbitrary.property>This is some text</arbitrary.property>
</properties>
</profile>
</profiles>
...
</project>

17
Resource Filtering

• We will take the following example to understand this:

• A project contains the following resource file, myfile.txt, which contains a reference to the project.artifactId property.

• src/main/resources/myfile.txt

• This is a sample resource file

• My artifact id: ${project.artifactId}

• By default, at build time maven doesn't resolve the values inside ${}.

• In order to resolve such property values, we need to add ‘build.resources.resource.filtering’ to our pom.xml.

18
Resource Filtering contd

• Here is what pom.xml looks like

<project 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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.psl</groupId>
<artifactId>testSimpleJavaApp16May</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>testSimpleJavaApp16May</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
…………more dependencies…………
</dependencies>

<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>
19
Resource Filtering contd
• Now perform a "mvn clean process-resources" to process the resource files in the project. The "process-resources" is a phase in the maven default lifecycle.

• Console output:

[INFO] Scanning for projects...


[INFO]
[INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building testSimpleJavaApp16May 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ testSimpleJavaApp16May ---
[INFO] Deleting D:\STraining\MvnWorkspace2\testSimpleJavaApp16May\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ testSimpleJavaApp16May ---
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent!
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.154 s
[INFO] Finished at: 2017-05-22T13:48:18+05:30
[INFO] Final Memory: 7M/245M
[INFO] ------------------------------------------------------------------------

20
Resource Filtering contd

• After this maven command has completed, we can see that the myfile.txt file has been copied to target/classes

• And the content looks like:

target/classes/myfile.txt
This is a sample resource file

My artifact id: testSimpleJavaApp16May

• This means that it's possible to filter resource files to resolve property values by specifying build.resources.resource.filtering
to be 'true' in your pom.xml file

21
References
• Maven: The Complete Reference

• https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html

• https://developer.jboss.org/wiki/CreatingACustomLifecycleInMaven

• https://maven.apache.org/guides/introduction/introduction-to-profiles.html

• https://maven.apache.org/guides/mini/guide-site.html

• https://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html

• https://maven.apache.org/pom.html#Properties

• https://javabrains.io/courses/buildsys_mavenintro/

22
Thank you!
Persistent University

You might also like