Ant下使用checkstyle全攻略

来源:配置管理之路( http://bbs.scmroad.com ) 作者:laofo

本来,我以为在ant下配置checkstyle是很容易的事情.结果却并非我想象. 下面是一些提示,和一些问题的排错过程.

Checkstyle项目主页:
http://checkstyle.sourceforge.net


先看一下checkstyle的概述
Checkstyle is a development tool to help programmers write Java code that adheres to a coding standard. It automates the process of checking Java code to spare humans of this boring (but important) task. This makes it ideal for projects that want to enforce a coding standard.
Checkstyle is highly configurable and can be made to support almost any coding standard. An example configuration file is supplied supporting the Sun Code Conventions. As well, other sample configuration files are supplied for other well known conventions.
简单的说就是检查代码规范的.详细的就看主页了: P


下面我们讲怎么在Ant下使用checkstyle.
1. 到checkstyle主页下载checkstyle的安装包
2. 解压之后我们会看到很多文件
其中
checkstyle-5.0-beta01.jar
checkstyle-all-5.0-beta01.jar
是运行checkstyle不可缺少的
sun_checks.xml
是sun公司的代码规范(我们暂时可以利用sun公司的代码规范检查自己的代码,当然我们也可以针对各个公司的不同标准进行定制)
我们还能在contrib目录下看到好多的xsl文件
选择一个就好了,这些文件是为了正确显示检查后的结果用的. 它们规定了怎么去显示检查结果, 样式表.
我选择的是/contrib/checkstyle-frames.xsl 这个文件,把它重命名为checkstyle.xsl, 然后
所以我们得到了四个文件


  • checkstyle-5.0-beta01.jar
  • checkstyle-all-5.0-beta01.jar
  • sun_checks.xml
  • /contrib/checkstyle-frames.xsl



我们还是选取Cruisecontrol自带的项目conneectfour.当然你也可以选择其它的项目.
3. 我们把上边提到的两个jar包放到项目的lib下面
/lib/checkstyle-5.0-beta01.jar
/lib/checkstyle-all-5.0-beta01.jar

4 设置检查规范
在connectfour根目录下建立一个docs的目录,然后把sun_checks.xml放进去
/docs/sun_checks.xml

5 修改build.xml,加入checkstyle任务
我们打开这个项目的构建文件build.xml
加入一下代码
<property name="checkstyle.dir"
value="${basedir}\lib"/>
<path id="checkstyle.dir">
<fileset dir="${checkstyle.dir}">
<include name="*.jar"/>
</fileset>
</path>
<taskdef name="checkstyle"
classname="com.puppycrawl.tools.checkstyle.CheckStyleTask" classpathref="checkstyle.dir" />
<target name="checkstyle" depends="clean, update,compile,test"
description="Generates a report of code convention violations.">
<mkdir dir="target/check-results"/>
<checkstyle config="docs/sun_checks.xml"
failureProperty="checkstyle.failure"
failOnViolation="false">
<formatter type="xml" tofile="target/check-results/checkstyle_report.xml"/>
<fileset dir="src" includes="**/*.java"/>
</checkstyle>
<style in="target/check-results/checkstyle_report.xml" out="target/check-results/checkstyle_report.html" style="checkstyle.xsl"/>
</target>


6 运行checkstyle 任务
我们在这个项目的根目录下运行ant checkstyle,如果没有错误,证明checkstyle成功运行,这个时候会有一个checkstyle_report.html在target/check-results目录下产生.我们可以用浏览器打开这个html文件就会看到这次检查的结果.


FAQ:

如果你能一次运行成功,那当然很幸运了,可是我在搭建的时候遇到了很多错误.我把我如何解决的记录下来和大家分享.甚至有的错误现在还没有解决,只是采取了一些折衷的方法.

问题1
D:\DevEnv\CruiseControl\projects\connectfour-svn\build.xml:77: Only one of the attributes name, file and resource can be set

如果我们这个样子定义checkstyle就会产生上边那个错误
<taskdef resource="checkstyletask.properties" classpath="./lib/checkstyle-all-5.0-beta01.jar" />

不知道什么原因.等以后找到为什么了再来补上.


<taskdef name="checkstyle"
classname="com.puppycrawl.tools.checkstyle.CheckStyleTask" classpathref="checkstyle.dir" />
定义成这个样子也不行.所以我借鉴了我定义svnant的方式,如下定义了checkstyle,终于能通过了.思路就是先定义checkstyle的目录,然后再引入.这样做就可以了.

<property name="checkstyle.dir"
value="${basedir}\lib"/>
<path id="checkstyle.dir">
<fileset dir="${checkstyle.dir}">
<include name="*.jar"/>
</fileset>
</path>
<!--
<taskdef name="checkstyle"
classname="com.puppycrawl.tools.checkstyle.CheckStyleTask" classpathref="checkstyle.dir" />
-->
<taskdef name="checkstyle"
classname="com.puppycrawl.tools.checkstyle.CheckStyleTask" classpathref="checkstyle.dir" />



问题2:
D:\DevEnv\CruiseControl\projects\connectfour>ant checkstyle
Buildfile: build.xml
checkstyle:
BUILD FAILED
D:\DevEnv\CruiseControl\projects\connectfour\build.xml:75: Unable to create a Checker: cannot initialize module PackageHtml - Unable to instantiate PackageHtml

Total time: 0 seconds


我google这个错误的信息,得到以下的一些帮助内容.
http://checkstyle.sourceforge.net/5.x/config_javadoc.html#JavadocPackage

JavadocPackage
Description

Checks that each Java package has a Javadoc comment. By default it only allows a package-info.java file, but can be configured to allow a package.html file.
An error will be reported if both files exist as this is not allowed by the Javadoc tool.


经上边的提示,我打开sun_checks.xml文件
把检查package.html文件是否存在的一句给注释掉了.
<!-- Checks that a package.html file exists for each package. -->
<!-- See http://checkstyle.sf.net/config_javadoc.html#PackageHtml -->


<!--注释掉下边这句

<module name="PackageHtml"/>
-->

问题3
nt最简单的构建已经成功,但在命令行运行ant -verbose时出现如下内容:
[antlib.org.apache.tools.ant]Could not load definitions from resource org/apache/tools/ant/antlib.xml.It could not be found.

Baidu后得来的一个土方法
[此问题的推荐答案]
1. Copied the ant.jar to any new/temp folder.
2. Extracted the jar using jar xvf <jar name> to the same folder.
3. Copied the file antlib.xml from
"org/apache/tools/ant/types/conditions/antlib.xml" to
org/apache/tools/ant/antlib.xml.
4. Then updated the ant.jar with the file "org/apache/tools/ant/antlib.xml"
5. Placed the updated ant.jar to the $ANT_HOME/lib
这个办法比较野蛮,但能解决问题

内容可以在下面的链接找到:
http://bbs.zdnet.com.cn/viewthread.php?tid=681507
http://zhidao.baidu.com/question/67170892.html


经过此篇文章,我们又向持续集成迈了一步.下一步,我们就是在Cruisecontrol中把checkstyle的结果发布出去.
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值