使用 ant 让你愉快编程(4)

摘要: checkstyle_checks.xml, java.header

现在已经有了目录结构, 有了 build_common.xml, common.xml, usage.txt,
build.xml, build.properties 等文件, 已经可以完成大部分任务了.
现在介绍如何进行辅助任务, 比如代码检查.

代码检查在 build_common.xml 中已经有这个任务了, 但是还需要两个配置
文件配合, 还需要到 这里下载 checkstyle 的 jar 包并将
它放到 work/common/lib/ 目录下.

这两个配置文件是: work/common/template/build/ 目录下的
checkstyle_checks.xml 文件和 java.header 文件
checkstyle_checks.xml 文件说明了对 java 文件应该如何进行检查,
java.header 文件指出每个 java 文件的头部构造.

. checkstyle_checks.xml 文件如下, 是借用的 Johan
的源文件, 作了一点儿修改. 其中每项配置的具体说明都有给出链接, 不明白的
话可以仔细看看.

<?xml version="1.0" encoding="iso-8859-1"?>
<!--
    Copyright 2004 Johan K�ng�d, http://dev.kanngard.net

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
-->
<!DOCTYPE module PUBLIC
    "-//Puppy Crawl//DTD Check Configuration 1.2//EN"
    "http://www.puppycrawl.com/dtds/configuration_1_2.dtd">
<!--
  Based on the Checkstyle configuration file sun_checks.xml with some minor
  modifications..
  Checkstyle is very configurable. Be sure to read the documentation at
  http://checkstyle.sf.net
-->
<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">
            <property name="format" value="^[_A-Z][a-zA-Z0-9]*$"/>
        </module>
        <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}/build/java.header"/>
            <property name="ignoreLines" value="3,5,8"/>
        </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="MethodParamPad"/>
        <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">
            <property name="tokens" value="VARIABLE_DEF"/>
        </module>

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


. 以下是我的 java 文件头部构造, 当然每个团队可以有自己的约定.
因为第3,5,8行是随文件的改变而改变的, 因此我忽略了对他们的检查,
这一点可以从上面的 checkstyle_checks.xml 文件中看出来.

/*
 * -----------------------------------------------------------
 * file name  : _filename_
 * authors    : camry(camry@gmail.com)
 * created    : _datetime_
 * copyright  : (c) 2003 Vitular Inc. All Rights Reserved.
 *
 * modifications:
 *
 * -----------------------------------------------------------
 */

有的朋友可能觉得每次都在文件前写这么一个头岂不是很麻烦, 但是对于我来说,
这一点非常容易就可以让 vim 做到了. 当我用 vim 新建一个 java 文件时,
它会自动加入这个头部说明, 并用适当的文件名和时间替换 _filename_ 和
_datetime_ 这两个参数. 如何做到这一点将来会做说明.

使用这个 checkstyle 时会有这么一个麻烦的地方: 它不允许在行尾有多余的
空格. 对于程序员来说, 谁在乎这么几个空格呢, 可是程序是非常严谨的:)
使用vim的朋友可以在这里发现如何轻松消除行尾空格.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值