简介

Mojo:Maven plain Old Java Object。每一个 Mojo 就是 Maven 中的一个执行目标(executable goal),而插件则是对单个或多个相关的 Mojo 做统一分发。

一个 Mojo 包含一个简单的 Java 类。插件中多个类似 Mojo 的通用之处可以使用抽象父类来封装。Maven插件项目的打包方式packaging必须为maven-plugin

第一个插件

依赖

<?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">
    <parent>
        <artifactId>samples-maven</artifactId>
        <groupId>com.unclezs</groupId>
        <version>1.0</version>
    </parent>
    <packaging>maven-plugin</packaging>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>hello-maven-plugin</artifactId>
    <name>hello-maven-plugin</name>
    <description>我的第一maven插件</description>
    <properties>
        <custom.plugin.version>3.6.0</custom.plugin.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-plugin-api</artifactId>
            <version>${custom.plugin.version}</version>
            <exclusions>
                <exclusion>
                    <artifactId>plexus-utils</artifactId>
                    <groupId>org.codehaus.plexus</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.plugin-tools</groupId>
            <artifactId>maven-plugin-annotations</artifactId>
            <version>${custom.plugin.version}</version>
            <exclusions>
                <exclusion>
                    <artifactId>maven-artifact</artifactId>
                    <groupId>org.apache.maven</groupId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-plugin-plugin</artifactId>
                <version>${custom.plugin.version}</version>
                <!--自定义插件前缀 mvn xxx:mojoName,默认为xxx-maven-plugin的xxx-->
                <configuration>
                    <goalPrefix>hello</goalPrefix>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

编写mojo

/**
 * @author blog.unclezs.com
 * @date 2020/12/6 2:51 上午
 */
@Mojo(name = "custom")
public class CustomPlugin extends AbstractMojo {
    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {
        getLog().info("hello maven plugin!!!");
    }
}

安装到本地仓库

maven clean install -U

测试插件

再另个一项目下引入

<build>
    <plugins>
        <plugin>
            <groupId>com.unclezs</groupId>
            <artifactId>hello-maven-plugin</artifactId>
            <version>1.0</version>
            <!--这个如果加上mvn compile的时候自动执行-->
            <executions>
              <execution>
                <phase>compile</phase>
                <goals>
                  <goal>custom</goal>
                </goals>
              </execution>
            </executions>
        </plugin>
    </plugins>
</build>

然后执行

mvn hello:custom
mvn com.unclezs:hello-maven-plugin:1.0:custom
mvn groupId:artifactId:version:goal

参数传递

package com.unclezs;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import java.io.File;
import java.net.URL;
import java.util.*;

/**
 * @author blog.unclezs.com
 * @date 2020/12/6 2:51 上午
 */
@Mojo(name = "param")
public class ParameterPlugin extends AbstractMojo {
    @Parameter(property = "param.name", defaultValue = "${project.artifactId}")
    private String name;
    @Parameter
    private Integer number;
    @Parameter
    private Boolean bool;
    @Parameter
    private Double doubleValue;
    @Parameter
    private Date date;
    @Parameter
    private File file;
    @Parameter
    private URL url;
    @Parameter
    private Color color;
    @Parameter
    private String[] array;
    @Parameter
    private List<String> list;
    @Parameter
    private Map<String, String> map;
    @Parameter
    private Properties properties;
    @Parameter
    private User obj;

    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {
        getLog().info("String:"+name);
        getLog().info("Integer:" + number);
        getLog().info("Boolean:" + bool);
        getLog().info("Double:" + doubleValue);
        getLog().info("Date:" + date);
        getLog().info("File:" + file.getName());
        getLog().info("URL:" + url);
        getLog().info("enum:" + color);
        getLog().info("array:" + Arrays.toString(array));
        getLog().info("List:" + list);
        getLog().info("Map:" + map);
        getLog().info("Properties:" + properties);
        getLog().info("obj:" + obj);

    }

    public enum Color {
        GREEN,
        RED,
        BLUE
    }
}

对应的引用时候传值

<build>
    <plugins>
        <plugin>
            <groupId>com.unclezs</groupId>
            <artifactId>hello-maven-plugin</artifactId>
            <version>1.0</version>
            <configuration>
                <name>unclezs</name>
                <number>123</number>
                <bool>true</bool>
                <doubleValue>1.0</doubleValue>
                <date>2005-10-06 2:22:55.1 PM</date>
                <file>${project.basedir}/pom.xml</file>
                <url>https://blog.unclezs.com</url>
                <color>GREEN</color>
                <array>
                    <param>value1</param>
                    <param>value2</param>
                </array>
                <list>
                    <param>value1</param>
                    <param>value2</param>
                </list>
                <map>
                    <key1>value1</key1>
                    <key2>value2</key2>
                </map>
                <properties>
                    <property>
                        <name>propertyName1</name>
                        <value>propertyValue1</value>
                    </property>
                    <property>
                        <name>propertyName2</name>
                        <value>propertyValue2</value>
                    </property>
                </properties>
                <obj>
                    <age>12</age>
                    <name>uncle</name>
                </obj>
            </configuration>
            <!--这个如果加上mvn compile的时候自动执行-->
            <executions>
                <execution>
                    <phase>compile</phase>
                    <goals>
                        <goal>custom</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

测试输出

更多细节查看plugin-parameters

相关链接

评论

博客
分类
标签
归档
关于