maven 自定义插件
插件快速入门
新建一个maven项目
选择maven-archetype-quickstart模板
更改package类型
<packaging>maven-plugin</packaging>
ps:项目出现红叉报错不管
引入依赖
<dependencies>
<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>
</dependencies>
我的pom.xml(参考使用,请忽略)
<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>priv.dengjl</groupId>
<artifactId>test-maven-plugin</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>maven-plugin</packaging>
<name>test-maven-plugin</name>
<dependencies>
<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>
</dependencies>
</project>
编写goal实现
这里声明使用阶段为install
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;
@Mojo(name="print", defaultPhase=LifecyclePhase.INSTALL)
public class MyFirstPlugin extends AbstractMojo {
public void execute() throws MojoExecutionException, MojoFailureException {
getLog().info( "Hello, plugin" );
}
}
将插件打包安装到本地仓库
命令
mvn clean install
打包成功后,本地仓库有对应的jar文件
测试插件
新增另外一个maven工程
配置自定义的插件
<build>
<plugins>
<plugin>
<groupId>priv.dengjl</groupId>
<artifactId>test-maven-plugin</artifactId>
<version>0.0.1-SNAPSHOT</version>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>print</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
运行命令
install阶段执行自定义插件
mvn install
插件参数
新增插件实现
clean阶段执行
import java.util.List;
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;
@Mojo(name="paramPrint", defaultPhase=LifecyclePhase.CLEAN)
public class ParameterPlugin extends AbstractMojo {
@Parameter
private String message;
@Parameter
private List<String> manyParams;
public void execute() throws MojoExecutionException, MojoFailureException {
getLog().info( message);
if (manyParams != null) {
for (String string : manyParams) {
getLog().info( string);
}
}
}
}
使用插件配置
<plugin>
<groupId>priv.dengjl</groupId>
<artifactId>test-maven-plugin</artifactId>
<version>0.0.1-SNAPSHOT</version>
<configuration>
<message>this is my test plugin message@</message>
<manyParams>
<manyParam>2345</manyParam>
<manyParam>ertyu</manyParam>
</manyParams>
</configuration>
<executions>
<execution>
<phase>clean</phase>
<goals>
<goal>paramPrint</goal>
</goals>
</execution>
</executions>
</plugin>
执行命令mvn clean
需要先对插件对包,不然还是原来的jar包
mvn clean
实际项目中使用
输出src/main/java下面有多少文件
新增插件文件
package priv.dengjl.test_maven_plugin;
import java.io.File;
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;
@Mojo(name = "listFile", defaultPhase = LifecyclePhase.PACKAGE)
public class ListFilePlugin extends AbstractMojo {
@Parameter
private String path;
public void execute() throws MojoExecutionException, MojoFailureException {
getLog().info("================>");
getLog().info(path);
if (path == null) {
return;
}
getLog().info("<================");
printFile(path, " ");
}
public void printFile(String path, String tab) {
File file = new File(path);
if (file.exists()) { // 文件存在 用File类数组接收目录下所有的文件的抽象路径
File[] listFile = file.listFiles();
// 遍历目录下所有文件判断是否为文件夹
for (File file2 : listFile) {
getLog().info(tab + file2.getName());
// 如果是目录,把当前目录和taB作为参数传入,调用自身,
if (file2.isDirectory()) {// 传入子文件夹路径并换行
printFile(file2.getPath(), tab + "\t");
}
}
}
}
}
增加配置
<plugin>
<groupId>priv.dengjl</groupId>
<artifactId>test-maven-plugin</artifactId>
<version>0.0.1-SNAPSHOT</version>
<configuration>
<path>${basedir}/src/main/java</path>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>listFile</goal>
</goals>
</execution>
</executions>
</plugin>
测试