自定义maven插件
1、 创建一个maven 项目
通过上述方式创建,会自动引入maven-plugin-api 依赖和打包方式;如下
<packaging>maven-plugin</packaging>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>2.0</version>
</dependency>
2、 继承 AbstractMojo 类,并重写execute 方法,并添加注解
**
* @goal com-doc 插件的标识
* @phase compile 表示哪个阶段运行插件
*/
public class DocProduct extends AbstractMojo{
public void execute() {
this.getLog().info("进入自定义插件");
}
}
3、 外部引用插件
<plugin>
<groupId>cn.pl</groupId>
<artifactId>common-doc</artifactId>
<version>1.0-SNAPSHOT</version>
<configuration>
<classPath>${basedir}\target\classes</classPath>
<libDir>${basedir}\target\${artifactId}\WEB-INF\lib</libDir>
<basePackage>${basedir}\target/classes\cn\pl</basePackage>
<targetFile>${basedir}\src\main\webapp\open-api.html</targetFile>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>com-doc</goal>
</goals>
</execution>
</executions>
</plugin>
<configuration>
节点的参数,可以在继承与AbstractMojo 的类中接受,需要添加注解,例如:
<execution>
子节点 <phase>compile</phase>
表示运行在哪个阶段
<goals> <goal>com-doc</goal> </goals>
表示插件的标识 同AbstractMojo 的子类注解保持一致。
自定义maven 插件已完结
扫描类
package cn.pl;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.URLStreamHandler;
import java.util.ArrayList;
import java.util.List;
/**
* @goal com-doc
* @phase compile
*/
public class DocProduct extends AbstractMojo{
/**
* @parameter expression="${classPath}"
*/
private String classPath;
/**
* @parameter expression="${libDir}"
*/
private String libDir;
/**
* @parameter expression="${basePackage}"
*/
private String basePackage;
/**
* @parameter expression="${targetFile}"
*/
private String targetFile;
private URLClassLoader loader;
public void execute() throws MojoExecutionException, MojoFailureException {
this.getLog().info("进入自定义插件");
try {
String libDir = (new URL("file",null,new File(this.libDir).getCanonicalPath()+File.separator)).toString();
String baseDir = (new URL("file",null,new File(this.classPath).getCanonicalPath()+File.separator)).toString();
File libDirFile = new File(libDir.replaceAll("file",""));
URLStreamHandler us = null;
List<URL> libs = new ArrayList<URL>();
if(null != libDirFile.listFiles()){
for (File jar: libDirFile.listFiles()) {
libs.add(new URL(null,libDir+jar.getName(),us));
}
}
libs.add(new URL(null,baseDir,us));
loader = new URLClassLoader(libs.toArray(new URL[libs.size()]),Thread.currentThread().getContextClassLoader());
File dir = new File(basePackage);
List<Class<?>> classes = new ArrayList<Class<?>>();
scanner(classes,dir);
this.getLog().info(String.valueOf(classes.size()));
for (Class clazz: classes) {
this.getLog().info("-------------"+clazz.getName());
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 扫面配置基础类
* @param dir
* @return
*/
public void scanner( List<Class<?>> classes,File dir){
File[] files = dir.listFiles();
for (File file: files) {
if(file.isDirectory()){
scanner(classes,file);
}else {
if (!file.getName().endsWith(".class")){
continue;
}
String path = file.getPath();
String className = getClassName(path);
try {
classes.add(Class.forName(className,true,loader));
this.getLog().info("加载进入的:"+className);
} catch (ClassNotFoundException e) {
e.printStackTrace();
continue;
}
}
}
}
public String getClassName(String path){
classPath = (classPath==null?"":classPath);
String classDir = path.replaceAll("\\\\","/")
.replaceAll(classPath.replaceAll("\\\\","/"),"")
.replaceAll("/",".")
.replaceAll("\\.class","");
String className = classDir.substring(1,classDir.length());
return className;
}
}