Tuesday, 15 March 2022

How to create custom maven plugin?

In this post, I am going to explain how to create custom maven plugin in Eclipse IDE.

 

Step 1: Create new maven project.

 Open Eclipse.

 

File -> New -> Maven Project

 


 

Select the checkbox ‘Create a simple project (skip archetype selection)’

 

 


Click on Next button.




Update

a.   Group Id as ‘com.sample.app’

b.   Artifact Id as ‘print-dependencies-maven-plugin’.

c.    Version as 1

 

Click on Finish button.

 

Step 2: Update pom.xml

 

Set the packaging type as ‘maven-plugin’.

<packaging>maven-plugin</packaging>

 

Add maven mojo dependencies to the pom.xml.

<dependencies>
	<dependency>
		<groupId>org.apache.maven</groupId>
		<artifactId>maven-plugin-api</artifactId>
		<version>${maven.plugin.api.version}</version>
	</dependency>
	<dependency>
		<groupId>org.apache.maven.plugin-tools</groupId>
		<artifactId>maven-plugin-annotations</artifactId>
		<version>${maven.plugin.anotations}</version>
		<scope>provided</scope>
	</dependency>
	<dependency>
		<groupId>org.apache.maven</groupId>
		<artifactId>maven-project</artifactId>
		<version>${maven.project.version}</version>
	</dependency>
</dependencies>

 

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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.sample.app</groupId>
	<artifactId>print-dependencies-maven-plugin</artifactId>
	<version>1</version>

	<packaging>maven-plugin</packaging>


	<properties>
		<maven.compiler.target>1.8</maven.compiler.target>
		<maven.compiler.source>1.8</maven.compiler.source>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.report.outputEncoding>UTF-8</project.report.outputEncoding>

		<maven.plugin.api.version>3.8.5</maven.plugin.api.version>
		<maven.plugin.anotations>3.6.4</maven.plugin.anotations>
		<maven.project.version>2.2.1</maven.project.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.apache.maven</groupId>
			<artifactId>maven-plugin-api</artifactId>
			<version>${maven.plugin.api.version}</version>
		</dependency>
		<dependency>
			<groupId>org.apache.maven.plugin-tools</groupId>
			<artifactId>maven-plugin-annotations</artifactId>
			<version>${maven.plugin.anotations}</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.maven</groupId>
			<artifactId>maven-project</artifactId>
			<version>${maven.project.version}</version>
		</dependency>
	</dependencies>

</project>

 

Step 3: Define plugin goals.

 

You can define a goal by implementing Mojo interface.

 

PrintDependencies.java

 

package com.sample.app.goals;

import java.util.List;

import org.apache.maven.model.Dependency;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;

@Mojo(name = "print-dependencies", defaultPhase = LifecyclePhase.COMPILE)
public class PrintDependencies extends AbstractMojo {

	@Parameter(defaultValue = "${project}", required = true, readonly = true)
	MavenProject project;

	@Override
	public void execute() throws MojoExecutionException, MojoFailureException {
		List<Dependency> dependencies = project.getDependencies();

		System.out.println("Executing the goal--->print-dependencies");
		System.out.println("_________________________________________");
		for (Dependency dependency : dependencies) {
			System.out.println("groupId : " + dependency.getGroupId());
			System.out.println("artifactId : " + dependency.getArtifactId());
			System.out.println("version : " + dependency.getVersion() + "\n");
		}

		System.out.println("_________________________________________");

	}

}

 

Navigate to the directory, where pom.xml file is located, execute the command ‘mvn clean install’. Plugin gets installed into your local repository on successful execution.

 

How to execute the custom plugin?

Syntax

mvn groupId:artifactId:version:goal

 

Example

mvn com.sample.app:print-dependencies-maven-plugin:1:print-dependencies

 

How to use this custom plugin in a maven project?

Create another maven project ‘plugin-usage’ and update pom.xml with custom plugin details

 

pom.xml

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.sample.app</groupId>
	<artifactId>plugin-usage</artifactId>
	<version>1</version>

	<dependencies>

		<dependency>
			<groupId>com.google.code.gson</groupId>
			<artifactId>gson</artifactId>
			<version>2.9.0</version>
		</dependency>

		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-classic</artifactId>
			<version>1.2.10</version>
		</dependency>


		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-core</artifactId>
			<version>1.2.10</version>
		</dependency>

		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.7.36</version>
		</dependency>


	</dependencies>


	<build>
		<plugins>
			<plugin>
				<groupId>com.sample.app</groupId>
				<artifactId>print-dependencies-maven-plugin</artifactId>
				<version>1</version>
				<executions>
					<execution>
						<goals>
							<goal>print-dependencies</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
</project>

 

Navigate to the project, where the pom.xml file is located, execute the command ‘mvn clean compile’. It print all the dependencies information.

$mvn clean compile
[INFO] Scanning for projects...
[INFO] 
[INFO] --------------------< com.sample.app:plugin-usage >---------------------
[INFO] Building plugin-usage 1
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ plugin-usage ---
[INFO] Deleting /Users/krishna/eclipse-workspace/plugin-usage/target
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ plugin-usage ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ plugin-usage ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- print-dependencies-maven-plugin:1:print-dependencies (default) @ plugin-usage ---
Executing the goal--->print-dependencies
_________________________________________
groupId : com.google.code.gson
artifactId : gson
version : 2.9.0

groupId : ch.qos.logback
artifactId : logback-classic
version : 1.2.10

groupId : ch.qos.logback
artifactId : logback-core
version : 1.2.10

groupId : org.slf4j
artifactId : slf4j-api
version : 1.7.36

_________________________________________
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.688 s
[INFO] Finished at: 2022-03-15T22:36:07+05:30
[INFO] ------------------------------------------------------------------------

 

You can download complete examples from this link.


 


 

Previous                                                    Next                                                    Home

No comments:

Post a Comment