`

请您先登录,才能继续操作

Ant 用CheckStyle审查代码

    博客分类:
  • Ant
 
阅读更多

 一个例子:

<?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

<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>
      
 
 
分享到:
评论

相关推荐

    eclipse插件:代码检查CheckStyle5.1

    CheckStyle 是一个非常重要的开源工具,它主要用于检查 Java 代码是否符合特定的编码规范和风格。...通过熟练使用 CheckStyle,开发者能够编写出更优雅、更易于理解和维护的代码,从而提升整个项目的品质。

    checkstyle中文帮助文档

    5. **在Ant中使用Checkstyle**:Ant是Java项目常用的构建工具,Checkstyle可以通过Ant任务进行集成。配置包括定义Checkstyle任务,指定配置文件,然后运行任务检查代码。 6. **在Eclipse中使用Checkstyle**:...

    checkstyle 教程

    - **集成构建工具**:如Maven和Ant,可以通过构建脚本运行CheckStyle检查,确保代码在构建过程中遵循规定。 - **扩展和定制**:通过创建自定义检查器,可以进一步扩展CheckStyle的功能,满足更具体的需求。 总之,...

    Checkstyle for Eclipse 插件,本地站点安装

    4. **集成构建**:除了在 IDE 中使用,Checkstyle 也可以作为 Maven 或 Ant 构建的一部分,确保代码在提交前就满足规范要求。 在实际开发过程中,Checkstyle 不仅有助于提升代码质量,减少代码缺陷,还能提高团队...

    checkStyle的使用.zip

    开发者可以将Checkstyle集成到构建脚本(如Ant或Maven)中,作为持续集成的一部分,确保每次提交的代码都符合规定的编码标准。 除了Eclipse,Checkstyle还可以在其他IDE如IntelliJ IDEA、NetBeans中使用,甚至可以...

    基于ant脚本的代码质量评价的研究与应用.doc

    3. 代码审查与重构:通过ANT,可以自动化执行CodeReview工具,帮助团队进行代码审查,促进代码风格的一致性,同时也能辅助代码重构过程。 4. 性能测试与功能测试:ANT可以与性能测试工具(如JMeter)和功能测试工具...

    Checkstyle 使用手册

    在Ant构建脚本中,可以使用类似以下的代码片段来运行Checkstyle任务: ```xml &lt;taskdef resource="checkstyletask.properties" classpathref="checkstyle-all-3.4.jar"/&gt; &lt;target name="checkstyle_check" depends=...

    checkstye 最新 checkstyle5.6

    6. **与其他工具集成**:除了Eclipse,Checkstyle还可以与Maven、Ant等构建工具集成,实现持续集成环境下的代码质量检查。 7. **规则详解**:Checkstyle 5.6包含多种检查规则,例如: - *Whitespace*:检查空格和...

    编码规范化插件checkStyle

    #### 五、在Ant中使用Checkstyle ##### 5.1 N分钟极速入门 - **安装Checkstyle**:确保已经正确安装了Checkstyle。 - **编写build.xml**:创建一个Ant构建脚本来运行Checkstyle。 - **运行Ant任务**:通过命令行...

    CheckStyle的安装与配置[归纳].pdf

    总的来说,CheckStyle 是一款强大的 Java 代码质量检查工具,通过安装、配置和使用 CheckStyle,开发团队可以遵循一致的编码规范,提升项目的整体质量。对于大型项目或多人协作的项目,引入 CheckStyle 更是必不可少...

    代码走查工具介绍

    在软件开发过程中,代码质量是决定项目成功与否的关键因素之一...在实际操作中,可以先运行CheckStyle来统一代码风格,然后使用PMD检查代码质量,最后通过FindBugs查找可能的运行时错误,形成一个完整的代码审查流程。

    JAVA代码复查工具使用指南.pdf

    具体处理流程请参照部门提供的《代码复查Checklist.xls》(Code Review Checklist-Java)文档中的“使用checkstyle工具进行编码规范检查,检查项一览”与“使用findbugs工具进行代码质量检查,检查项一览”章节。...

    轻松有效检查Java代码的三个工具

    使用这些工具的好处在于,它们能够显著减少手动代码审查的工作量,提高代码的整洁性和一致性。通过与IDE的集成,如NetBeans、Eclipse、jEdit和JDeveloper,开发者可以直接在编辑器中定位并修复错误,提高了工作效率...

    让开发自动化-持续检查

    - **集成与使用**:CheckStyle可以通过其提供的Ant任务或Maven插件轻松集成到构建过程中。例如,在Ant构建脚本中,可以配置`failOnViolation`属性来决定在遇到编码标准偏离时是否中断构建。 ```xml ${basedir}/...

    pmd-bin-5.2.3

    - PMD支持Ant和Jenkins等持续集成工具,可以自动化代码审查流程。 - 通过PMD的API,开发者可以开发自定义的代码分析模块,实现更深度的代码质量控制。 总的来说,"pmd-bin-5.2.3"是一个用于代码质量检测的实用...

    sf-build-scripts:为 Salesforce 项目构建和部署脚本

    6. **代码格式化与静态代码分析**:为了保持代码风格一致性和提高代码质量,sf-build-scripts 可以进行代码格式化和静态代码分析,如使用 Pmd 或 Checkstyle。 三、使用 sf-build-scripts 要开始使用 sf-build-...

    25个让Java程序员更高效的Eclipse插件.txt

    - **概述**:PMD 是一款流行的Java代码审查工具,它可以检测常见的编程缺陷和潜在的问题。 - **特点**: - 检测诸如死循环、未使用的变量等常见的编程错误。 - 支持自定义规则,可以适应不同的项目需求。 - 提供...

Global site tag (gtag.js) - Google Analytics