經過上述方式建立,會自動引入maven-plugin-api 依賴和打包方式;以下html
<packaging>maven-plugin</packaging> <dependency> <groupId>org.apache.maven</groupId> <artifactId>maven-plugin-api</artifactId> <version>2.0</version> </dependency>
** * @goal com-doc 插件的標識 * @phase compile 表示哪一個階段運行插件 */ public class DocProduct extends AbstractMojo{ public void execute() { this.getLog().info("進入自定義插件"); } }
<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 的子類註解保持一致。
掃描類java
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; } }