Maven - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Maven Plugins

Maven Plugins

shape Introduction

Maven doesn't know how to create JAR files and compile the code. It is responsible to create the XML files, manage phases and life-cycles. The present chapter 'Maven Plugins' explains about Goals, Properties, Phases and Profiles.

shape Description

Design functionalities of Maven can be done using Plug-ins, which are mostly retrieved from the Central repository. Simply put, plug-in is the heart of Maven. Plugins are categorized into two types:
  1. Reporting plug-ins
  2. Build plug-ins

shape Maven Plug-ins

Some of the important plug-ins are:
Plug-in Description
maven-compiler-plugin compiles Java sources
maven-resources-plugin Copies resources to the output directory
maven-surefire-plugin Used to run JUnit tests
maven-install-plugin Used to install build artifact into local repository
maven-deploy-plugin Used to deploy artifact to remote repository
maven-clean-plugin Used to clean after the build
maven-jar-plugin Used to build JAR from the current project
maven-war-plugin Used to build WAR from the current project
maven-archetype-plugin Generates a skeleton project structure from an archetype.

Goals

shape Description

Small divisions of Maven Plugins tasks are called as goals or task in plug-ins. Goals are also called as Mojo’s i.e., Maven Plain Old Java Project.

shape Syntax

Syntax for Maven goals is as follows: [xml] <plugin prefix>:<goal> [/xml]

shape Examples

Surefire Maven Plugins has two goals. Compile plug-in of Maven has three goals: [xml] $ mvn compiler:compile $ mvn compiler:testCompile $ mvn surefire:test[/xml]

Properties

shape Description

Properties can be specified in build files and command line. One can override them if required.

shape Examples

[xml] <properties> <skipTests>true</skipTests> <maven.build.timestamp.format>yyyyMMdd-HHmm</maven.build.timestamp.format> <buildTimestamp>${maven.build.timestamp}</buildTimestamp> <buildId>${buildType}${buildTimestamp}</buildId> </properties> [/xml]

Phases

shape Description

Normally, build life cycle will be done in phases. Maven executes all the existing phases in a sequence until it is instructed to execute a given phase. At the same time, all the related goals will be executed. Following are the phases in which validate, compile, test, package, verify, integration test, install and deploy are the important phases.

shape Phases

Phase Description
validate Validation of project is carried out and provides the information required to complete the build process.
generate-resources Source code generation to include while compiling.
process-sources Source code is processed.
generate-resources Resource generation to include in packages.
process-resources Resources packing and copying into destination directory and is ready for package.
compile Source code compilation
process-classes Generated files into post-process after compilation.
generate-test-sources Test source generation to include in compilation.
process-test-sources Processing the test source code.
generate-test-resources Creation of resources to test.
process-test-resources Copying and processing the resources to test the destination.
test-compile Compiling the test source code to test the destination.
test With unit testing framework, run the tests.
prepare-package Preparing the package by performing operations before actual packaging.
package Gather the compiled code and package in JAR,WAR or EAR.
pre-integration test The actions to be performed before integration tests are executed like setting the environment.
integration test Processing and deploying the package.
post-integration test Performing the action after the execution of integration tests like cleaning the environment.
verify Checking the packages are valid or not.
install Installing the package into local repository.
deploy Copying the package into remote repository.

shape Examples

Below example shows how to add goals to phases. [xml] <plugin> <groupId>com.splessons.example</groupId> <artifactId>splessons-some-maven-plugin</artifactId> <version>1.0</version> <executions> <execution> <phase>verify</phase> <goals> <goal>checklinks</goal> </goals> </execution> </executions> </plugin>[/xml]

Profiles

shape Description

Maven uses profiles to specify various configurations. Certain instructions can be given to use profiles. –p profilename command is used to get the profiles.

shape Examples

[xml] <profiles> <profile> <id>dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <db.location>URL_to_dev_system</db.location> <logo.image>companylogo.png</logo.image> </properties> </profile> <profile> <id>production</id> <properties> <db.location>URL_to_prod_system</db.location> <logo.image>companylogo2.png</logo.image> </properties> </profile> </profiles> [/xml]

Summary

shape Key Points

  • Maven Plugins help in Project designing functions.
  • Combining goals gives plug-ins.
  • Profile specifies configurations.