本文主要是介紹maven的幾個常見第三方插件(cobertura、findbugs、source、assembly、插件開發)配置和使用,接http://trinea.iteye.com/blog/1290898
maven本質上是一個插件框架,它的所有工作都交給插件來做,每個插件可以有多個goal。
除了自帶的插件之外還有很多比較成熟的第三方插件,我們也很容易上手進行簡單的插件開發,下面一一介紹
1 自帶插件
maven自帶的核心插件為Build plugins和Reporting plugins。
mvn compile編譯源碼實際上就利用到了maven-compiler-plugin,其他phase也類似用到了相應的插件
關于maven自帶的核心插件見:http://maven.apache.org/plugins/index.html
2 第三方插件
2.1 maven有很多成熟的第三方插件
如jetty 對于web開發使用jetty作為容器
native 編譯c和c++代碼
sql 執行sql腳本
其他更多見:http://maven.apache.org/plugins/index.html#Outside_The_Maven_Land
下面具體介紹下單元測試覆蓋率插件cobertura、findbugs
2.2 maven2的cobertura插件
2.2.1 cobertura是一款用來計算java代碼測試覆蓋率的工具,基于jcoverage。能計算每個類、包、整個工程的行覆蓋率和分支覆蓋率以及代碼復雜度(Cyclomatic complexity)并生成html或xml形式的報告,讓用戶很方便的查看代碼的單元測試覆蓋率情況。cobertura的原理是通過對class文件進行插樁然后計算。
2.2.2 maven2的cobertura插件介紹
插件地址為http://mojo.codehaus.org/cobertura-maven-plugin/index.html
a、首先在pom中添加配置如下
Xml代碼

- <reporting>
- <outputDirectory>target/site</outputDirectory>
- <plugins>
- <plugin>
- <groupId>org.codehaus.mojo</groupId>
- <artifactId>cobertura-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </reporting>
b、運行goal
到項目根目錄下運行mvn cobertura:cobertura 將會插樁class文件、測試、生成覆蓋率報告
cobertura支持的goal如下
c、在target\site\cobertura目錄下生成報告文件,打開index.html可以查看具體報告
mvn cobertura:cobertura執行前會執行test phase,即執行單側代碼
2.3 maven2的findbugs插件
2.3.1 findbugs是靜態檢查java代碼的工具,根據一些bugs的表達式檢查代碼中的bugs,可以自定義檢查規則
2.3.2 maven2的findbugs插件介紹
插件地址為http://mojo.codehaus.org/findbugs-maven-plugin/index.html
a、首先在pom中添加配置如下
不同goal的配置略有不同,可自己調整,以下介紹的是mvn findbugs:findbugs的配置
Xml代碼

- <reporting>
- <plugins>
- <plugin>
- <groupId>org.codehaus.mojo</groupId>
- <artifactId>findbugs-maven-plugin</artifactId>
- <version>2.3.1</version>
- </plugin>
- </plugins>
- </reporting>
b、運行goal
到項目根目錄下運行mvn findbugs:findbugs將會開始檢查,并生成bugs報告
findbugs支持的goal如下
Xml代碼

- findbugs:check
- Fail the build if there were any FindBugs violations in the source code. An
- XML report is put out by default in the target directory with the errors. To
- see more documentation about FindBugs' options, please see the FindBugs
- Manual..
-
- findbugs:findbugs
- Generates a FindBugs Report when the site plugin is run. The HTML report is
- generated for site commands only.
-
- findbugs:gui
- Launch the Findbugs GUI. It will use all the parameters in the POM fle.
-
- findbugs:help
- Display help information on findbugs-maven-plugin.
- Call
- mvn findbugs:help -Ddetail=true -Dgoal=<goal-name>
- to display parameter details.
c、在target\site\findbugs目錄下生成報告文件,打開index.html可以查看具體報告
mvn findbugs:findbugs綁定到了compile phase,即在編譯時自動檢查
http://qa.taobao.com/?p=4206
2.4 maven的source插件
2.4.1 source插件用來將工程打包成帶源代碼的jar包
2.4.2 maven2的source插件介紹
Xml代碼

- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-source-plugin</artifactId>
- <version>2.1.2</version>
- <executions>
- <execution>
- <id>attach-sources</id>
- <phase>verify</phase>
- <goals>
- <goal>jar-no-fork</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
- </plugins>
- </build>
直接運行mvn clean install會在target下打出兩個包,帶***-sources.jar的為源碼包
2.5 maven的assembly插件
2.5.1 assembly插件可用來將工程依賴的jar包和工程都打成一個jar打包
2.5.2 maven2的assembly插件pom配置如下
Xml代碼

- <build>
- <plugins>
- <plugin>
- <artifactId>maven-assembly-plugin</artifactId>
- <configuration>
- <descriptorRefs>
- <descriptorRef>jar-with-dependencies</descriptorRef>
- </descriptorRefs>
- </configuration>
- </plugin>
- </plugins>
- </build>
直接運行mvn assembly:assembly會在target下出現***-with-dependencies.jar的jar包
2.6 插件開發
maven的插件開發相當簡單,可以參考http://trinea.iteye.com/blog/1171957