maven之自定义插件

发布于:2025-03-18 ⋅ 阅读:(21) ⋅ 点赞:(0)

写在前面

在使用maven肯定是离不开插件的,比如执行mvn clean或者时mvn compile其实运行的就是绑定的默认插件。虽然我们一般不需要来自定义插件,但是为了使用的过程中更加的清晰,来尝试自定义插件还是很有必要的,所以本文就一起来看下这部分内容。

1:hello world

想要定义插件,我们首先需要创建一个普通的maven项目,接着在pom中引入定义插件需要的依赖:

 <!--这个依赖引入了插件开发需要的相关基础类-->
<dependency>
    <groupId>org.apache.maven</groupId>
    <artifactId>maven-plugin-api</artifactId>
    <version>3.0</version>
</dependency>
 <!--这个依赖引入了插件开发需要的相关注解-->
<dependency>
    <groupId>org.apache.maven.plugin-tools</groupId>
    <artifactId>maven-plugin-annotations</artifactId>
    <version>3.4</version>
    <scope>provided</scope>
</dependency>

接着我们来定义插件类:

@Mojo(name = "TimerPlugin")
public class TimerPlugin extends AbstractMojo {

    public void execute() throws MojoExecutionException, MojoFailureException {
        String currentTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        getLog().info("timer plugin is running,current time is " + currentTime);
    }
}

抽象类AbstractMojo实现了接口org.apache.maven.plugin.Mojo,主要定义了逻辑执行方法execute,日志设置相关方法,其中execute是来写具体插件逻辑的。接着执行mvn clean install把插件安装到本地仓库,成功后就可以在仓库中看到我们的插件jar了:
在这里插入图片描述
接着就可以来使用了,两种方式,第一种是直接使用,第二种是绑定到maven的生命周期中。

1.1:直接使用

首先创建一个maven项目,然后执行mvn 插件groupId:插件artifactId[:插件版本]:插件目标名称:

PS D:\tmp\maven-plugin-demo> mvn org.example:maven-plugin-demo:TimerPlugin
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------< org.example:maven-plugin-demo >--------------------
[INFO] Building maven-plugin-demo 1.0-SNAPSHOT
[INFO] ----------------------------[ maven-plugin ]----------------------------
[INFO]
[INFO] --- maven-plugin-demo:1.0-SNAPSHOT:TimerPlugin (default-cli) @ maven-plugin-demo ---
[INFO] timer plugin is running,current time is 2025-03-14
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.456 s
[INFO] Finished at: 2025-03-14T16:55:27+08:00
[INFO] ------------------------------------------------------------------------

timer plugin is running,current time is 2025-03-14就是我们插件的输出了。

1.2:绑定到生命周期

需要在plugins标签中定义:

<build>
    <plugins>
        <plugin>
            <groupId>org.example</groupId>
            <artifactId>maven-plugin-demo</artifactId>
            <version>1.0-SNAPSHOT</version>
            <executions>
                <execution>
                    <id>test1111</id>
                    <phase>compile</phase>
                    <goals>
                        <goal>TimerPlugin</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

其中phase定义了要绑定的生命周期,goal是要执行的逻辑,即在@Mojo中定义的信息,定义后我们就可以在idea的maven面板中看到相关的插件信息了:
在这里插入图片描述
当我们执行compile时该插件就会执行了:
在这里插入图片描述

2:带参数的插件

需要使用到注解@Parameter,定义如下类:

@Mojo(name = "TimerPlugin")
public class TimerPlugin extends AbstractMojo {

    @Parameter(property = "timer.username" ,defaultValue = "moutory")
    private String userName;

    @Parameter(property = "timer.status", defaultValue = "happy")
    private String status;

    public void execute() throws MojoExecutionException, MojoFailureException {
        String currentTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        getLog().info("timer plugin is running, current time is " + currentTime);
        getLog().info(String.format("hi %s ! Now you are %s",userName,status));
    }
}

安装插件后使用。
首先看命令行直接使用:

D:\tmp\maven-plugin-demo>mvn org.example:maven-plugin-demo:TimerPlugin -Dtimer.username=xxxxx -Dtimer.status=尽量保持平静
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------< org.example:maven-plugin-demo >--------------------
[INFO] Building maven-plugin-demo 1.0-SNAPSHOT
[INFO] ----------------------------[ maven-plugin ]----------------------------
[INFO]
[INFO] --- maven-plugin-demo:1.0-SNAPSHOT:TimerPlugin (default-cli) @ maven-plugin-demo ---
[INFO] timer plugin is running, current time is 2025-03-14
[INFO] hi xxxxx ! Now you are 尽量保持平静
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.473 s
[INFO] Finished at: 2025-03-14T17:37:16+08:00
[INFO] ------------------------------------------------------------------------

通过-D来传递参数。再来看下绑定到生命周期使用:

<build>
    <plugins>
        <plugin>
            <groupId>org.example</groupId>
            <artifactId>maven-plugin-demo</artifactId>
            <version>1.0-SNAPSHOT</version>
            <executions>
                <execution>
                    <id>test</id>
                    <phase>compile</phase>
                    <goals>
                        <goal>TimerPlugin</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <userName>Jim</userName>
                <status>good</status>
            </configuration>
        </plugin>
    </plugins>
</build>

绑定到compile阶段,所以我们可以指定compile来看下效果:

PS D:\tmp\untitled1111> mvn compile
[INFO] Scanning for projects...
[INFO] 
[INFO] ----------------------< org.example:untitled1111 >----------------------
[INFO] Building untitled1111 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ untitled1111 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ untitled1111 ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-plugin-demo:1.0-SNAPSHOT:TimerPlugin (test) @ untitled1111 ---
[INFO] timer plugin is running, current time is 2025-03-14
[INFO] hi Jim ! Now you are good
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.245 s
[INFO] Finished at: 2025-03-14T17:40:08+08:00
[INFO] ------------------------------------------------------------------------

写在后面

参考文章列表

maven进阶——开发自定义插件


网站公告

今日签到

点亮在社区的每一天
去签到