基本上快把maven实战这本书看完了,对里面的知识点不敢说全懂,但至少懂了个百分之六七十,不过大部分概念还是清楚地,
剩下的就需要实际的码代码中去学习了。
回归正题,编写一个maven插件:
第一步:
建立一个maven项目,可以用命令行的方式建立如:mvn archetype:generate
然后选择:
maven-archetype-plugin
待输入完坐标信息之后,一个maven插件就创建好了。
或者还可以在eclipse中通过新建maven项目,然后选择maven-archetype-plugin这个type,并且输入相应
坐标信息进行创建。
创建完后,项目代码结构如下图所示:
当创建完后,会自动生成一个mojo类,mojo类似于pojo,是maven插件命令的具体执行类,里面会有一个execute()方法,用于具体执行。
第二步:
先配置pom.xml文件,具体只需要配置部分dependency和plugin,如下见我项目的部分pom.xml
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.version>3.0</maven.version>
</properties>
<dependencies>
<!-- 写maven插件必备的一些api -->
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>${maven.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>3.0.8</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<!-- 注意,必须要这个maven,plugin -->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>${maven.version}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.version}</version>
</plugin>
</plugins>
</build>
开始没有使用maven-plugin-plugin,后来报了个错,说没有解释器。
类似于:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-plugin-plugin:2.9:descriptor (default-descriptor) on project plugin-example1: Error extracting plugin descriptor: 'No mojo definitions were found for plugin: org.freebird:plugin-example1.' -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-plugin-plugin:2.9:descriptor (default-descriptor) on project plugin-example1: Error extracting plugin descriptor: 'No mojo definitions were found for plugin: org.freebird:plugin-example1.'
这时候就要加入maven-plugin-plugin插件。
第三步,如何编写Mojo类?
大体的思路就是,通过传入的参数,然后进行实际的业务操作。我这里主要是以统计代码行数为原型进行说明:
首先获取maven内置隐含变量:
/**
* @parameter expression = "${project.basedir}"
* @readonly
* @required
*/
private File baseDir;
/**
* @parameter expression = "${project.build.sourceDirectory}"
* @readonly
* @required
*/
private File sourceDirectory;
/**
* @parameter expression = "${project.build.testSourceDirectory}"
* @readonly
* @required
*/
private File testSourceDirectory;
/**
* @parameter expression = "${project.build.resources}"
* @readonly
* @required
*/
private List<Resource> resources;
/**
* @parameter expression = "${project.build.testResources}"
* @readonly
* @required
*/
private List<Resource> testResources;
/**
* The file types which will be incluede for counting
* @parameter
*/
private String[] includes;
这里遇到过个坑,开始是用的@parameter这个注解,具体格式是:
@Parameter( defaultValue = "${project.basedir}",property = "basedir", readonly = true, required = true )
但是总是获取不到值,对,获取不到内置的隐含变量,最终搞了几十分钟,最终用了上述方法,可能是没了解深入吧。
然后接下来进行execute方法:
public void execute() throws MojoExecutionException, MojoFailureException {
if(includes == null || includes.length == 0){ //如果没有在pom.xml中说明includes,就是用默认的includes。
includes = INCLUDES_DEFAULT;
}
try {
//分别统计四种目录下的代码行数。
countDir(sourceDirectory);
countDir(testSourceDirectory);
for(Resource resource:resources){
countDir(new File(resource.getDirectory()));
}
for(Resource resource:testResources){
countDir(new File(resource.getDirectory()));
}
} catch (Exception e) {
throw new MojoExecutionException("Unable to count lines of code.",e);
}
}
具体就是找到所有文件,然后逐个统计各个文件代码行数。
基本的maven插件就写完了。
还有一个值得注意的点,就是必须要提供@goal标注,也就是mojo类的最上面那里,
这个goal标记,就是用于maven判断,执行该插件的哪一个类,不然不可能一个插件只有一个mojo类吧!
第四步,测试自己写的maven插件;
开始测试的时候被陷入到了eclipse这个坑里面了:
我首先是新建了一个简单的maven项目,然后直接debug as ,然后在eclipse的goal中输入goal命令:
com.anla.study:CountLines:0.0.1-SNAPSHOT:count
然后只会出现一个结果,就是只统计我写的框架里面的代码,也就是你在另一个项目中执行,实际上是它自己
统计自己!!
后来直接在另一个项目根目录中,执行mvn com.anla.study:CountLines:0.0.1-SNAPSHOT:count
又可以统计了,
注意:以上情况是我并没有在新建的项目中生命我的maven插件。
还有另一个方法,就是在新建的项目中,在pom文件中使用这个插件,也就是把这个插件与他的一个生命周期绑定起来,
如下新的maven的pom.xml:
<build>
<plugins>
<plugin>
<groupId>com.anla.study</groupId>
<artifactId>CountLines</artifactId>
<version>0.0.1-SNAPSHOT</version>
<configuration>
<includes>
<include>java</include>
<include>properties</include>
</includes>
<ratios>
<ratio>1.0</ratio>
<ratio>0.5</ratio>
</ratios>
</configuration>
<executions>
<execution>
<id>count line number</id>
<phase>install</phase>
<goals>
<goal>count</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
这样一来,会统计当前项目的代码行数,并且在install这个阶段就会统计好的。
第五步:优化命令输入
记得上一步,需要输入:com.anla.study:CountLines:0.0.1-SNAPSHOT:count
这么长才能使用,其实可以更简单的输入。
可以在maven安装目录下setting.xml添加几行,就可以简化了:
<pluginGroups>
<pluginGroup>com.anla.study</pluginGroup>
</pluginGroups>
然后就可以直接通过这一段运行了:
mvn CountLines:count
有需要的同学可以直接下载项目啊:
https://github.com/anLA7856/CountLines
当然有问题的同学可以提出来,大家一起学习哈
(๑><๑)