一个例子:
<?xml version="1.0" encoding="UTF-8"?> <project name="junit" default="checkstyle"> <property name="dir.src" location="src"/> <property name="dir.lib" location="WebContent/WEB-INF/lib"/> <property name="checkstyle.path" location="checkstyle"/> <property name="checkstyle.xml" location="${dir.src}/checkstyle_checks.xml"/> <property name="checkstyle.xsl" location="${dir.src}/checkstyle-frames.xsl"/> <path id="checkstyle.classpath"> <fileset dir="${dir.lib}"> <exclude name="junit-4.10.jar"/> <exclude name="commons-lang-2.4.jar"/> </fileset> </path> <target name="checkstyle-init"> <taskdef name="checkstyle" classname="com.puppycrawl.tools.checkstyle.CheckStyleTask" classpathref="checkstyle.classpath"/> <mkdir dir="${checkstyle.path}"/> <delete includeemptydirs="true"> <fileset dir="${checkstyle.path}"> <include name="**/*"/> </fileset> </delete> </target> <target name="checkstyle-run" depends="checkstyle-init"> <checkstyle failOnViolation="false" failureProperty="checkstyle.failed" config="${dir.src}/sun_checks.xml"> <fileset dir="${dir.src}" includes="**/*.java"/> <formatter type="plain"/> <formatter type="xml" toFile="${checkstyle.path}/checkstyle_report.xml"/> </checkstyle> </target> <target name="checkstyle-report" depends="checkstyle-run" if="checkstyle.failed"> <xslt style="${checkstyle.xsl}" in="${checkstyle.path}/checkstyle_report.xml" out="${checkstyle.path}/checkstyle_report.html"> </xslt> <fail> Checkstyle reported style failures.See ${checkstyle.path}/checkstyle_report.html </fail> </target> <target name="checkstyle" depends="checkstyle-report"/> </project>
输出:
E:\workspace\Diary>ant -f build_checkstyle.xml Buildfile: E:\workspace\Diary\build_checkstyle.xml checkstyle-init: checkstyle-run: [checkstyle] Running Checkstyle 5.6 on 2 files [checkstyle] E:\workspace\Diary\src\diary\core\entity\Diary.java:0: Missing pack age-info.java file. [checkstyle] E:\workspace\Diary\src\diary\core\entity\Diary.java:9: Missing a Ja vadoc comment. [checkstyle] E:\workspace\Diary\src\diary\core\entity\Diary.java:11:1: File cont ains tab characters (this is the first instance). [checkstyle] E:\workspace\Diary\src\diary\core\entity\Diary.java:11:9: Missing a Javadoc comment. [checkstyle] E:\workspace\Diary\src\diary\core\entity\Diary.java:13:9: Missing a Javadoc comment. [checkstyle] E:\workspace\Diary\src\diary\core\entity\Diary.java:14:9: Missing a Javadoc comment. [checkstyle] E:\workspace\Diary\src\diary\core\entity\Diary.java:15:9: Missing a Javadoc comment. [checkstyle] E:\workspace\Diary\src\diary\core\entity\Diary.java:16:9: Missing a Javadoc comment. [checkstyle] E:\workspace\Diary\src\diary\core\entity\Diary.java:18:9: Method 'g etId' is not designed for extension - needs to be abstract, final or empty. [checkstyle] E:\workspace\Diary\src\diary\core\entity\Diary.java:18:9: Missing a Javadoc comment. [checkstyle] E:\workspace\Diary\src\diary\core\entity\Diary.java:22:9: Method 's etId' is not designed for extension - needs to be abstract, final or empty. [checkstyle] E:\workspace\Diary\src\diary\core\entity\Diary.java:22:9: Missing a Javadoc comment. [checkstyle] E:\workspace\Diary\src\diary\core\entity\Diary.java:22:27: Paramete r id should be final. [checkstyle] E:\workspace\Diary\src\diary\core\entity\Diary.java:22:34: 'id' hid es a field. [checkstyle] E:\workspace\Diary\src\diary\core\entity\Diary.java:26:9: Method 'g etName' is not designed for extension - needs to be abstract, final or empty. [checkstyle] E:\workspace\Diary\src\diary\core\entity\Diary.java:26:9: Missing a Javadoc comment. [checkstyle] E:\workspace\Diary\src\diary\core\entity\Diary.java:30:9: Method 's etName' is not designed for extension - needs to be abstract, final or empty. [checkstyle] E:\workspace\Diary\src\diary\core\entity\Diary.java:30:9: Missing a Javadoc comment. [checkstyle] E:\workspace\Diary\src\diary\core\entity\Diary.java:30:29: Paramete r name should be final. [checkstyle] E:\workspace\Diary\src\diary\core\entity\Diary.java:30:36: 'name' h ides a field. [checkstyle] E:\workspace\Diary\src\diary\core\entity\Diary.java:34:9: Method 'g etCreateTime' is not designed for extension - needs to be abstract, final or emp ty. [checkstyle] E:\workspace\Diary\src\diary\core\entity\Diary.java:34:9: Missing a Javadoc comment. [checkstyle] E:\workspace\Diary\src\diary\core\entity\Diary.java:38:9: Method 's etCreateTime' is not designed for extension - needs to be abstract, final or emp ty. [checkstyle] E:\workspace\Diary\src\diary\core\entity\Diary.java:38:9: Missing a Javadoc comment. [checkstyle] E:\workspace\Diary\src\diary\core\entity\Diary.java:38:35: Paramete r createTime should be final. [checkstyle] E:\workspace\Diary\src\diary\core\entity\Diary.java:38:40: 'createT ime' hides a field. [checkstyle] E:\workspace\Diary\src\diary\core\entity\Diary.java:42:9: Method 'g etContent' is not designed for extension - needs to be abstract, final or empty. [checkstyle] E:\workspace\Diary\src\diary\core\entity\Diary.java:42:9: Missing a Javadoc comment. [checkstyle] E:\workspace\Diary\src\diary\core\entity\Diary.java:46:9: Method 's etContent' is not designed for extension - needs to be abstract, final or empty. [checkstyle] E:\workspace\Diary\src\diary\core\entity\Diary.java:46:9: Missing a Javadoc comment. [checkstyle] E:\workspace\Diary\src\diary\core\entity\Diary.java:46:32: Paramete r content should be final. [checkstyle] E:\workspace\Diary\src\diary\core\entity\Diary.java:46:39: 'content ' hides a field. [checkstyle] E:\workspace\Diary\src\diary\core\entity\Diary.java:50:9: Method 't oString' is not designed for extension - needs to be abstract, final or empty. [checkstyle] E:\workspace\Diary\src\diary\core\entity\Diary.java:52: Line is lon ger than 80 characters (found 145). [checkstyle] E:\workspace\Diary\src\diary\core\service\DiaryService.java:0: Miss ing package-info.java file. [checkstyle] E:\workspace\Diary\src\diary\core\service\DiaryService.java:7: Miss ing a Javadoc comment. [checkstyle] E:\workspace\Diary\src\diary\core\service\DiaryService.java:9:1: Fi le contains tab characters (this is the first instance). [checkstyle] E:\workspace\Diary\src\diary\core\service\DiaryService.java:9:9: Me thod 'list' is not designed for extension - needs to be abstract, final or empty . [checkstyle] E:\workspace\Diary\src\diary\core\service\DiaryService.java:9:9: Mi ssing a Javadoc comment. [checkstyle] E:\workspace\Diary\src\diary\core\service\DiaryService.java:13:9: M ethod 'get' is not designed for extension - needs to be abstract, final or empty . [checkstyle] E:\workspace\Diary\src\diary\core\service\DiaryService.java:13:9: M issing a Javadoc comment. [checkstyle] E:\workspace\Diary\src\diary\core\service\DiaryService.java:13:26: Parameter name should be final. [checkstyle] E:\workspace\Diary\src\diary\core\service\DiaryService.java:17:9: M ethod 'save' is not designed for extension - needs to be abstract, final or empt y. [checkstyle] E:\workspace\Diary\src\diary\core\service\DiaryService.java:17:9: M issing a Javadoc comment. [checkstyle] E:\workspace\Diary\src\diary\core\service\DiaryService.java:17:25: Parameter diary should be final. [checkstyle] E:\workspace\Diary\src\diary\core\service\DiaryService.java:21:9: M ethod 'update' is not designed for extension - needs to be abstract, final or em pty. [checkstyle] E:\workspace\Diary\src\diary\core\service\DiaryService.java:21:9: M issing a Javadoc comment. [checkstyle] E:\workspace\Diary\src\diary\core\service\DiaryService.java:21:27: Parameter diary should be final. [checkstyle] E:\workspace\Diary\src\diary\core\service\DiaryService.java:25:9: M ethod 'delete' is not designed for extension - needs to be abstract, final or em pty. [checkstyle] E:\workspace\Diary\src\diary\core\service\DiaryService.java:25:9: M issing a Javadoc comment. [checkstyle] E:\workspace\Diary\src\diary\core\service\DiaryService.java:25:27: Parameter id should be final. [checkstyle] E:\workspace\Diary\src\diary\core\service\DiaryService.java:29:9: M ethod 'delete' is not designed for extension - needs to be abstract, final or em pty. [checkstyle] E:\workspace\Diary\src\diary\core\service\DiaryService.java:29:9: M issing a Javadoc comment. [checkstyle] E:\workspace\Diary\src\diary\core\service\DiaryService.java:29:27: Parameter diary should be final. checkstyle-report: [xslt] Processing E:\workspace\Diary\checkstyle\checkstyle_report.xml to E: \workspace\Diary\checkstyle\checkstyle_report.html [xslt] Loading stylesheet E:\workspace\Diary\src\checkstyle-frames.xsl BUILD FAILED E:\workspace\Diary\build_checkstyle.xml:43: Checkstyle reported style failures.S ee E:\workspace\Diary\checkstyle/checkstyle_report.html Total time: 1 second
测试中用到的项目在附件中,里面包含了运行后的输出内容,项目很简单。
Checkstyle官网关于如何在Ant中使用Checkstyle
Description
This task runs Checkstyle over specified Java files. The task has been tested using ANT 1.5. The latest version of checkstyle can be found athttp://checkstyle.sourceforge.net/. This task is included in the checkstyle distribution.
Installation
The easiest way is to include checkstyle-5.6-all.jar in the classpath. This contains all the classes required to run Checkstyle. Alternatively, you must include the compile third party dependencies listed in Project Dependencies in the classpath.
To use the task in a build file, you will need the following taskdef declaration:
<taskdef resource="checkstyletask.properties" classpath="/path/to/checkstyle-5.6-all.jar"/>
Or, assuming that Checkstyle is in the global classpath (not recommended), then you will need the following taskdef declaration:
<taskdef resource="checkstyletask.properties"/>
Or if you use Ant 1.6 and later and assuming that Checkstyle is in the library search path, then you may use antlib feature of Ant (seehttp://ant.apache.org/manual/Types/antlib.html for more details). For example:
<project name="foo" ... xmlns:cs="antlib:com.puppycrawl.tools.checkstyle"> ... <cs:checkstyle> ... </cs:checkstyle> ... </project>
Parameters
Attribute | Description | Required |
file | File to run checkstyle on. | One of either fileor at least one nested filesetelement |
config | Specifies the location of the file that defines the configuration modules. The location can either be a filesystem location, or a name passed to the ClassLoader.getResource() method. See here for a description of how to define a configuration. |
Exactly one of config or configURL |
configURL | Specifies a URL that defines the configuration modules. See here for a description of how to define a configuration. | Exactly one of config or configURL |
properties | Specifies a file that contains properties for expanded property values of the configuration. Ant properties (like ${basedir}) and nested property elements override the properties in this file. | No |
failOnViolation | Specifies whether the build will continue even if there are violations. Defaults to "true". | No |
failureProperty | The name of a property to set in the event of a violation. | No |
maxErrors | The maximum number of errors that are tolerated before breaking the build or setting the failure property. Defaults to "0". | No |
maxWarnings | The maximum number of warnings that are tolerated before breaking the build or setting the failure property. Defaults to "2147483647", i.e. Integer.MAX_VALUE. | No |
classpath | The classpath to use when looking up classes. Defaults to the current classpath. | No |
classpathref | The classpath to use when looking up classes, given as a reference to a path defined elsewhere. | No |
omitIgnoredModules | For efficiency, Checkstyle does not invoke modules with a configured severity of "ignore" (since their output would be ignored anyway). A small number of modules may choose to log above their configured severity level and so always need to be invoked. This settings specifies that behaviour. Defaults to "true". | No |
Note that the packageNamesFile parameter has been dropped for Checkstyle 5.0, because of significant changes regarding package name file handling. Seefor details.
Nested Elements
This task supports the nested elements <fileset>, <classpath>, <formatter>, and <property>.
The parameters for the <formatter> element are:
Attribute | Description | Required |
type |
The type of output to generate. The valid values are:
Defaults to "plain". |
No |
toFile | The file to write output to. Defaults to standard output. Note, there is no way to explicitly specify standard output. | No |
useFile | Boolean that determines whether output should be sent to a file. Default is true. | No |
A <property> element provides a property for expanded property values of the configuration. The parameters for the <property> element are:
Attribute | Description | Required |
key |
The key for the property. |
Yes |
value | The value of the property specified as a string. | Either value or file |
file | The value of the property specified as a file. This is great for specifying file names relative to the ANT build file. | Either value or file |
Examples
Run checkstyle with configuration file docs/sun_checks.xml on a single file
<checkstyle config="docs/sun_checks.xml" file="Check.java"/>
Run checkstyle on a set of Java files using site-wide configuration and an expanded property value
<checkstyle config="/path/to/site/sun_checks.xml"> <fileset dir="src/checkstyle" includes="**/*.java"/> <!-- Location of cache-file. Something that is project specific --> <property key="checkstyle.cache.file" file="target/cachefile"/> </checkstyle>
Run checkstyle on a set of files and output messages to standard output in plain format, and a file in XML format
<checkstyle config="docs/sun_checks.xml"> <fileset dir="src/checkstyle" includes="**/*.java"/> <formatter type="plain"/> <formatter type="xml" toFile="build/checkstyle_errors.xml"/> </checkstyle>
Run checkstyle with configuration file docs/sun_checks.xml on a file and provide a package names file
<checkstyle config="docs/sun_checks.xml" packageNamesFile="myPackageNames.xml" file="Check.java"/>
Run checkstyle in an automated build and send an email report if style violations are detected
<target name="checkstyle" description="Generates a report of code convention violations."> <checkstyle config="docs/sun_checks.xml" failureProperty="checkstyle.failure" failOnViolation="false"> <formatter type="xml" tofile="checkstyle_report.xml"/> <fileset dir="src" includes="**/*.java"/> </checkstyle> <style in="checkstyle_report.xml" out="checkstyle_report.html" style="checkstyle.xsl"/> </target> <!-- run this target as part of automated build --> <target name="checkstyle-nightly" depends="checkstyle" if="checkstyle.failure" description="Sends email if checkstyle detected code conventions violations."> <!-- use your own server and email addresses below. See Ant documentation for details --> <mail from="qa@some.domain" tolist="someone@some.domain,someoneelse@some.domain" mailhost="mailbox.some.domain" subject="Checkstyle violation(s) in project ${ant.project.name}" files="checkstyle_report.html"/> </target>
相关推荐
Ant工程checkstyle编写(代码检测):注意附件中的注意事项,如果在实施过程中提示没有找到配置文件,请查看自己的build文件是否书写规范(是否没有执行到对应的target)
checkStyle java代码检查工具
代码检查 CheckStyle-IDEA,可以本地加载该插件,避免因为墙的原因长时间无法下载带来的困扰。
CheckStyle 代码优化工具 代码格式 java格式
eclipse_checkstyle代码规范安装,eclipse_checkstyle代码规范安装
无毒 无污染 CheckStyle代码审核绿色版本 内有使用说明文档 绝对好使 -_-||
checkstyle代码检查器
Eclipse\CheckStyle代码风格配置文件
在本系列 八月份 的那期文章中,我得出了这样的结论:将检验工具集成到构建过程(例如,使用 Ant 或 Maven)中,能够建立起一种寻找潜在缺陷的方法。尽管这种方法使一致性成为可能并超越了 IDE,但它也有一点反作用...
NULL 博文链接:https://zw7534313.iteye.com/blog/2263657
checkstyle代码编辑规范
CheckStyle代码检查工具使用说明
Java代码规范、格式化和checkstyle检查配置文档
NULL 博文链接:https://wobfei.iteye.com/blog/707789
成员聊天:checkstyle,checkstyle是一个开发工具,可以帮助程序员编写遵循编码标准的java代码。默认情况下,它支持google java风格指南和sun代码约定,但高度可配置。它可以通过ant任务和命令行程序调用。.zip
Myeclipse checkstyle5.3 代码格式走查,规范代码格式,开发必备。特别推荐。
Ant详细使用,包括调用CMD,checkstyle,FTP,PMD
包括Checkstyle在Eclipse中和Maven中的配置详解,下了绝不后悔,一文在手,开发不愁
Checkstyle压缩包和使用说明,Checkstyle是规范代码编写格式的工具类。