Software DevelopmentLearn the Uses of Different Maven Plugins

Learn the Uses of Different Maven Plugins

In the last article, we discussed the use and significance of various Maven repositories. In this article, we are going to discuss the use of various Maven Plug-ins. Maven can be considered as a plugin execution framework in which each and every task is conducted through Plug-ins. Maven Plug-ins are used to perform the following type of tasks.

• JAR file creation.
• WAR file creation.
• Compilation of code files.
• Unit testing.
• Project document creation.
• Project reports creation.

A plugin in Maven generally delivers a set of goals that can be performed by using the following syntax.

mvn [plugin-name]:[goal-name]

As an example, we can compile a Maven project with the maven-compiler-plugin just by initiating a compile-goal after execution of the following command.

mvn compile : compile

Types of Maven Plug-ins
The following type of Plug-ins are present in Maven.

S No.

Plugin Type

Description

1.

Build plugins

Build Plug-ins are used for the execution during the build process. They are configured in the <build/> element of pom.xml file.

2.

Reporting plugins

Reporting Plug-ins are used for execution during the site generation process and they are configured in the <reporting/> element of the pom.xml file.

Frequently used common Plug-ins of Maven.

S No.

Plug-in

Description

1.

clean

This Plug-in is used to clean up target after the build. It is used to completely delete the target directory.

2.

compiler

This Plug-in is used to compile Java source files.

3.

surefire

This Plug-in is used to run the JUnit unit tests. It is used to create test reports.

4.

jar

This Plug-in is used to build a JAR file from the current project.

5.

war

This Plug-in is used to build a WAR file from the current project.

6.

javadoc

This Plug-in is used to generate Javadoc for the project.

7.

antrun

This Plug-in is used to run a set of ant tasks from any phase which we have mentioned for the build.

Example
In the following example, we have used the maven-antrun-plugin in order to print data on console for various Maven profiles. Here the phase for each of profile is ‘clean’ and goal is ‘run’. The following is the content of the POM.xml file.

<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.eduonix</groupId>
	<artifactId>build.profile</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>build.profile</name>
	<url>http://maven.apache.org</url>

	<build>
		<filters>
			<filter>src/main/resources/${env}.properties</filter>
		</filters>
		<defaultGoal>install</defaultGoal>
		<!-- Filter other resources to the java directory -->
		<resources>
			<resource>
				<directory>src/main/resources</directory>
				<filtering>true</filtering>
				<includes>
					<include>*.properties</include>
					<include>*.xml</include>
				</includes>
			</resource>
		</resources>

		<finalName>project</finalName>
	</build>

	<properties>
		<!-- default environment -->
		<env>environment</env>
		<!-- Set default encoding to UTF-8 -->
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<!-- environment definitions -->
	<profiles>
		<profile>
			<id>sit</id>
			<properties>
				<env>environment.sit</env>
			</properties>
			<build>
				<plugins>
					<plugin>
						<groupId>org.apache.maven.plugins</groupId>
						<artifactId>maven-antrun-plugin</artifactId>
						<version>1.1</version>
						<executions>
							<execution>
								<phase>clean</phase>
								<goals>
									<goal>run</goal>
								</goals>
								<configuration>
									<tasks>
										<echo>Using environment.sit.properties</echo>
									</tasks>
								</configuration>
							</execution>
						</executions>
					</plugin>
				</plugins>
			</build>
		</profile>
		<profile>
			<id>uat</id>
			<properties>
				<env>environment.uat</env>
			</properties>
			<build>
				<plugins>
					<plugin>
						<groupId>org.apache.maven.plugins</groupId>
						<artifactId>maven-antrun-plugin</artifactId>
						<version>1.1</version>
						<executions>
							<execution>
								<phase>clean</phase>
								<goals>
									<goal>run</goal>
								</goals>
								<configuration>
									<tasks>
										<echo>Using environment.uat.properties</echo>
									</tasks>
								</configuration>
							</execution>
						</executions>
					</plugin>
				</plugins>
			</build>
		</profile>
		<profile>
			<id>prod</id>
			<properties>
				<env>environment.prod</env>
			</properties>
			<build>
				<plugins>
					<plugin>
						<groupId>org.apache.maven.plugins</groupId>
						<artifactId>maven-antrun-plugin</artifactId>
						<version>1.1</version>
						<executions>
							<execution>
								<phase>clean</phase>
								<goals>
									<goal>run</goal>
								</goals>
								<configuration>
									<tasks>
										<echo>Using environment.prod.properties</echo>
									</tasks>
								</configuration>
							</execution>
						</executions>
					</plugin>
				</plugins>
			</build>
		</profile>
	</profiles>

	<!-- Create a local repo so we can distribute jars without using the system 
		scope -->
	<!-- http://blog.dub.podval.org/2010/01/maven-in-project-repository.html -->
	<repositories>
		<repository>
			<id>lib</id>
			<name>lib</name>
			<releases>
				<enabled>true</enabled>
				<checksumPolicy>ignore</checksumPolicy>
			</releases>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
			<url>file://${project.basedir}/lib</url>
		</repository>
	</repositories>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.16</version>
		</dependency>
	</dependencies>
</project>

When we execute any of the Maven profile say ‘prod’, we can observe that the Maven Plug-in will start processing and display the clean phase of the clean life cycle as shown below.
Maven profile say prod

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building build.profile 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ build.profile ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 4 resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ build.profile ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ build.profile ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ build.profile ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ build.profile ---
[INFO] Surefire report directory: C:\work\project\build.profile\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.eduonix.build.profile.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.016 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] 
[INFO] --- maven-antrun-plugin:1.1:run (default) @ build.profile ---
[INFO] Executing tasks
     [echo] Using environment.prod.properties
[INFO] Executed tasks
[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ build.profile ---
[INFO] Building jar: C:\work\project\build.profile\target\project.jar
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ build.profile ---
[INFO] Installing C:\work\project\build.profile\target\project.jar to C:\Users\Aparajita\.m2\repository\com\eduonix\build.profile\0.0.1-SNAPSHOT\build.profile-0.0.1-SNAPSHOT.jar
[INFO] Installing C:\work\project\build.profile\pom.xml to C:\Users\Aparajita\.m2\repository\com\eduonix\build.profile\0.0.1-SNAPSHOT\build.profile-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.377 s
[INFO] Finished at: 2017-09-03T20:44:50-04:00
[INFO] Final Memory: 11M/172M
[INFO] ------------------------------------------------------------------------

We can conclude the following points based on the above Maven Plug-ins.
• We can specify various Maven Plug-ins in pom.xml by using plugins element.
• Each of the specified plugin can have one or more goals.
• We can define the phase where these plug-ins can start processing by using their phase elements. Here we have used clean phase.
• We can configure tasks to echo particular message on the console by binding them to particular goals of plugin. Here, we have bound echo task to the run goal of maven-antrun-plugin.
• Maven has the capability to download the plugin from central or remote repository if it is not present in the local repository and can start its processing.

Source code for the various Maven Plug-ins

Conclusion: –
In this article, we discussed the different Maven plug-ins, along with a suitable example for ‘clean’ phase and goal as ‘run’.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -