网上有很多关于maven插件编写的内容,这里我还是以我的方式,大部分代码形式来阐述。
<?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <groupId>cn.vicky</groupId> <artifactId>tools-plugins-tests</artifactId> <version>1.0-SNAPSHOT</version> <packaging>maven-plugin</packaging> <name>tools-plugins-tests</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.maven</groupId> <artifactId>maven-plugin-api</artifactId> <version>3.0.5</version> </dependency> </dependencies> </project>
package cn.vicky.tests; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; /** * @goal test1 * @phase compile * @requiresProject false 该命令可以在任何路径下执行,因为他不依赖maven项目 */ public class Test1 extends AbstractMojo { /** * @parameter expression="${name}" * @required */ String name; /** * @parameter expression="${age}" * @required */ int age; /** * @parameter expression="${isOk}" * @required */ boolean isOk; public void execute() throws MojoExecutionException, MojoFailureException { getLog().info(this.toString()); } public String toString() { return "String is : \"" + name + "\"" + "int is : \"" + age + "\"" + "boolean is : \"" + isOk + "\""; } }
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package cn.vicky.tests; import java.io.File; import java.util.List; import org.apache.maven.model.Resource; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; /** * * @goal test3 * @phase compile * @requiresProject true 该命令只能在Maven项目下执行,因为他依赖maven项目 */ public class Test3 extends AbstractMojo { private static final String[] DEFAULT_INCLUDES = new String[]{"java", "xml", "properties"}; /** * 项目根目录 * * @parameter expression="${project.basedir}" * @required * @readonly */ private File basedir; /** * 项目资源目录 * * @parameter expression="${project.build.sourceDirectory}" * @required * @readonly */ private File sourceDirectory; /** * 项目测试资源目录 * * @parameter expression="${project.build.testSourceDirectory}" * @required * @readonly */ private File testSourceDirectory; /** * 其他目录,由于没有配置required 以及 readonly,故可以不用配置, 且在pom.xml * plugin->configuration会显示该属性的代码提示 * * @parameter expression="${otherFile}" */ private File otherFile; /** * 项目资源 * * @parameter expression="${project.build.resources}" * @required * @readonly */ private List<Resource> resources; /** * 项目测试资源 * * @parameter expression="${project.build.testResources}" * @required * @readonly */ private List<Resource> testResources; /** * 额外参数,由于没有配置expression,所以只能过通过pom.xml plugin->configuration配置获得 * * @parameter */ private String[] includes; /** * 额外参数,可以通过pom.xml plugin->configuration配置获得,也可以通过 -Dother=a,b,c * 这样获得,但如果同时在pom.xml中配置也通过-D配置, 那么将使用-D替换pom.xml的配置 * * @parameter expression="${others}" */ private String[] others; /** * 设置一个默认值 * * @parameter default-value="99" */ private int num; public void execute() throws MojoExecutionException, MojoFailureException { for (Object key : getPluginContext().keySet()) { System.out.println("key:" + key + " value:" + getPluginContext().get(key)); } if (includes == null) { includes = DEFAULT_INCLUDES; } getLog().info("\t basedir : " + basedir.toString()); getLog().info("\t sourceDirectory : " + sourceDirectory.toString()); getLog().info("\t testSourceDirectory : " + testSourceDirectory.toString()); if (otherFile != null) { getLog().info("\t otherFile : " + otherFile.toString()); } for (Resource rs : resources) { getLog().info("\t resource : " + rs.toString()); } for (Resource rs : testResources) { getLog().info("\t testResource : " + rs.toString()); } if (includes != null) { for (String str : includes) { getLog().info("\t include : " + str); } } if (others != null) { for (String str : others) { getLog().info("\t other : " + str); } } getLog().info("\t num : " + num); } }
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package cn.vicky.tests; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.maven.model.Resource; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; /** * * @goal filecounts * @phase compile * @requiresProject true 该命令只能在Maven项目下执行,因为他依赖maven项目 */ public class FileCounts extends AbstractMojo { private static final String[] DEFAULT_INCLUDES = new String[]{"java", "xml", "properties"}; /** * 项目根目录 * * @parameter expression="${project.basedir}" * @required * @readonly */ private File basedir; /** * 项目资源目录 * * @parameter expression="${project.build.sourceDirectory}" * @required * @readonly */ private File sourceDirectory; /** * 项目测试资源目录 * * @parameter expression="${project.build.testSourceDirectory}" * @required * @readonly */ private File testSourceDirectory; /** * 项目资源 * * @parameter expression="${project.build.resources}" * @required * @readonly */ private List<Resource> resources; /** * 项目测试资源 * * @parameter expression="${project.build.testResources}" * @required * @readonly */ private List<Resource> testResources; /** * 额外参数,由于没有配置expression,所以只能过通过pom.xml plugin->configuration配置获得 * * @parameter */ private String[] includes; public void execute() throws MojoExecutionException, MojoFailureException { if (includes == null) { includes = DEFAULT_INCLUDES; } List<File> rfFiles = new ArrayList<File>(); getRfFiles(rfFiles, basedir); for (File file : rfFiles) { try { System.out.println(getFilecounts(file)); } catch (FileNotFoundException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } } } private void getRfFiles(List<File> files, File file) { if (!file.exists()) { return; } if (file.isDirectory()) { File[] listFiles = file.listFiles(); for (File f : listFiles) { getRfFiles(files, f); } } else { for (String include : includes) { if (file.getName().endsWith("." + include)) { files.add(file); break; } } } } private FileCountsInfo getFilecounts(File file) throws FileNotFoundException, IOException { BufferedReader br = new BufferedReader(new FileReader(file)); int count = 0; try { while (br.ready()) { br.readLine(); count++; } } catch (Exception e) { } finally { br.close(); } return new FileCountsInfo(file, count); } } class FileCountsInfo { private File file; private int count; public FileCountsInfo(File file, int count) { this.file = file; this.count = count; } public File getFile() { return file; } public void setFile(File file) { this.file = file; } public int getCount() { return count; } public void setCount(int count) { this.count = count; } @Override public String toString() { return file.toString() + " count: " + count; } }
mvn cn.vicky:tools-plugins-tests:test1 -Dname=vicky -Dage=27 -DisOk=true
[tools-plugins-tests:test1]
String is : "vicky"int is : "27"boolean is : "true"
mvn cn.vicky:tools-plugins-tests:test3 -Dothers=o1,o2,o3
[tools-plugins-tests:test3]
key:pluginDescriptor value:Component Descriptor: role: 'org.apache.maven.plugin.Mojo', implementation: 'cn.vicky.tests.FileCounts', role hint: 'cn.vicky:tools-plugins-tests:1.0-SNAPSHOT:filecounts'
role: 'org.apache.maven.plugin.Mojo', implementation: 'cn.vicky.tests.Test2', role hint: 'cn.vicky:tools-plugins-tests:1.0-SNAPSHOT:test2'
role: 'org.apache.maven.plugin.Mojo', implementation: 'cn.vicky.tests.Test3', role hint: 'cn.vicky:tools-plugins-tests:1.0-SNAPSHOT:test3'
role: 'org.apache.maven.plugin.Mojo', implementation: 'cn.vicky.tests.Test1', role hint: 'cn.vicky:tools-plugins-tests:1.0-SNAPSHOT:test1'
---
key:project value:MavenProject: cn.vicky:tools-plugins-tests-tests:1.0-SNAPSHOT @ E:\tools-plugins\tools-plugins-tests-tests\pom.xml
basedir : E:\tools-plugins\tools-plugins-tests-tests
sourceDirectory : E:\tools-plugins\tools-plugins-tests-tests\src\main\java
testSourceDirectory : E:\tools-plugins\tools-plugins-tests-tests\src\test\java
otherFile : E:\tools-plugins\tools-plugins-tests-tests\xxx
resource : Resource {targetPath: null, filtering: false, FileSet {directory: E:\tools-plugins\tools-plugins-tests-tests\src\main\resources, PatternSet [includes: {}, excludes: {}]}}
testResource : Resource {targetPath: null, filtering: false, FileSet {directory: E:\tools-plugins\tools-plugins-tests-tests\src\test\resources, PatternSet [includes: {}, excludes: {}]}}
include : java
include : xml
include : properties
include : sql
other : o1
other : o2
other : o3
num : 1000
mvn cn.vicky:tools-plugins-tests:filecounts
[tools-plugins-tests:filecounts]
E:\tools-plugins\tools-plugins-tests-tests\nbactions.xml count: 31
E:\tools-plugins\tools-plugins-tests-tests\pom.xml count: 49
E:\tools-plugins\tools-plugins-tests-tests\src\main\java\cn\vicky\tools\plugins\tests\tests\App.java count: 14
E:\tools-plugins\tools-plugins-tests-tests\src\test\java\cn\vicky\tools\plugins\tests\tests\AppTest.java count: 35