开发maven自定义插件简单示例,参考官方文档:https://maven.apache.org/guides/plugin/guide-java-plugin-development.html
插件开发
1、插件项目pom引入依赖
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>3.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.5</version>
<scope>provided</scope>
</dependency>
2、新建MyMojo
类,继承AbstractMojo
并实现抽象方法execute()
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import java.util.List;
/**
* @Class MyMojo
* @Description MyMojo
* @Author zeke
* @Version 1.0.0
**/
@Mojo(name = "myMojo", defaultPhase = LifecyclePhase.PACKAGE)
public class MyMojo extends AbstractMojo {
@Parameter
private String msg;
@Parameter
private List<String> options;
@Parameter(property = "args")
private String args;
// mvn install -Dargs=123
public void execute() throws MojoExecutionException, MojoFailureException {
System.out.println("myMojo plugin !!! msg = " + msg);
System.out.println("myMojo plugin !!! options = " + options);
System.out.println("myMojo plugin !!! args = " + args);
}
}
3、mvn clean install
插件到本地仓库。
插件使用
1、在项目pom
引入插件,并传递一些参数。
<build>
<plugins>
<plugin>
<groupId>cool.zeke</groupId>
<artifactId>my-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<configuration>
<msg>this is msg</msg>
<options>
<option>option1</option>
<option>option2</option>
<option>option3</option>
</options>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>myMojo</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
2、执行mvn package
,看到输出如下日志即可表示成功。
myMojo plugin !!! msg = this is msg
myMojo plugin !!! options = [option1, option2, option3]
myMojo plugin !!! args = null
[INFO] ------------------------------------------------------------------------