Ant + checkstyle 实现代码检查
1 checkstyle简介
checkstyle是一个帮助开发者按照某种习惯编写java代码的工具,他实现了代码检查的自动化,帮助人们从这种繁琐的工作中解放出来。
默认提供了对sun编程规范的支持,但是checkstyle是一个具有高可配置性的,你完全可以根据自己的要求来配置需要检查的内容。
2 工具下载、安装
2.1 Ant下载
最新版本1.6.1,下载地址:http://ant.apache.org/
解压缩到c:/ant1.6.1,后面将引用为%ant_home%
2.2 cheskstyle下载
最新版本3.3,下载地址:http://checkstyle.sourceforge.net/
解压缩到c:/checkstyle3.3,后面将引用为%checkstyle_home%
3 简单配置
3.1 环境变量:
set path=%ant_home%/bin
set classpath=%checkstyle_home%/checkstyle-all-3.3.jar
3.2 build.xml文件
n 增加taskdef
<taskdef resource="checkstyletask.properties"
classpath="%checkstyle_home%\checkstyle-all-3.3.jar"/>
n 增加task
<target name="checkstyle">
<checkstyle config="sun_checks.xml" classpath="%checkstyle_home%\checkstyle-all-3.3.jar">
<fileset dir="build/classes" includes="**/*.java"/>
</checkstyle>
</target>
属性说明:
config 要使用的格式配置文件
classpath 要用到的jar文件
fileset 需要检查的文件集合
4 说明
需要检查的内容是基于module配置的,所以如果你不要检查那些module,你可以将他去掉,下面是默认的sun代码规范检查的配置文件:
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
"-//Puppy Crawl//DTD Check Configuration 1.1//EN"
"http://www.puppycrawl.com/dtds/configuration_1_1.dtd">
<!--
Checkstyle configuration that checks the sun coding conventions from:
- the Java Language Specification at
http://java.sun.com/docs/books/jls/second_edition/html/index.html
- the Sun Code Conventions at http://java.sun.com/docs/codeconv/
- the Javadoc guidelines at
http://java.sun.com/j2se/javadoc/writingdoccomments/index.html
- the JDK Api documentation http://java.sun.com/j2se/docs/api/index.html
- some best practices
Checkstyle is very configurable. Be sure to read the documentation at
http://checkstyle.sf.net (or in your downloaded distribution).
Most Checks are configurable, be sure to consult the documentation.
To completely disable a check, just comment it out or delete it from the file.
Finally, it is worth reading the documentation.
-->
<module name="Checker">
<!-- Checks that a package.html file exists for each package. -->
<!-- See http://checkstyle.sf.net/config_javadoc.html#PackageHtml -->
<module name="PackageHtml"/>
<!-- Checks whether files end with a new line. -->
<!-- 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 -->
<module name="JavadocMethod"/>
<module name="JavadocType"/>
<module name="JavadocVariable"/>
<module name="JavadocStyle"/>
<!-- Checks for Naming Conventions. -->
<!-- See http://checkstyle.sf.net/config_naming.html -->
<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 -->
<!-- <module name="Header"> -->
<!-- The follow property value demonstrates the ability -->
<!-- to have access to ANT properties. In this case it uses -->
<!-- the ${basedir} property to allow Checkstyle to be run -->
<!-- from any directory within a project. See property -->
<!-- expansion, -->
<!-- http://checkstyle.sf.net/config.html#properties -->
<!-- <property -->
<!-- name="headerFile" -->
<!-- value="${basedir}/java.header"/> -->
<!-- </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"/>
<module name="IllegalImport"/> <!-- defaults to sun.* packages -->
<module name="RedundantImport"/>
<module name="UnusedImports"/>
<!-- Checks for Size Violations. -->
<!-- See http://checkstyle.sf.net/config_sizes.html -->
<module name="FileLength"/>
<module name="LineLength"/>
<module name="MethodLength"/>
<module name="ParameterNumber"/>
<!-- Checks for whitespace -->
<!-- See http://checkstyle.sf.net/config_whitespace.html -->
<module name="EmptyForIteratorPad"/>
<module name="NoWhitespaceAfter"/>
<module name="NoWhitespaceBefore"/>
<module name="OperatorWrap"/>
<module name="ParenPad"/>
<module name="TypecastParenPad"/>
<module name="TabCharacter"/>
<module name="WhitespaceAfter"/>
<module name="WhitespaceAround"/>
<!-- Modifier Checks -->
<!-- See http://checkstyle.sf.net/config_modifiers.html -->
<module name="ModifierOrder"/>
<module name="RedundantModifier"/>
<!-- Checks for blocks. You know, those {}'s -->
<!-- See http://checkstyle.sf.net/config_blocks.html -->
<module name="AvoidNestedBlocks"/>
<module name="EmptyBlock"/>
<module name="LeftCurly"/>
<module name="NeedBraces"/>
<module name="RightCurly"/>
<!-- Checks for common coding problems -->
<!-- See http://checkstyle.sf.net/config_coding.html -->
<module name="AvoidInlineConditionals"/>
<module name="DoubleCheckedLocking"/> <!-- MY FAVOURITE -->
<module name="EmptyStatement"/>
<module name="EqualsHashCode"/>
<module name="HiddenField"/>
<module name="IllegalInstantiation"/>
<module name="InnerAssignment"/>
<module name="MagicNumber"/>
<module name="MissingSwitchDefault"/>
<module name="RedundantThrows"/>
<module name="SimplifyBooleanExpression"/>
<module name="SimplifyBooleanReturn"/>
<!-- Checks for class design -->
<!-- See http://checkstyle.sf.net/config_design.html -->
<module name="DesignForExtension"/>
<module name="FinalClass"/>
<module name="HideUtilityClassConstructor"/>
<module name="InterfaceIsType"/>
<module name="VisibilityModifier"/>
<!-- Miscellaneous other checks. -->
<!-- See http://checkstyle.sf.net/config_misc.html -->
<module name="ArrayTypeStyle"/>
<module name="FinalParameters"/>
<module name="GenericIllegalRegexp">
<property name="format" value="\s+$"/>
<property name="message" value="Line has trailing spaces."/>
</module>
<module name="TodoComment"/>
<module name="UpperEll"/>
</module>
</module>
5 如何自己配置
可以看到,xml里面的内容都是类似<module name="PackageHtml"/>这样的,这表示检查这部分的规范,置于具体的规范内容,每个module的前面都有连接地址,大家可以去看。
以<module name="PackageHtml"/>为例:
他的要求是说,每个包下面都要有个package.html文件,如果你不想检查这一项,你可以将这个元素从xml内容中删除。
分享到:
相关推荐
CheckStyle则是一款强大的代码检查工具,它能够按照预设的编码规范对Java源代码进行静态检查,找出不符合规范的代码片段,例如命名不规范、空格与缩进错误、缺少注释等问题。CheckStyle支持自定义规则集,可以适应...
在"Ant+checkstyle 实现代码检查"文档中,可能详细介绍了如何配置和运行上述Ant任务,包括错误处理和报告生成。 Checkstyle的规则覆盖了多个方面,包括命名规范(类名、变量名等)、导入语句、注释、空格和缩进、...
Ant工程中的Checkstyle是一个强大的静态代码分析工具,用于检查Java源代码是否符合预定义的编码规范和最佳实践。本篇将详细介绍如何在Ant工程中配置和使用Checkstyle进行代码检测。 首先,我们需要理解Ant的基本...
在本系列 八月份 的那期文章中,我得出了这样的结论:将检验工具集成到构建过程(例如,使用 Ant 或 Maven)中,能够建立起一种寻找潜在缺陷的方法。尽管这种方法使一致性成为可能并超越了 IDE,但它也有一点反作用...
这个"eclipse插件:代码检查CheckStyle5.1"是 CheckStyle 的一个版本,专门为 Eclipse IDE 设计,帮助开发者在编码过程中实时发现并修正不符合规范的代码,从而提升代码质量。 CheckStyle 插件的安装和配置是使用它...
6. **在Eclipse中使用Checkstyle**:Eclipse用户可以安装Checkstyle插件,实现代码编辑器内的实时检查和警告提示。插件安装、配置及使用方法都在文档中有详细步骤。 7. **常用的检查**:Checkstyle涵盖了多种检查...
CheckStyle是一款功能强大且操作简单的代码检查工具,它可以与Ant结合使用,并且是Open Source的,这意味着用户不需要担心收到律师函。CheckStyle的主要功能是检查Java代码的格式和风格,以确保代码的可读性和维护性...
6. **命令行工具**:除了IDE插件,Checkstyle 还提供了一个命令行工具,方便在构建脚本(如Ant、Maven)中集成,实现自动化检查。 7. **社区活跃**:由于 Checkstyle 社区活跃,不断有新的检查项被添加,问题和bug...
此外,Checkstyle支持通过Ant、Maven和Gradle等构建工具进行命令行检查,使得在持续集成环境中也能保证代码质量。 在提供的压缩包文件中,`p2.index`和`content.jar`可能包含了Eclipse插件的元数据和核心功能实现,...
7. **Maven和Ant集成**:对于使用Maven或Ant构建系统的项目,可以通过添加相应的插件将Checkstyle集成到构建流程中,确保每次构建时都会执行代码检查。 8. **自定义规则**:除了内置的检查,用户还可以通过实现`...
例如,如果项目需要在打包前检查代码质量,可以创建一个自定义任务来运行Checkstyle或PMD等静态代码分析工具。 在博客文章《http://blog.csdn.net/lsmfeixiang/article/details/49681583》中,作者详细解释了如何...
- **整合构建工具**:可以将CheckStyle集成到Maven或Ant等构建工具中,在构建过程中自动执行代码检查。 - **生成报告**:CheckStyle可以生成HTML或XML格式的报告,便于团队成员查看和分析代码质量问题。 #### 六、...
- Checkstyle用于检查Java代码是否符合一定的编码规范。 - 下载Checkstyle插件并配置路径。 - 在build.xml中添加Checkstyle任务。 ```xml <target name="checkstyle"> <checkstyle config="${checkstyle.config...
Checkstyle 是一个开源的代码质量检查工具,主要应用于Java项目,它可以确保代码遵循特定的编码规范和风格。Checkstyle 可以与构建工具如 Ant、Maven 或者现代的构建系统如 Gradle 集成,帮助开发者在早期发现潜在的...
StrictDuplicateCode 严格的重复代码检查**:检查重复代码,并提供删除或重构建议。 **7.13. 各种量度** - **7.13.1. 布尔表达式的复杂度**:评估布尔表达式的复杂程度。 - **7.13.2. 类数据的抽象耦合**:分析类...
- **集成构建工具**:如Maven和Ant,可以通过构建脚本运行CheckStyle检查,确保代码在构建过程中遵循规定。 - **扩展和定制**:通过创建自定义检查器,可以进一步扩展CheckStyle的功能,满足更具体的需求。 总之,...
Checkstyle 是一个著名的开源工具,主要用于静态代码分析和编码规范检查。...通过本地站点安装 Checkstyle 插件,开发者可以更灵活地管理和更新这个强大的代码检查工具,确保其始终与项目的开发需求保持同步。
Checkstyle 是一个强大的开源代码检查工具,主要用于确保代码遵循特定的编码规范和风格。它能够帮助开发者维护一致的代码质量,提高代码可读性和可维护性。Checkstyle 可以集成到开发环境中,如 Ant、Maven 或者 IDE...
Checkstyle是一款著名的开源静态代码分析工具,主要用于检查Java源代码是否符合特定的编码规范和最佳实践。这个"checkstyle5.5"版本是该工具的一个较早但稳定的发行版,适用于那些希望在项目中实施一致性和质量控制...