当前位置: 首页 > news >正文

maven之自定义插件

写在前面

在使用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进阶——开发自定义插件 。

相关文章:

  • 【清华大学第七版】DeepSeek赋能家庭教育的实操案例(批改作文+辅助语文/数学/科学学习+制定学习计划)
  • 黑盒测试VS白盒测试
  • 30天学习Java第五天——数组 字符串
  • 探索Trae:Cursor的完美替代,Claude-3.5-Sonnet与GPT-4o免费体验
  • SpringBoot3集成RocketMQ阿里巴巴消息队列中间件实现消息发送与接收详细教程
  • 人工智能之数学基础:线性变换的象空间和零空间
  • 富文本编辑器(Rich Text Editor,RTE)
  • 自动化APP测试APPium的元素等待
  • C++:类和对象(从底层编译开始)详解[前篇]
  • 【NVIC】
  • import ast与import json的区别
  • 电机控制常见面试问题(十二)
  • Wiform基础知识21-40
  • wepy微信小程序自定义底部弹出框功能,显示与隐藏效果(淡入淡出,滑入滑出)
  • 【sql靶场】第11、12关-post提交注入
  • 《突破GitHub网路访问困境:揭秘与应对》
  • 基于大模型的上睑下垂手术全流程预测与方案优化研究报告
  • K8S学习之基础三十:k8s的资源访问方式
  • 博通免费版(winlinux) VMware Workstation Pro 17 安装包分享
  • ORACLE 19.8版本遭遇ORA-600 [kqrHashTableRemove: X lock].宕机的问题分析
  • 简阳建设厅官方网站/北京seo公司助力网络营销
  • 做网站建设的技巧/seo网络排名优化技巧
  • 个人网站,可以做淘宝客吗/威海seo
  • 东港网站建设/百度指数的搜索指数
  • 建设银行网站怎么修改手机号码吗/电脑清理优化大师
  • 呼和浩特制作网站/best网络推广平台