
Apache Maven is a powerful tool for project management and build automation. When developing custom Maven plugins, Maven Plugin Annotations simplify and standardize how plugin metadata and configurations are defined. This guide provides a deep dive into what Maven Plugin Annotations are, how they work, and how you can get started using them.
🔍 What is Maven-Plugin-Annotations?
Maven Plugin Annotations are part of the org.apache.maven.plugin-tools suite, specifically the maven-plugin-annotations artifact. These annotations are used to simplify the creation of Maven plugins by replacing verbose and error-prone plugin descriptor XML (plugin.xml) files with clean, declarative Java annotations.
These annotations are processed during build time by the Maven Plugin Tools API, which automatically generates the necessary plugin descriptor (META-INF/maven/plugin.xml) for your plugin.
Key Annotations:
@Mojo: Declares a goal (likecompile,clean) of a Maven plugin.@Parameter: Defines configuration parameters for the Mojo.@Component: Injects shared Maven components.@Execute: Controls plugin execution phase or goal dependency.
💡 Major Use Cases of Maven Plugin Annotations
1. Developing Custom Maven Plugins
Annotations provide a cleaner and faster way to define plugin goals and parameters without manually writing XML descriptors.
2. Automating Build Tasks
You can create plugins for code generation, packaging, compilation steps, versioning, testing, or deployment.
3. Reusable DevOps Tools
Create reusable internal tools or DevOps pipelines using custom Maven plugins.
4. Simplified Metadata Configuration
Replace verbose XML with readable, maintainable Java code, improving team productivity.
5. Enhanced Integration
Annotations allow smooth integration with Maven’s lifecycle and improve compatibility with Maven’s Plugin Descriptor Generator.
🛠 How Maven Plugin Annotations Work (with Architecture)

Maven Plugin Annotations work as a bridge between Java code and Maven’s plugin system. Here’s a high-level overview of how they work:
🏗 Architecture Overview:
- Java Mojo with Annotations
You define your plugin logic in a Java class and annotate it with@Mojo,@Parameter, etc. - Annotation Processing via Plugin Tools
During build time,maven-plugin-pluginuses themaven-plugin-annotationsand scans the annotations in your code. - Descriptor Generation
Aplugin.xmlfile is automatically created inMETA-INF/maven/, defining the plugin’s goals, configuration, and dependencies. - Plugin Deployment
You install/deploy the plugin to a repository and invoke it using standard Maven commands. - Plugin Execution
When the plugin is used in another project, Maven invokes the annotated logic via the standard Mojo interface.
☑ Example:
@Mojo(name = "greet", defaultPhase = LifecyclePhase.PACKAGE)
public class GreetMojo extends AbstractMojo {
@Parameter(property = "greet.name", defaultValue = "Developer")
private String name;
public void execute() {
getLog().info("Hello, " + name + "!");
}
}
This plugin prints a greeting during the package phase.
🔄 Basic Workflow of Maven Plugin Annotations
- Define a Mojo class extending
AbstractMojo. - Use
@Mojoto register the goal name. - Declare inputs using
@Parameter. - Optionally use
@Componentto inject Maven internals (e.g.,MavenProject). - Build using
maven-plugin-pluginto generateplugin.xml. - Install or deploy the plugin to your Maven repository.
- Use the plugin in other projects via
pom.xml.
🚀 Step-by-Step Getting Started Guide
Step 1: Setup Your Project
Create a new Maven project for the plugin:
mvn archetype:generate
-DgroupId=com.example.plugins
-DartifactId=custom-maven-plugin
-DarchetypeArtifactId=maven-archetype-plugin
Step 2: Add Required Dependencies
In pom.xml, include:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.9.0</version>
<executions>
<execution>
<goals>
<goal>descriptor</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.9.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
Step 3: Create Your Plugin Logic
@Mojo(name = "greet", defaultPhase = LifecyclePhase.INSTALL)
public class GreetMojo extends AbstractMojo {
@Parameter(property = "greet.name", defaultValue = "World")
private String name;
public void execute() throws MojoExecutionException {
getLog().info("Greetings, " + name + "!");
}
}
Step 4: Build the Plugin
Run:
mvn clean install
This will generate the plugin.xml and install the plugin in your local repo.
Step 5: Use the Plugin
In another project’s pom.xml:
<build>
<plugins>
<plugin>
<groupId>com.example.plugins</groupId>
<artifactId>custom-maven-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<executions>
<execution>
<goals>
<goal>greet</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Run the plugin:
mvn com.example.plugins:custom-maven-plugin:1.0-SNAPSHOT:greet -Dgreet.name="Sneha"