`
Surlymo
  • 浏览: 95614 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

格式化代码、风格检查、代码漏洞检查以及UT覆盖率检查

阅读更多

    在敏捷开发中,随着文档地位不断被削弱,此时代码的可读性和质量往往成为直接决定项目是否健壮可维护的关键。这就要求我们的代码具有良好的风格(包括注释、命名等)、代码格式标准、程序没有非法调用和低级bug,以及用以对功能进行解释的单元测试能够尽可能多地覆盖核心功能。如果每次我们都手动去依次检查这些点是否达标,那持续集成将变得无比缓慢。更遑论持续交付了。

    所以在持续集成(CI)的过程中,每次local build的时候,我们可以利用一些maven插件对代码进行以上需求的自动化校准或校验。宥于时间,所以此处不能对每个插件的使用进行详细说明。以后有时间会进行详细整理。以下我们依次进行简单介绍。

 

一、代码格式化

eclipse插件:

打开eclipse,window --> Preferences --> Java --> Code Style --> Formatter



maven插件:

同时,在maven中导入插件可以保证在每次mvn install的时候都默认进行代码的自动格式化:

 

<plugin>
    <groupId>com.googlecode.maven-java-formatter-plugin</groupId>
    <artifactId>maven-java-formatter-plugin</artifactId>
    <version>0.4</version>
    <configuration>
        <configFile>formatter-profile.xml</configFile>
        <encoding>gbk</encoding>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>format</goal>
            </goals>
        </execution>
    </executions>
</plugin>	
  其中,如果不需要特定的配置文件指定,则无需引入<configuration>标签。

二、风格检查

eclipse插件:

checkstyle插件的安装见:http://orange5458.iteye.com/blog/1547851。此处建议采用手动安装,自动安装的地址在我使用过程中无法连接。此处摘取部分内容说明如下:

1)下载地址:http://sourceforge.net/projects/eclipse-cs/files/OldFiles/

2)将下载包解压后的features和plugins目录中的文件复制到%ECLIPSE_HOME%下的对应目录中

3)重启eclipse

默认将采用sun的风格标准进行代码风格检查,如果不采用默认的checkstyle配置,要导入配置文件只需:preferences--> checkstyle --> New --> External Configuration File. 并导入对应xml配置即可。checkstyle配置文件的说明见:http://blog.csdn.net/kaiqi239/article/details/5734785



 

常用方法:

1、手动检查:

待检查目标点击右键 --> checkstyle --> check code with checkstyle

2、清除检查结果:

待检查目标点击右键 --> checkstyle --> clear checkstyle voilations

3、查看风格报警信息:

window --> show view --> checkstyle violations

maven插件:

除此之外,我们可以采用checkstyle的maven插件来实现与持续集成的绑定,详细见http://blog.csdn.net/kongxx/article/details/7750015

 

三、代码漏洞检查

eclipse插件:

采用findbugs来进行代码检查。http://tidus2005.iteye.com/blog/462212 中对eclipse中如何安装和使用findbugs进行了较为详细的说明。此处不再赘述。

maven插件:

如果要引入其maven插件,仅需:

 

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>findbugs-maven-plugin</artifactId>
    <version>2.3.1</version>
    <configuration>
        <threshold>High</threshold>
        <findbugsXmlOutput>true</findbugsXmlOutput>
        <findbugsXmlOutputDirectory>.</findbugsXmlOutputDirectory>
	<findbugsHtmlOutput>true</findbugsHtmlOutput>	<findbugsHtmlOutputDirectory>target/site</findbugsHtmlOutputDirectory>
    </configuration>
    <executions>
        <execution>
            <id>findbugs</id>
            <phase>verify</phase>
            <goals>
                <goal>findbugs</goal>
            </goals>
        </execution>
    </executions>
</plugin>

 

然后执行:

 

mvn org.codehaus.mojo:findbugs-maven-plugin:2.0.1:findbugs

即可获得对应的报告数据。这样hudson或者jenkins的CI平台,就可以依据报告进行代码漏洞的分析。

 

四、UT覆盖率检查

eclipse插件:

采用cobertura进行UT覆盖率检查。eclipse的cobertura插件名为ecobertura,详细请见:http://ecobertura.johoop.de/

maven插件:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>cobertura-maven-plugin</artifactId>
    <version>2.5.1</version>
    <configuration>
        <formats>
            <format>xml</format>
        </formats>
        <aggregate>true</aggregate>
        <instrumentation>
            <includes>
                <include>${cobertura.include}</include>
            </includes>
        </instrumentation>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>clean</goal>
                <goal>cobertura</goal>
            </goals>
        </execution>
     </executions>
</plugin>

 执行:

mvn clean cobertua:cobertura

 即可生成报告。依此结合CI平台进行相应的分析和管控。

 

 

 

 

 

  • 大小: 58.4 KB
  • 大小: 157 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics