SoFunction
Updated on 2025-05-17

Detailed steps for Maven plugin

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-archetypeQuickly 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.
  • inheritAbstractMojoAnd 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,nameSpecify the Goal name.
  • @Parameter: Declare configurable parameters,propertySupport command line argument transmission (such as-Dname=Alice),defaultValueSet 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:supportListMapwait.

@Parameter
private List<String> messages;
@Parameter
private Map<String, String> properties;

Dependency injection

pass@ComponentInject Maven context object:

@Component
private MavenProject project; // Get project informationpublic void execute() {
    getLog().info("Project Artifact: " + ());
}

Lifecycle binding

existexecutionSpecified in thephase(likecompilepackage), the plug-in Goal will be automatically executed in the corresponding stage.

7. Debugging and testing

  • Log output:usegetLog().debug()or-XParameters enable Maven debug log.
  • Unit Testing:usemaven-plugin-testing-harnessPerform a Mojo test.
  • Integration Testing:existsrc/itWrite integrated test cases in the directory and usemaven-invoker-pluginimplement.

8. Generate a document

usemaven-plugin-pluginGenerate 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:examinegoalPrefixWhether the configuration and plug-in are installed correctly.
  • Parameter not injected:confirm@ParameterThe annotation is correct and the field is notfinal
  • Dependency conflict:usemvn dependency:treeTroubleshoot 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!