During the process of learning and understanding Nexus-public, I found that a large part of the Java project is deeply integrated with different functions in the form of Maven plug-ins. The advantage is that the functions that originally needed to be split into multiple projects can be processed centrally, making the project lighter and the integration of front and back ends more convenient. Write a Maveen plug-in development process for this, in case it is not needed from time to time.
Developing a Maven plugin involves multiple steps, and here is a detailed guide:
1. Create a Maven plugin project
usemaven-plugin-archetype
Quickly generate project skeletons:
mvn archetype:generate \ -DgroupId= \ -DartifactId=custom-maven-plugin \ -DarchetypeGroupId= \ -DarchetypeArtifactId=maven-plugin-archetype \ -DinteractiveMode=false
2. Plugin core: Mojo class
- Mojo(Maven Old Java Object)It is the execution unit of the plug-in, and each Goal corresponds to a Mojo class.
- inherit
AbstractMojo
And implementexecute()
Methods, logic is written here.
Sample Mojo class
package ; import ; import ; import ; import ; @Mojo(name = "greet") // Define Goal name and call it through mvn custom:greetpublic class GreetingMojo extends AbstractMojo { @Parameter(property = "name", defaultValue = "World") // Read parameters from the configuration or command line private String name; @Parameter(property = "verbose", defaultValue = "false") private boolean verbose; public void execute() throws MojoExecutionException { if (verbose) { getLog().info("Verbose mode enabled."); } getLog().info("Hello, " + name + "!"); } }
Key Notes
-
@Mojo
: The tag class is Mojo,name
Specify the Goal name. -
@Parameter
: Declare configurable parameters,property
Support command line argument transmission (such as-Dname=Alice
),defaultValue
Set the default value. -
getLog()
: Provides plug-in log output capability.
3. Configure plugin metadata
existBasic information and dependencies for configuring plugins in:
<project> <modelVersion>4.0.0</modelVersion> <groupId></groupId> <artifactId>custom-maven-plugin</artifactId> <version>1.0.0</version> <packaging>maven-plugin</packaging> <dependencies> <!-- MavenPluginAPIrely --> <dependency> <groupId></groupId> <artifactId>maven-plugin-api</artifactId> <version>3.8.6</version> </dependency> <!-- Annotation processing --> <dependency> <groupId>-tools</groupId> <artifactId>maven-plugin-annotations</artifactId> <version>3.6.4</version> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <!-- 生成Plugin描述符 --> <plugin> <groupId></groupId> <artifactId>maven-plugin-plugin</artifactId> <version>3.6.4</version> <configuration> <goalPrefix>custom</goalPrefix> <!-- Plugin前缀 --> </configuration> </plugin> </plugins> </build> </project>
4. Build and install plug-ins
mvn clean install
Install the plugin to the local Maven repository for use by other projects.
5. Use plug-ins
On the projectConfiguration in:
<build> <plugins> <plugin> <groupId></groupId> <artifactId>custom-maven-plugin</artifactId> <version>1.0.0</version> <configuration> <name>Alice</name> <verbose>true</verbose> </configuration> <executions> <execution> <phase>compile</phase> <!-- Bind tocompilestage --> <goals> <goal>greet</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
Direct command line call
mvn :custom-maven-plugin:1.0.0:greet -Dname=Alice # Or use prefix (requires correct goalPrefix configuration)mvn custom:greet -Dname=Alice
6. Advanced features
Parameter type support
Complex Types:supportList
、Map
wait.
@Parameter private List<String> messages; @Parameter private Map<String, String> properties;
Dependency injection
pass@Component
Inject Maven context object:
@Component private MavenProject project; // Get project informationpublic void execute() { getLog().info("Project Artifact: " + ()); }
Lifecycle binding
existexecution
Specified in thephase
(likecompile
、package
), the plug-in Goal will be automatically executed in the corresponding stage.
7. Debugging and testing
-
Log output:use
getLog().debug()
or-X
Parameters enable Maven debug log. -
Unit Testing:use
maven-plugin-testing-harness
Perform a Mojo test. -
Integration Testing:exist
src/it
Write integrated test cases in the directory and usemaven-invoker-plugin
implement.
8. Generate a document
usemaven-plugin-plugin
Generate plugin documentation:
mvn plugin:report
The generated target document is located attarget/site/
, describes the Goals and parameters of the plugin.
Frequently Asked Questions
-
Goal not found:examine
goalPrefix
Whether the configuration and plug-in are installed correctly. -
Parameter not injected:confirm
@Parameter
The annotation is correct and the field is notfinal
。 -
Dependency conflict:use
mvn dependency:tree
Troubleshoot dependencies.
Through the above steps, you can develop a fully functional Maven plug-in, flexibly expand the construction process, and adapt to the specific needs of the project.
This is the end of this article about the detailed steps of Maven plug-in. For more related Maven plug-in content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!