代码格式化的好处我想我就不需要说了,大家肯定也都理解其优点,否则你也不会看这个文档.
这里我以checkstyle3.4为例,讲解checkstyle的使用,确切的说是其配置的详细解释,如果你是checkstyle3.4以下,那么有一部分module将是不可以允许的,请删除之;在这份文档中我试图把所有的module都分析讲解一次,同时下面的这份文档也是我的设置,对于我个人不喜欢的设置我都注释了,如果你需要请取消注释或如何!!
checkstyle简介,其是目前最广泛使用的代码检查工具,功能强大,操作简单可以和ant结合使用,最重要的是其是open source的,你不用担心收到律师函,哈哈!
主页: http://checkstyle.sourceforge.net/
下载本文示例: olics_checkstyle_checks.zip
开始我们的讲解:
首先,checkstyle可以和ant结合使用,下面是ant脚步片断
<!--checkstyle配置,这里你替换成你实际的环境-->
<property name="checkstyle.config" value="${project.docs.dir}/checkstyle_checks.xml"/>
<property name="checkstyle.report.style" value="${project.docs.dir}/checkstyle-frames.xsl"/>
<property name="checkstyle.result" value="${build.checkstylereport.dir}/checkstyle_result.xml"/>
<property name="checkstyle.report" value="${build.checkstylereport.dir}/checkstyle_report.html"/>
<!―checkstyle脚步-->
<taskdef resource="checkstyletask.properties" classpathref=" checkstyle-all-3.4.jar"/>
<target name="checkstyle_check" depends="init">
<checkstyle config="${checkstyle.config}" failonviolation="false" failureproperty="checkstyle.failure">
<formatter type="xml" tofile="${checkstyle.result}"/>
<fileset dir="${project.src.dir}" includes="**/*.java"/>
</checkstyle>
<!―生成报告,其格式取决于${checkstyle.report.style}-->
<style in="${checkstyle.result}" out="${checkstyle.report}" style="${checkstyle.report.style}"/>
</target>
下面是我理解的 checkstyle 的使用 :
<?xml version="1.0" encoding="utf-8"?>
<!doctype module public "-//puppy crawl//dtd check configuration 1.2//en"
"http://www.puppycrawl.com/dtds/configuration_1_2.dtd">
<!-- 对于所有的模块来书,如果有这个模块则说明检测这一项,没有则不检测这一项 -->
<!-- 所有的模块中,其root必须为checker -->
<module name="checker">
<!-- 检验每个包是否存在package.html文件-->
<!-- see http://checkstyle.sf.net/config_javadoc.html#packagehtml -->
<!--
<module name="packagehtml"/>
-->
<!-- 检验每个文件末尾是否有一个空行,在unit机器上是有的,在cvs上如果没有会有警告的-->
<!-- see http://checkstyle.sf.net/config_misc.html#newlineatendoffile -->
<!--
<module name="newlineatendoffile"/>
-->
<!-- checks that property files contain the same keys. -->
<!-- see http://checkstyle.sf.net/config_misc.html#translation -->
<module name="translation"/>
<module name="treewalker">
<!-- checks for javadoc comments. -->
<!-- see http://checkstyle.sf.net/config_javadoc.html -->
<!-- checks javadoc comments for method definitions.-->
<module name="javadocmethod">
<property name="scope" value="public"/>
<!-- 是否允许错误的参数声明,true为允许,缺省为不允许 -->
<property name="allowmissingparamtags" value="true"/>
<!-- 是否允许错误的错误声明,true为允许,缺省为不允许 -->
<property name="allowmissingthrowstags" value="true"/>
<!-- 是否允许错误的返回类型声明,true为允许,缺省为不允许 -->
<property name="allowmissingreturntag" value="true"/>
</module>
<!--checks javadoc comments for class and interface definitions.-->
<module name="javadoctype"/>
<!-- checks that variables have javadoc comments.-->
<module name="javadocvariable">
<property name="scope" value="protected"/>
</module>
<!-- 检查javadoc的格式 -->
<module name="javadocstyle">
<property name="scope" value="public"/>
<!-- comment的第一句的末尾是否要有一个句号,true必须有,default为true -->
<property name="checkfirstsentence" value="false"/>
<!-- 检查错误的html脚本,比如不匹配,true检查,default为true -->
<property name="checkhtml" value="true"/>
</module>
<!-- checks for naming conventions. -->
<!-- see http://checkstyle.sf.net/config_naming.html -->
<!-- 确省必须以abstract开始或者以factory结束 -->
<!--
<module name="abstractclassname"/>
-->
<module name="constantname"/>
<module name="localfinalvariablename"/>
<module name="localvariablename"/>
<module name="membername"/>
<module name="methodname"/>
<module name="packagename"/>
<module name="parametername"/>
<module name="staticvariablename"/>
<module name="typename"/>
<!-- checks for headers -->
<!-- see http://checkstyle.sf.net/config_header.html -->
<!-- 检查文件是否以指定文件开始,这里最好是放一些版权信息和工程描述 -->
<!-- headerfile:指定的文件 -->
<!-- ignorelines:忽略哪些行,以","分隔 -->
<!--
<module name="header">
<property name="headerfile" value="java.header"/>
<property name="ignorelines" value="2, 3, 4, 5"/>
</module>
-->
<!-- following interprets the header file as regular expressions. -->
<!--
<module name="regexpheader"/>
-->
<!-- checks for imports -->
<!-- see http://checkstyle.sf.net/config_import.html -->
<!-- 检查使用*号的导入,默认为全部类 -->
<module name="avoidstarimport"/>
<!-- 检查是否有非法的包,确省检查sun.*;对于某些包是不建议直接调用的 -->
<module name="illegalimport">
<property name="illegalpkgs" value="sun.*"/>
</module>
<!-- 检查多于的导入,如一个类导入了多次 -->
<module name="redundantimport"/>
<!-- 检查没有使用的导入 -->
<module name="unusedimports"/>
<!-- 导入排序 -->
<!-- groups:分组,哪些是一组的 -->
<!-- ordered:同一个组内是否排序,true排序,确省为true -->
<!-- separated:各个组之间是否需要用空行分隔,确省为false -->
<!-- casesensitive:是否是大小写敏感的,确省是 -->
<!--
<module name="importorder">
<property name="groups" value="java,javax"/>
<property name="ordered" value="true"/>
<property name="separated" value="true"/>
<property name="casesensitive" value="true"/>
</module>
-->
<!-- checks for size violations. -->
<!-- see http://checkstyle.sf.net/config_sizes.html -->
<!-- 检查方法内可执行语句的个数,确省为30行 -->
<!--
<module name="executablestatementcount">
<property name="max" value="30"/>
</module>
-->
<!-- 文件的最大行数,缺省为1500 -->
<module name="filelength">
<property name="max" value="2000"/>
</module>
<!-- 每行的最大字符数,缺省为80 -->
<module name="linelength">
<!-- 忽略指定格式的行,如*号开始的,等 -->
<!--
<property name="ignorepattern" value="^ */* *[^ ]+$"/>
-->
<property name="max" value="120"/>
</module>
<!-- 方法的最大行数,缺省为150 -->
<module name="methodlength">
<property name="max" value="200"/>
<!-- 统计时是否包括空行和以//开始的注释,缺省为统计(true)-->
<property name="countempty" value="false"/>
</module>
<!-- 匿名类的最大行数,缺省为20 -->
<module name="anoninnerlength">
<property name="max" value="60"/>
</module>
<!-- 检查方法和构造子参数的最大个数,缺省为7 -->
<module name="parametern
闽公网安备 35060202000074号