自定义maven插件的实现
---------------------------------------------------------------------------------------------
[版权申明:本文系作者原创,转载请注明出处]
文章出处:https://blog.csdn.net/sdksdk0/article/details/80678434
作者:朱培 ID:sdksdk0
--------------------------------------------------------------------------------------------
最近在折腾maven,然后研究了一下maven的插件的写法,然后做了一个案例,通过maven插件来统计当前工程中的目录下有多少个java文件,操作步骤如下,在intellij idea中使用。
一、基本使用
1、新建一个maven项目,我这边jdk的build选择的是1.8,在pom.xml中添加
<packaging>maven-plugin</packaging>
2、添加依赖
<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>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<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>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
3、java类:
插件名为sogoucloud,支持msg,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;
import java.util.List;
@Mojo(name="sogoucloud",defaultPhase= LifecyclePhase.PACKAGE)
public class SogoucloudMojo extends AbstractMojo {
@Parameter
private String msg;
@Parameter
private List<String> options;
@Parameter(property = "args")
private String args;
public void execute() throws MojoExecutionException,MojoFailureException{
System.out.println("hello sogoucloud:"+msg);
System.out.println("hello sogoucloud:"+options);
System.out.println("hello sogoucloud:"+args);
}
}
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;
import java.util.List;
@Mojo(name="sogoucloud",defaultPhase= LifecyclePhase.PACKAGE)
public class SogoucloudMojo extends AbstractMojo {
@Parameter
private String msg;
@Parameter
private List<String> options;
@Parameter(property = "args")
private String args;
public void execute() throws MojoExecutionException,MojoFailureException{
System.out.println("hello sogoucloud:"+msg);
System.out.println("hello sogoucloud:"+options);
System.out.println("hello sogoucloud:"+args);
}
}
4、打包 mvn clean install ,安装到仓库,便于被其他项目引用
5、新建另一个maven web项目,引入前面的插件
<build>
<plugins>
<plugin>
<groupId>cn.sogoucloud</groupId>
<artifactId>sogoucloud-maven-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<configuration>
<msg>This is my message</msg>
<options>
<option>one Array</option>
<option>two Array</option>
</options>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>sogoucloud</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<plugins>
<plugin>
<groupId>cn.sogoucloud</groupId>
<artifactId>sogoucloud-maven-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<configuration>
<msg>This is my message</msg>
<options>
<option>one Array</option>
<option>two Array</option>
</options>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>sogoucloud</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
6、可以在项目下看到这个插件
7、使用,mvn install -Dargs=aaa,将这个maven项目install起来,就可以看到之前传的参数了
二、实战Archetype,生成骨架
新建好骨架的maven工程之后,然后再项目路径下执行:
mvn archetype:create-from-project
然后会在target目录中生成generated-sources\archetype,然后进入这个目录
cd D:\IntelWorkspace\sogoucloud-plugin-demo\target\generated-sources\archetype
将这个骨架install到仓库中
mvn install
使用创建的骨架来使用:
mvn archetype:generate -DarchetypeCatalog=local
然后就可以在工作空间中生成以这个骨架为基础的项目工程。
三、案例实战:写一个maven插件统计当前工程里面有多少个java文件:
1、java类
@Mojo(name="countFile",defaultPhase= LifecyclePhase.PACKAGE)
public class CountFileMojo extends AbstractMojo {
@Parameter(property = "path")
private String path;
int num1 = 0; //总文件数量
int num2 = 0; //目录数量
int num3 = 0; //java文件数量
public void execute() throws MojoExecutionException,MojoFailureException{
System.out.println(path);
System.out.println(countFile(path));
}
public String countFile(String dir){
File f = new File(dir);
File fs[] = f.listFiles();
if(fs!=null){
for(int i=0;i<fs.length;i++){
File currenFile = fs[i];
if(currenFile.isFile()){
num1+=1; //如果是文件就加1
String fileName = currenFile.getName();
String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
if(suffix.equals("java")){
num3+=1;
}
}else{
//否则就是目录
num2+=1;
countFile(currenFile.getAbsolutePath());
}
}
}
return "Total number of File:"+num1+"||||The number of dir is:"+num2+"||||Total number of Java File:"+num3;
}
}
public class CountFileMojo extends AbstractMojo {
@Parameter(property = "path")
private String path;
int num1 = 0; //总文件数量
int num2 = 0; //目录数量
int num3 = 0; //java文件数量
public void execute() throws MojoExecutionException,MojoFailureException{
System.out.println(path);
System.out.println(countFile(path));
}
public String countFile(String dir){
File f = new File(dir);
File fs[] = f.listFiles();
if(fs!=null){
for(int i=0;i<fs.length;i++){
File currenFile = fs[i];
if(currenFile.isFile()){
num1+=1; //如果是文件就加1
String fileName = currenFile.getName();
String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
if(suffix.equals("java")){
num3+=1;
}
}else{
//否则就是目录
num2+=1;
countFile(currenFile.getAbsolutePath());
}
}
}
return "Total number of File:"+num1+"||||The number of dir is:"+num2+"||||Total number of Java File:"+num3;
}
}
2、在另外一个工程中引用:
<plugin>
<groupId>cn.sogoucloud</groupId>
<artifactId>sogoucloud-maven-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<configuration>
<path>${project.basedir}</path>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<!--<goal>sogoucloud</goal>-->
<goal>countFile</goal>
</goals>
</execution>
</executions>
</plugin>
<groupId>cn.sogoucloud</groupId>
<artifactId>sogoucloud-maven-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<configuration>
<path>${project.basedir}</path>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<!--<goal>sogoucloud</goal>-->
<goal>countFile</goal>
</goals>
</execution>
</executions>
</plugin>
查看结果:
本文用到的案例源码下载地址如下:https://download.csdn.net/download/sdksdk0/10476412