自定义一个maven插件

准备

这是我参考的maven相关的一些官方资料,不关心的话,直接跳过看下面的示例。
资料如下:

  • Maven Plugin Plugin
    地址:https://maven.apache.org/plugin-tools/maven-plugin-plugin/
    我用它创建插件描述符用的
  • Maven Plugin API
    地址:http://maven.apache.org/ref/3.6.2/maven-plugin-api/index.html
    插件API,maven依赖:
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-plugin-api</artifactId>
            <version>3.6.2</version>
        </dependency>
  • Using Plugin Tools Java Annotations
    光上面的那个依赖可不够,还有用到的注解在这个依赖
        <dependency>
            <groupId>org.apache.maven.plugin-tools</groupId>
            <artifactId>maven-plugin-annotations</artifactId>
            <version>3.6.0</version>
            <optional>true
            </optional> <!-- annotations are not used at runtime because @Retention(value=CLASS), they are needed only to build the plugin -->
        </dependency>

使用说明:https://maven.apache.org/plugin-tools/maven-plugin-plugin/examples/using-annotations.html

  • 还有个东东
    这个开发的时候用不到,我只是看了这个插件的源码来参考的:
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>3.1.1</version>
        </dependency>

示例

  1. 新建个工程
    在这里插入图片描述
    工程结构就这样,写的示例就1个类,代码如下:
/**
 * @Auther: 许晓东
 * @Date: 19-10-13 22:40
 * @Description:
 */

//name字段指定插件目标,默认是在validate阶段执行,我用的maven3,pom中是支持覆盖的
@Mojo(name = "echo", defaultPhase = LifecyclePhase.VALIDATE)
public class HelloWorldMojo extends AbstractMojo {

    // 指定传入的参数,这个注解的具体用法可参看官方文档,参数通过-Dparam=hello,来注入赋值hello
    @Parameter(property = "param")
    private String param;

    // 插件执行时,执行这个方法
    public void execute() throws MojoExecutionException, MojoFailureException {
    	// 打印一行日志
        getLog().info("hello world, your param is " + param);
    }
}

pom.xml配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.xuxd.maven.plugins</groupId>
    <artifactId>echo-hello-world</artifactId>
    <version>1.0-SNAPSHOT</version>

    <packaging>maven-plugin</packaging>


    <dependencies>
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-plugin-api</artifactId>
            <version>3.6.2</version>
        </dependency>


        <dependency>
            <groupId>org.apache.maven.plugin-tools</groupId>
            <artifactId>maven-plugin-annotations</artifactId>
            <version>3.6.0</version>
            <optional>true
            </optional> <!-- annotations are not used at runtime because @Retention(value=CLASS), they are needed only to build the plugin -->
        </dependency>

    </dependencies>

    <build>
        <finalName>echo-hello-world-plugin</finalName>
        <plugins>
            <plugin>
                <!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-plugin-plugin -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-plugin-plugin</artifactId>
                <version>3.6.0</version>
                <configuration>
                    <!-- 插件描述符中目标前缀,更多配置参看官方文档-->
                    <goalPrefix>echo-hello-world</goalPrefix>
                    <!--如果想看下这个文件描述符,就把这个注释放开,让它创建到这个目录下 -->
                    <!--<outputDirectory>target/dir/META-INF/maven</outputDirectory>-->
                </configuration>
                <!--<executions>
                    <execution>
                        <id>default-descriptor</id>
                        <phase>process-classes</phase>
                    </execution>
                </executions>-->
            </plugin>
        </plugins>
    </build>
</project>
  1. 安装到本地仓库
mvn clean install
  1. 测试
    在另一个工程引入这个插件:
            <plugin>
                <groupId>com.xuxd.maven.plugins</groupId>
                <artifactId>echo-hello-world</artifactId>
                <version>1.0-SNAPSHOT</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>echo</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

测试一下:
在这里插入图片描述
因为默认是validate阶段,所以这个阶段及之后都会执行的。
传入参数试一下:
在这里插入图片描述
完成。
这只是个最基本的demo,更复杂的就根据自己需要开发了。

发布了136 篇原创文章 · 获赞 69 · 访问量 17万+

猜你喜欢

转载自blog.csdn.net/x763795151/article/details/102540105