转载自:http://julianlali.blog.163.com/blog/static/58133643201023103035974/
(一)
将CheckStyle(http://maven.apache.org/plugins/maven-checkstyle-plugin/usage.html)、FindBugs(http://mojo.codehaus.org/findbugs-maven-plugin/2.0.1/usage.html)集成到Maven中,代码检查可以通过构建工具自动完成。
将cobertura(http://mojo.codehaus.org/cobertura-maven-plugin/usage.html)集成到Maven中,可生成单元测试覆盖率报告。
那么,如何将配置集中化,以便在完成一次项目构建时不必手动处理配置呢?
使用Maven内置的依赖项机制可轻松解决问题。
1. 安装jdk1.5,配置JAVA_HOME、path中加上%JAVA_HOME%\bin;
2. 安装Maven,配置MAVEN_HOME、path中加上%MAVEN_HOME%\bin;
3. 配置Maven的插件安装目录,修改conf\下的settings.xml文件,修改其中的<localRepository>,如D:/app/m2/repository;
4. 配置Maven的可用内存(防止内存溢出),修改文件%MAVEN_HOME%\bin\mvn.bat,在@REM set MAVEN_OPTS=-Xdebug -Xnoagent -Djava.compiler=NONE...下添加一行set MAVEN_OPTS= -Xms256m –Xmx1024m。
使用配置管理工具如SVN将代码下载到本地。
(二)
首先,修改要检查代码库top级的pom.xml文件,在build pluginManagement和build plugins以及reporting部分配置插件,以便于下载安装对应版本的插件(Maven会自动从其镜像库中下载),方法如下:
<project>
...
<build>
<!-- To define the plugin version in your parent POM -->
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.3</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>2.0.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>2.5</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.4</version>
</plugin>
...
</plugins>
</pluginManagement>
<!-- To use the plugin goals in your POM or parent POM -->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.3</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>2.0.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>2.5</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.4</version>
</plugin>
...
</plugins>
</build>
...
<!-- To use the report goals in your POM or parent POM -->
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<configuration>
<configLocation>Loong_Checks.xml</configLocation>
</configuration>
<version>2.3</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>2.5</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.4</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>2.0.1</version>
<configuration>
<xmlOutput>true</xmlOutput>
<effort>Max</effort>
</configuration>
</plugin>
..
</plugins>
</reporting>
...
</project>
maven-checkstyle-plugin的最新版本为2.5,其对应的CheckStyle核心版本为5.0;maven-checkstyle-plugin 2.3对应的CheckStyle核心版本为4.4。查看maven仓库中checkstyle插件的pom文件,可看到如下内容,其中的版本号就为对应的CheckStyle的版本号。其他插件与此同理。
<dependency>
<groupId>checkstyle</groupId>
<artifactId>checkstyle</artifactId>
<version>4.4</version>
</dependency>
(三)
接下来,将自定义的checkstyle规则配置文件my_Checks.xml拷贝到top级目录。
也可以将配置文件放在子文件夹下,配置中带上相对路径即可。
<reporting>
<plugins>
……
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<configuration>
<configLocation>build-tools/src/main/resources/xx/my_checks.xml</configLocation>
</configuration>
<version>2.3</version>
</plugin>
……
</plugins>
</reporting>
如果使用插件自带的规则文件,可以作如下配置。maven-checkstyle-plugin插件自带的规则有sun_checks.xml、maven_checks.xml等,可查看插件包。
<reporting>
<plugins>
……
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<configuration>
<configLocation>config/maven_checks.xml</configLocation>
</configuration>
<version>2.3</version>
</plugin>
……
</plugins>
</reporting>
(四)
在reporting部分增加jxr插件(http://maven.apache.org/plugins/maven-jxr-plugin/usage.html),生成代码报告,这样在CheckStyle、Findbugs等报告中点击问题对应的链接就可以直接看到出错的代码。
<reporting>
<plugins>
……
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jxr-plugin</artifactId>
</plugin>
……
</plugins>
</reporting>
(五)
这些插件都是针对代码级的检查,生成的报告也是按照代码目录组织的,如果想要生成一个整体情况的报告,需要使用dashboard插件(http://mojo.codehaus.org/dashboard-maven-plugin/usage.html),生成聚合报告,修改pom文件,在build pluginManagement和build plugins以及reporting部分配置dashboard插件:
<project>
...
<build>
<!-- To define the plugin version in your parent POM -->
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>dashboard-maven-plugin</artifactId>
<version>1.0.0-beta-1</version>
</plugin>
...
</plugins>
</pluginManagement>
<!-- To use the plugin goals in your POM or parent POM -->
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>dashboard-maven-plugin</artifactId>
<version>1.0.0-beta-1</version>
</plugin>
...
</plugins>
</build>
...
<!-- To use the report goals in your POM or parent POM -->
<reporting>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>dashboard-maven-plugin</artifactId>
<version>1.0.0-beta-1</version>
</plugin>
...
</plugins>
</reporting>
...
</project>
(六)
要注意插件仓库的配置,如果项目maven仓库未提供插件的相关版本,有时构建时会报错找不到相关插件,需要修改pom文件:
<pluginRepositories>
<pluginRepository>
<id>Codehaus repository</id>
<url>http://repository.codehaus.org/</url>
</pluginRepository>
</pluginRepositories>
(七)
与其他插件不同,要想在dashboard聚合报告中包含FindBugs报告,需要在pom文件中对FindBugs作如下配置(http://mojo.codehaus.org/dashboard-maven-plugin/findbugs_support.html):
<reporting>
<plugins>
...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>2.0.1</version>
<configuration>
<xmlOutput>true</xmlOutput>
<effort>Max</effort>
</configuration>
</plugin>
...
</plugins>
</reporting>
WARNING :
· Minimum version of findbugs-maven-plugin 1.1.1 required.
· Do not use the findbugs "<xmlOutputDirectory/>" config option if you want the dashboard to include the findbugs summary section. see MOJO-987 JIRA issue.
(八)
打开命令窗口,用cd命令转到项目所在的目录下。使用mvn install命令将pom文件中配置的插件下载安装到本地,然后使用mvn site命令进行检查并生成报告,用mvn dashboard:dashboard命令聚合报告。运行完毕,项目各子模块目录下会生成target目录,target\site下即为该项目的问题报告。如果只想检查指定目录,则cd到该目录下执行mvn命令即可。
这样生成的报告是默认的样式,默认的内容(项目pom文件reporting配置的报告都会生成),关于maven site的自定义配置和高级用法,参见 maven2站点的定制和发布
(九)
如果运行mvn checkstyle:checkstyle或mvn site过程中出现如下错误,则应该修改CheckStyle规则配置文件,去除其中的中文字符。
“[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] An error has occurred in Checkstyle report generation.
Embedded error: Failed during checkstyle configuration
Invalid byte 1 of 1-byte UTF-8 sequence.
”
(十)
如果checkstyle跳过代码检查,提示” Source directory does not exist - skipping report.”,可能是源码路径不对,默认路径为${project.build.sourceDirectory},默认是src\main\java。配置sourceDirectory属性,如下所示:
<reporting>
<plugins>
……
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<configuration>
<configLocation>my_checks.xml</configLocation>
<sourceDirectory>src</sourceDirectory>
</configuration>
<version>2.3</version>
</plugin>
……
</plugins>
</reporting>
WARNING :
关于各插件的参数配置,具体可以参考官网Goals页面,点击基本Goals如(checkstyle:checkstyle,findbugs:findbugs)进入页面,即可看到参数列表。
(十一)
checkstyle规则的选取很重要,sun_checks很严格,不一定适合所有项目,所以需要根据项目实际情况和要求,配置checkstyle规则配置文件。
一个典型的checkstyle规则配置文件:
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
"-//Puppy Crawl//DTD Check Configuration 1.2//EN"
"http://www.puppycrawl.com/dtds/configuration_1_2.dtd">
<module name="Checker">
<property name="severity" value="warning"/>
<!-- Checks that a package.html file exists for each package. -->
<!-- See http://checkstyle.sf.net/config_javadoc.html#PackageHtml -->
<module name="PackageHtml">
<property name="severity" value="ignore"/>
</module>
<!-- Checks whether files end with a new line. -->
<!-- See http://checkstyle.sf.net/config_misc.html#NewlineAtEndOfFile -->
<module name="NewlineAtEndOfFile">
<property name="fileExtensions" value="MF"/>
</module>
<!-- Checks that property files contain the same keys. -->
<!-- See http://checkstyle.sf.net/config_misc.html#Translation -->
<module name="Translation"/>
<module name="TreeWalker">
<property name="tabWidth" value="4"/>
<!-- Checks for whitespace -->
<!-- See http://checkstyle.sf.net/config_whitespace.html -->
<module name="EmptyForIteratorPad"/>
<module name="MethodParamPad"/>
<module name="NoWhitespaceAfter">
<property name="tokens" value="BNOT,DEC,DOT,INC,LNOT"/>
</module>
<module name="NoWhitespaceBefore"/>
<module name="OperatorWrap"/>
<module name="ParenPad"/>
<module name="TypecastParenPad"/>
<module name="WhitespaceAfter"/>
<module name="WhitespaceAround">
<property name="tokens" value="ASSIGN,BAND,BAND_ASSIGN,BOR,BOR_ASSIGN,BSR,BSR_ASSIGN,BXOR,BXOR_ASSIGN,COLON,DIV,DIV_ASSIGN,EQUAL,GE,LAND,LE,LITERAL_ASSERT,LITERAL_CATCH,LITERAL_DO,LITERAL_ELSE,LITERAL_FINALLY,LITERAL_FOR,LITERAL_IF,LITERAL_RETURN,LITERAL_SYNCHRONIZED,LITERAL_TRY,LITERAL_WHILE,LOR,MINUS_ASSIGN,MOD,MOD_ASSIGN,NOT_EQUAL,PLUS_ASSIGN,QUESTION,SL,SLIST,SL_ASSIGN,SR,SR_ASSIGN,STAR,STAR_ASSIGN,LITERAL_ASSERT,TYPE_EXTENSION_AND,WILDCARD_TYPE"/>
</module>
<!-- Checks for Size Violations. -->
<!-- See http://checkstyle.sf.net/config_sizes.html -->
<module name="FileLength"/>
<!-- LineLength is controlled in eclipse, auto fomatting when saving files -->
<!-- <module name="LineLength">
<property name="max" value="120"/>
</module> -->
<module name="MethodLength"/>
<module name="ParameterNumber"/>
<!-- Checks for Naming Conventions. -->
<!-- See http://checkstyle.sf.net/config_naming.html -->
<module name="ConstantName"/>
<module name="LocalFinalVariableName">
<property name="format" value="^[a-z][a-zA-Z0-9_]*$"/>
</module>
<module name="LocalVariableName">
<property name="format" value="^[a-z][a-zA-Z0-9_]*$"/>
</module>
<module name="MemberName">
<property name="format" value="^[a-z][a-zA-Z0-9_]*$"/>
</module>
<module name="MethodName"/>
<module name="PackageName"/>
<module name="ParameterName">
<property name="format" value="^[a-z][a-zA-Z0-9_]*$"/>
</module>
<module name="StaticVariableName">
<property name="format" value="^[a-z][a-zA-Z0-9_]*$"/>
</module>
<module name="TypeName"/>
<!-- Modifier Checks -->
<!-- See http://checkstyle.sf.net/config_modifiers.html -->
<module name="ModifierOrder"/>
<module name="RedundantModifier"/>
<!-- Miscellaneous other checks. -->
<!-- See http://checkstyle.sf.net/config_misc.html -->
<module name="ArrayTypeStyle"/>
<!-- <module name="FinalParameters"/> -->
<!-- <module name="GenericIllegalRegexp">
<property name="severity" value="ignore"/>
<property name="format" value="\s+$"/>
<property name="message" value="Line has trailing spaces."/>
</module> -->
<module name="UpperEll"/>
<!-- Checks for blocks. You know, those {}'s -->
<!-- See http://checkstyle.sf.net/config_blocks.html -->
<module name="EmptyBlock">
<property name="option" value="text"/>
</module>
<module name="LeftCurly"/>
<module name="NeedBraces"/>
<module name="RightCurly"/>
<module name="AvoidNestedBlocks"/>
<!-- Checks for class design -->
<!-- See http://checkstyle.sf.net/config_design.html -->
<module name="FinalClass"/>
<module name="InterfaceIsType"/>
<!-- <module name="DesignForExtension"/> -->
<module name="HideUtilityClassConstructor"/>
<!-- <module name="VisibilityModifier"/> -->
<!-- Checks for common coding problems -->
<!-- See http://checkstyle.sf.net/config_coding.html -->
<module name="AvoidInlineConditionals"/>
<module name="DoubleCheckedLocking"/> <!-- MY FAVOURITE -->
<module name="EmptyStatement"/>
<!-- <module name="EqualsHashCode"/> -->
<!-- <module name="HiddenField"/> -->
<module name="IllegalInstantiation"/>
<module name="InnerAssignment"/>
<!-- <module name="MagicNumber"/> -->
<module name="MissingSwitchDefault"/>
<!-- <module name="RedundantThrows">
<property name="logLoadErrors" value="true"/>
<property name="suppressLoadErrors" value="true"/>
</module> -->
<module name="SimplifyBooleanExpression"/>
<module name="SimplifyBooleanReturn"/>
<!-- Checks for imports -->
<!-- See http://checkstyle.sf.net/config_import.html -->
<module name="AvoidStarImport"/>
<module name="IllegalImport"/> <!-- defaults to sun.* packages -->
<module name="RedundantImport"/>
<module name="UnusedImports"/>
<!-- Checks for Javadoc comments. -->
<!-- See http://checkstyle.sf.net/config_javadoc.html -->
<module name="JavadocType">
<property name="excludeScope" value="private"/>
<property name="scope" value="nothing"/>
</module>
<module name="JavadocMethod">
<property name="allowMissingJavadoc" value="true"/>
<property name="logLoadErrors" value="true"/>
<property name="suppressLoadErrors" value="true"/>
</module>
<module name="JavadocVariable">
<property name="excludeScope" value="private"/>
<property name="scope" value="nothing"/>
</module>
<module name="JavadocStyle">
<property name="checkFirstSentence" value="false"/>
<property name="checkHtml" value="false"/>
</module>
<!-- Checks for Headers -->
<!-- See http://checkstyle.sf.net/config_header.html -->
<!-- <module name="Header"> -->
<!-- The follow property value demonstrates the ability -->
<!-- to have access to ANT properties. In this case it uses -->
<!-- the ${basedir} property to allow Checkstyle to be run -->
<!-- from any directory within a project. See property -->
<!-- expansion, -->
<!-- http://checkstyle.sf.net/config.html#properties -->
<!-- <property -->
<!-- name="headerFile" -->
<!-- value="${basedir}/java.header"/> -->
<!-- </module> -->
<!-- Following interprets the header file as regular expressions. -->
<!-- <module name="RegexpHeader"/> -->
</module>
</module>