How to code a maven plug in that incorporates maven clean and all the phases like…
- User
How to code a maven plug in that incorporates maven clean and all the phases like validate, compile, verify ...
- Assistant
How to code a maven plug in that incorporates maven clean and all the phases like validate, compile, verify ...
Creating a Maven plugin that integrates with various Maven lifecycle phases—including clean, validate, compile, verify, and others—allows you to execute custom actions at specific points in the build process. Below is a step-by-step guide to help you create such a Maven plugin.
pom.xmlLet's delve into each step in detail.
First, create a new Maven project that will serve as your plugin.
You can use the Maven Plugin Archetype to generate the basic structure:
mvn archetype:generate \
-DgroupId=com.example.maven.plugins \
-DartifactId=my-lifecycle-plugin \
-DarchetypeArtifactId=maven-archetype-plugin \
-DinteractiveMode=false
This command creates a project with the necessary structure for a Maven plugin.
The basic structure will look like this:
my-lifecycle-plugin/
├── pom.xml
└── src
└── main
└── java
└── com
└── example
└── maven
└── plugins
└── MyLifecycleMojo.java
Maven plugins consist of Mojo (Maven Old Java Object) classes, each corresponding to a specific goal. You can create separate Mojo classes for different lifecycle phases or combine them into a single Mojo.
For clarity, we'll create separate Mojos for different phases.
my-clean Goal for the Clean PhaseCreate a class CleanMojo.java:
package com.example.maven.plugins;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.*;
@Mojo(name = "my-clean", defaultPhase = LifecyclePhase.CLEAN)
public class CleanMojo extends AbstractMojo {
public void execute() throws MojoExecutionException {
getLog().info("Executing custom clean actions...");
// Implement your custom clean logic here
}
}
my-validate Goal for the Validate PhaseCreate a class ValidateMojo.java:
package com.example.maven.plugins;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.*;
@Mojo(name = "my-validate", defaultPhase = LifecyclePhase.VALIDATE)
public class ValidateMojo extends AbstractMojo {
public void execute() throws MojoExecutionException {
getLog().info("Executing custom validate actions...");
// Implement your custom validate logic here
}
}
Repeat the above pattern for other lifecycle phases like compile, test, package, verify, install, and deploy. Here's an example for the compile phase:
package com.example.maven.plugins;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.*;
@Mojo(name = "my-compile", defaultPhase = LifecyclePhase.COMPILE)
public class CompileMojo extends AbstractMojo {
public void execute() throws MojoExecutionException {
getLog().info("Executing custom compile actions...");
// Implement your custom compile logic here
}
}
Note: Each Mojo should have a unique name (my-clean, my-validate, etc.) and be bound to the appropriate defaultPhase.
pom.xmlYour pom.xml needs to define the plugin details, including packaging type, dependencies, and plugin annotations.
Here's an example 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
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.maven.plugins</groupId>
<artifactId>my-lifecycle-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>maven-plugin</packaging>
<name>My Lifecycle Maven Plugin</name>
<description>A Maven plugin that integrates with all standard lifecycle phases.</description>
<build>
<plugins>
<plugin>
<!-- Necessary for Maven to recognize this as a plugin -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.6.0</version>
<executions>
<execution>
<id>default-descriptor</id>
<goals>
<goal>descriptor</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<!-- Dependencies for writing Maven plugins -->
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>3.8.7</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.6.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
Key Points:
maven-plugin.maven-plugin-api and maven-plugin-annotations.Once you've implemented all necessary Mojos and configured the pom.xml, build and install the plugin to your local Maven repository.
mvn clean install
This command will compile your plugin, package it, and install it to your local Maven repository (~/.m2/repository by default).
Now that the plugin is installed locally, you can use it in other Maven projects by adding it to the project's pom.xml.
Suppose you have a Maven project where you want to use your custom lifecycle plugin. Add the plugin to the build section:
<project>
...
<build>
<plugins>
<plugin>
<groupId>com.example.maven.plugins</groupId>
<artifactId>my-lifecycle-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<executions>
<!-- Bind each goal to its respective phase -->
<execution>
<goals>
<goal>my-clean</goal>
</goals>
</execution>
<execution>
<goals>
<goal>my-validate</goal>
</goals>
</execution>
<execution>
<goals>
<goal>my-compile</goal>
</goals>
</execution>
<!-- Add executions for other phases similarly -->
</executions>
</plugin>
</plugins>
</build>
...
</project>
Explanation:
groupId, artifactId, and version of your custom plugin.<execution> blocks for each Mojo you want to bind to a lifecycle phase. Since each Mojo already has a defaultPhase, explicit binding is optional unless you need to customize it.Execute a standard Maven build lifecycle command:
mvn clean install
Your custom plugin's Mojos will be executed at their respective phases:
my-clean Mojo runs.my-validate Mojo runs.my-compile Mojo runs.Sample Output:
[INFO] --- my-lifecycle-plugin:1.0-SNAPSHOT:my-clean (default-cli) @ your-project ---
[INFO] Executing custom clean actions...
...
[INFO] --- my-lifecycle-plugin:1.0-SNAPSHOT:my-validate (default-cli) @ your-project ---
[INFO] Executing custom validate actions...
...
[INFO] --- my-lifecycle-plugin:1.0-SNAPSHOT:my-compile (default-cli) @ your-project ---
[INFO] Executing custom compile actions...
...
Separate Lifecycles: The clean lifecycle is separate from the default lifecycle (validate, compile, test, package, verify, install, deploy). Ensure you configure executions appropriately if you want your plugin to integrate with both.
Phase Binding Flexibility: While defaultPhase in the Mojo annotation binds the goal to a phase, you can override or add additional bindings in the consuming project's pom.xml.
To make your plugin more flexible, you can define parameters that can be configured in the pom.xml.
@Parameter(property = "myParam", defaultValue = "defaultValue")
private String myParam;
public void execute() throws MojoExecutionException {
getLog().info("Parameter value: " + myParam);
}
Once satisfied, you can publish your plugin to a remote repository (like Maven Central or a private Nexus/Artifactory repository) to make it available for other projects.
Creating a Maven plugin that integrates with various lifecycle phases involves:
pom.xml with necessary dependencies and plugin descriptors.pom.xml.By following these steps, you can customize and extend Maven's build process to fit your specific needs.
Not yet included in a published release.