posts - 11, comments - 3, trackbacks - 0, articles - 0
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

在ant中集成checkstyle工具

Posted on 2006-07-20 15:51 eddy liao 阅读(3008) 评论(0)  编辑  收藏 所属分类: 软件质量

checkstyle 是一个帮助开发者按照某种习惯编写 java 代码的工具,他实现了代码检查的自动化,帮助人们从这种繁琐的工作中解放出来。

默认提供了对 sun 编程规范的支持,但是 checkstyle 是一个具有高可配置性的,你完全可以根据自己的要求来配置需要检查的内容。

有以下这些东西
\lib\checkstyle-3.1\contrib\checkstyle-noframes.xsl
\lib\checkstyle-3.1\checkstyle-all-3.1.jar
\lib\checkstyle-3.1\sun_checks.xml                    

在build.xml文件中添加
    <patternset id="java.files.pattern" includes="**/*.java"/>

    <target name="checkstyle" depends="prepare"           依赖prepare target
        description="Check code style for compliance with coding standards">
        <property name="checkstyle.data.dir"
            location="${build.dir}/docs/checkstyle"/>                 存放路径
        <property name="checkstyle.data.file"
            location="${checkstyle.data.dir}/checkstyle.xml"/>     xml文件
        <property name="checkstyle.report.file"
            location="${checkstyle.data.dir}/checkstyle.html"/>   html文件
        <property name="checkstyle.xsl.file"
            location="${checkstyle.dir}/contrib/checkstyle-noframes.xsl"/>   选用的样式表,checkstyle.dir为jar包的位置
        <mkdir dir="${checkstyle.data.dir}"/>
        <taskdef resource="checkstyletask.properties" classpath="${checkstyle.jar}"/>  引入jar文件
        <checkstyle config="${checkstyle.dir}/sun_checks.xml"                      选用sun的规范,可以修改为自己的最佳实践
            failOnViolation="false" failureProperty="checkstyle.failure">
            <fileset dir="src">                                                                   对src目录进行检查
                <patternset refid="java.files.pattern"/>
            </fileset>
            <fileset dir="test">                                                                  对test目录进行检查
                <patternset refid="java.files.pattern"/>
            </fileset>
            <!-- uncomment to print to console as well -->
            <!--formatter type="plain"/-->
            <formatter type="xml" toFile="${checkstyle.data.file}"/>         生成xml文件
        </checkstyle>
        <xslt in="${checkstyle.data.file}" out="${checkstyle.report.file}"
            style="${checkstyle.xsl.file}"/>                                        生成报告,其格式取决于checkstyle.xsl.file
    </target>

如图所示:图1列出了所有文件,图2列出了所以错误

checkstyle1.JPG


checkstyle2.JPG



下面解释了一些常见的输出结果,以供参考。  
序号            输出内容意义    
1  Type  is  missing  a  javadoc  commentClass    缺少类型说明    
2“{”  should  be  on  the  previous  line  “{”  应该位于前一行    
3Methos  is  missing  a  javadoc  comment方法前面缺少javadoc注释    
4Expected  @throws  tag  for  “Exception”在注释中希望有@throws的说明    
5“.”  Is  preceeded  with  whitespace  “.”  前面不能有空格    
6“.”  Is  followed  by  whitespace“.”  后面不能有空格    
7“=”  is  not  preceeded  with  whitespace“=”  前面缺少空格    
8“=”  is  not  followed  with  whitespace“=”  后面缺少空格    
9“}”  should  be  on  the  same  line“}”  应该与下条语句位于同一行    
10Unused  @param  tag  for  “unused”没有参数“unused”,不需注释    
11Variable  “CA”  missing  javadoc变量“CA”缺少javadoc注释    
12Line  longer  than  80characters行长度超过80    
13Line  contains  a  tab  character行含有”tab”  字符    
14Redundant  “Public”  modifier冗余的“public”  modifier    
15Final  modifier  out  of  order  with  the  JSL  suggestionFinal  modifier的顺序错误    
16Avoid  using  the  “.*”  form  of  importImport格式避免使用“.*”    
17Redundant  import  from  the  same  package从同一个包中Import内容    
18Unused  import-java.util.listImport进来的java.util.list没有被使用    
19Duplicate  import  to  line  13重复Import同一个内容    
20Import  from  illegal  package从非法包中  Import内容    
21“while”  construct  must  use  “{}”“while”  语句缺少“{}”    
22Variable  “sTest1”  must  be  private  and  have  accessor  method变量“sTest1”应该是private的,并且有调用它的方法    
23Variable  “ABC”  must  match  pattern  “^[a-z][a-zA-Z0-9]*$”变量“ABC”不符合命名规则“^[a-z][a-zA-Z0-9]*$”    
24“(”  is  followed  by  whitespace“(”  后面不能有空格  25“)”  is  proceeded  by  whitespace“)”  前面不能有空格
25 Line has trailing spaces  行的最后不能有空格

根据sun_checks.xml文件的内容,可以到http://checkstyle.sourceforge.net/checks.html这里查看具体的配置,实现你们的最佳实践


只有注册用户登录后才能发表评论。


网站导航: