新建普通maven工程
注意artifactId的命名为:xxx-maven-plugin, packaging为maven-plugin.
<groupId>org.example</groupId> <artifactId>my123-maven-plugin</artifactId> <version>1.0-SNAPSHOT</version> <packaging>maven-plugin</packaging>
引入依赖
<dependency> <groupId>org.apache.maven</groupId> <artifactId>maven-plugin-api</artifactId> <version>2.0</version> </dependency> <dependency> <groupId>org.apache.maven.plugin-tools</groupId> <artifactId>maven-plugin-annotations</artifactId> <version>3.2</version> </dependency>
创建自定义的Mojo类
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 HelloMojo extends AbstractMojo { @Parameter private String name; @Override public void execute() throws MojoExecutionException, MojoFailureException { getLog().info("hello, mojo."); System.out.println(name); } }
注意此处配置了参数name,后面可以看到具体的参数配置位置。
安装插件
maven install
新建maven工程并引入自定义插件
<build> <plugins> <plugin> <groupId>org.example</groupId> <artifactId>my123-maven-plugin</artifactId> <version>1.0-SNAPSHOT</version> <configuration> <name>alice</name> </configuration> </plugin> </plugins> </build>
配置了参数name
运行插件