例子插件工程https://github.com/zhuquanwen/mvn-plugin.git
例子调用插件工程
-
创建一个Maven项目,具体怎么创建略。
-
将pom.xml文件的packaging 改为maven-plugin
<packaging>maven-plugin</packaging>
-
pom.xml中引入插件依赖
<dependencies> <dependency> <groupId>org.apache.maven.plugin-tools</groupId> <artifactId>maven-plugin-annotations</artifactId> <version>3.5.2</version> </dependency> <dependency> <groupId>org.apache.maven</groupId> <artifactId>maven-plugin-api</artifactId> <version>3.5.4</version> </dependency> </dependencies>
-
定义一个类继承
AbstractMojo
,实现excute
方法public class ZqwMojo extends AbstractMojo { public void execute() throws MojoExecutionException, MojoFailureException { System.out.println("我自定义的插件lalalalala!!!!!!!!!!!!!!!!"); } }
-
在上面的类上定义Mojo注解,name代表goal,defaultPhase是默认的phase
@Mojo(name = "zqw", defaultPhase = LifecyclePhase.PACKAGE)
-
运行
mvn clean install
将插件安装到本地仓库,当然也可以deploy到私服 -
新建一个模块或工程,pom.xml中引入刚才自定义的插件
<build> <plugins> <plugin> <groupId>com.zqw</groupId> <artifactId>maven-plugin-demo</artifactId> <version>1.0-SNAPSHOT</version> </plugin> </plugins> </build>
-
刷新maven或发现在新定义的模块或工程中,有了我们自定义的插件。
运行会发现有了我们刚才System.out.println的输出 -
现在这个简单的插件就算完成了,当运行demo:zqw时运行package 这个phase,并输出内容。但是当我们运行mvn package的时候是不会输出的。如果想让package的时候也行我们的插件,要配置如下:
<build> <plugins> <plugin> <groupId>com.zqw</groupId> <artifactId>maven-plugin-demo</artifactId> <version>1.0-SNAPSHOT</version> <executions> <execution> <phase>package</phase> <goals> <goal>zqw</goal> </goals> </execution> </executions> </plugin> </plugins> </build> ```
这样运行package就会输出我们的内容了
-
向插件中传入参数,在插件类处定义@Paramater
``` @Parameter private String msg; public void execute() throws MojoExecutionException, MojoFailureException { System.out.println(msg); System.out.println("共有Java文件数目:" + javaCount()); } ```
在调用处配置参数
``` <build> <plugins> <plugin> <groupId>com.zqw</groupId> <artifactId>mvn-plugin</artifactId> <version>1.0-SNAPSHOT</version> <configuration> <msg>lalalalalalala</msg> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>count</goal> </goals> </execution> </executions> </plugin> </plugins> </build> ```
-
插件中传入一个集合参数
在将@Parameter用集合接收后出现问题Execution default-descriptor of goal org.apache.maven.plugins:maven-plugin-plugin:3.2:descriptor failed: 52264
升级一下maven-plugin-plugin的版本就好了
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.5.2</version>
</plugin>
</plugins>
</build>
这样就可以定义List的参数了
```
@Parameter(property = "options")
private String[] options;
public void execute() throws MojoExecutionException, MojoFailureException {
System.out.println(msg);
if (options != null) Arrays.stream(options).forEach(System.out::println);
System.out.println("共有Java文件数目:" + javaCount());
}
```
调用处
<build>
<plugins>
<plugin>
<groupId>com.zqw</groupId>
<artifactId>mvn-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<configuration>
<msg>lalalalalalala</msg>
<options>
<option>111</option>
<option>222</option>
<option>333</option>
<option>444</option>
</options>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>count</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>