Professional Documents
Culture Documents
Maven
Persistent University
Module 5: Custom Builds
Key learning points
• Filtering Resources
- 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
- 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.
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:
- 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
9
Creating Project 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
13
Generating JavaDoc
14
Maven Profiles
• 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:
15
Maven Properties
• 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.
17
Resource Filtering
• A project contains the following resource file, myfile.txt, which contains a reference to the project.artifactId property.
• src/main/resources/myfile.txt
• 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
<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:
20
Resource Filtering contd
• After this maven command has completed, we can see that the myfile.txt file has been copied to target/classes
target/classes/myfile.txt
This is a sample resource file
• 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