清單 1. 將 CheckStyle 用于 Ant
<taskdef resource="checkstyletask.properties" classpath="${checkstyle.jar}"/> <checkstyle config ="${basedir}/cs-rules.xml" failOnViolation="false"> <formatter toFile="${checkstyle.data.file}" type="xml" /> <fileset casesensitive="yes" dir="${src.dir}" includes="**/*.java" /> </checkstyle> <xslt taskname="checkstyle" in="${checkstyle.data.file}" out="${checkstyle.report.file}" style="${checkstyle.xsl.file}" /> |
config
被設置成 cs-rules.xml
,這表示將根據源代碼目錄(在 fileset dir
屬性中)以遞歸的方式運行規則文件。xslt
任務接受根據 formatter toFile
屬性生成的文件。該任務使用 XSL 文件 checkstyle.xsl 將清單 1 生成的 XML 轉換成一個可讀的 HTML 文件,XSL 文件 checkstyle.xsl 包含在 CheckStyle 安裝文件中。
清單 2 是 CheckStyle 規則文件的示例片段。CheckStyle 可以在代碼基上運行 120 多個規則。