編寫第一個maven外掛(含完整專案)
基本上快把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
有需要的同學可以直接下載專案啊:
當然有問題的同學可以提出來,大家一起學習哈
(๑><๑)