Maven自定义插件的开发示例

1、创建maven项目,配置相关依赖pom.xml

注意 <packaging>maven-plugin</packaging>

<?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.wywtime.plugins</groupId>
    <artifactId>bsmn-maven-plugin</artifactId>
    <version>0.0.1</version>
    <packaging>maven-plugin</packaging>

    <dependencies>

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

        <dependency>
            <groupId>org.apache.maven.plugin-tools</groupId>
            <artifactId>maven-plugin-annotations</artifactId>
            <version>3.6.4</version>
            <scope>provided</scope>
        </dependency>

    </dependencies>

</project>

2、创建AbstractMojo实现类

package com.wywtime.plugin;


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;

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

    @Parameter
    private String basedir;

    @Parameter
    private String name;

    public void execute() throws MojoExecutionException, MojoFailureException {
        getLog().info("hello "+name+", this is bsmn's first plugin");
        getLog().info("current basedir is:"+basedir);
    }

}

3、安装插件

mvn install

发现通过idea工具install,与命令行有差异,是因为命令行用的是本地安装的maven

# 可通过如下指令来查看实际的配置
mvn help:effective-settings

4、修改settings.xml

  <pluginGroups>
	<pluginGroup>com.wywtime.plugins</pluginGroup>
  </pluginGroups>

5、在新项目中引入插件

修改pom.xml

  <build>
        <plugins>
            <plugin>
                <groupId>com.wywtime.plugins</groupId>
                <artifactId>bsmn-maven-plugin</artifactId>
                <version>0.0.1</version>
                <configuration>
                    <name>abc</name>
                    <basedir>${project.basedir}</basedir>
                </configuration>
            </plugin>
        </plugins>
    </build>

6、执行指令

mvn bsmn:hello

posted on 2022-06-24 22:23  白首码农  阅读(53)  评论(0编辑  收藏  举报