Checkstyle ANT Task - Version 2.3

Description

This task runs Checkstyle over specified Java files. The task has been tested using ANT 1.4.1. The latest version of checkstyle can be found at http://checkstyle.sourceforge.net/. This task is included in the checkstyle distribution.

Installation

The easiest way is to include checkstyle-all-2.3.jar in the classpath. This contains all the classes required to run Checkstyle. Alternatively, you must include the following in the classpath:

  1. checkstyle-2.3.jar
  2. ANTLR 2.7.1 classes. antlr.jar is included in the distribution.
  3. Jakarta Regexp 1.2 classes. jakarta-regexp-1.2.jar is included in the distribution.

To use the task in a build file, you will need the following taskdef declaration:

  <taskdef name="checkstyle"
           classname="com.puppycrawl.tools.checkstyle.CheckStyleTask"/>

Alternatively, since checkstyle version 2.2 you can use taskdef's resource attribute:

  <taskdef resource="checkstyletask.properties"/>

Parameters

Attribute Description Required
file File to run checkstyle on. One of either file or at least one nested fileset element
properties Specifies a properties file that contains the configuration options. This is useful for sharing checkstyle configurations across ant build files. The property names to use in the file are the same as on the command line. Other parameters to the ANT task override the settings in the properties file. No
lcurlyType Specifies the policy of where to put the left brace '{' for type declarations. The legal values are defined here. Defaults to "eol". No
lcurlyMethod Specifies the policy of where to put the left brace '{' for method declarations. The legal values are defined here. Defaults to "eol". No
lcurlyOther Specifies the policy of where to put the left brace '{' for other declarations. The keywords covered is defined here. The legal values are defined here. Defaults to "eol". No
rcurly Specifies the policy of where to put the right brace '}'. The keywords covered is defined here. The legal values are defined here. Defaults to "same". No
parenPad Specifies the policy of how to pad parenthesises. The legal values are defined here. Defaults to "nospace". No
tryBlock Specifies the policy of how check try blocks. The legal values are defined here. Defaults to "stmt". No
catchBlock Specifies the policy of how check catch blocks. The legal values are defined here. Defaults to "text". No
finallyBlock Specifies the policy of how check finally blocks. The legal values are defined here. Defaults to "stmt". No
wrapOp Specifies the policy of how to wrap on operators. The legal values are defined here. Defaults to "nl". No
allowTabs Indicates whether to allow tabs. Defaults to "false". No
allowProtected Indicates whether to allow protected data. Defaults to "false". No
allowPackage Indicates whether to allow package visible data. Defaults to "false". No
allowNoAuthor Indicates whether to allow no @author tag to be defined for class and interface Javadoc comments. Defaults to "false". No
maxLineLen Specifies the maximum line length. Default value is defined here. No
tabWidth The distance between tab stops, used in line number and column calculations. Default value is "8". No
ignoreLineLengthPattern Specifies a regular expression for lines to ignore in maximum line length checks. Defaults to "^$". No
ignoreImportLen Specifies whether to ignore the maximum line length for import statements. Defaults to "false". No
maxMethodLen Specifies the maximum method length. Default value is defined here. No
maxConstructorLen Specifies the maximum constructor length. Default value is defined here. No
maxFileLen Specifies the maximum file length. Default value is defined here. No
todoPattern Specifies the regular expression to match for to-do comments. Default value is defined here. No
memberPattern Specifies the regular expression to match against member variables. Default value is defined here. No
publicMemberPattern Specifies the regular expression to match against public member variables. Default value is defined here. No
paramPattern Specifies the regular expression to match against parameters. Default value is defined here. No
constPattern Specifies the regular expression to match against static/final variables. Default value is defined here. No
staticPattern Specifies the regular expression to match against static variables. Default value is defined here. No
typePattern Specifies the regular expression to match against type names. Default value is defined here. No
methodPattern Specifies the regular expression to match against method names. Default value is defined here. No
localVarPattern Specifies the regular expression to match against local variable names. Default value is defined here. No
headerFile Specifies the file containing the header lines. Default is to not check. No
headerLinesRegexp Specifies whether to interpret each line in the headerFile as a regular expression. Default is "false". No
headerIgnoreLine Specifies a comma separated list of the lines in the header to ignore when comparing. Default is to not ignore any line. No
javadocScope Specifies the visibility scope where javadoc comments are checked. Valid values are "nothing", "public", "protected", "package", "private" and "anoninner". Defaults to "private". No
requirePackageHtml Specifies whether to require that package documentation is available. Defaults to "false". No
ignoreImports Specifies whether to ignore checking import statements. Defaults to "false". No
illegalImports Comma separated list of package prefixes that are not allowed in import statements. Defaults to "sun". No
illegalInstantiations Comma separated list of fully qualified class names that are not allowed to be instantiated. Defaults to "". No
ignoreWhitespace Specifies whether to ignore checking whitespace. Defaults to "false". No
ignoreCastWhitespace Specifies whether to ignore checking for whitespace after a cast. Defaults to "false". No
ignoreBraces Specifies whether to ignore checking braces. Defaults to "false". No
ignoreLongEll Specifies whether to ignore checking the L in long integer literals. Defaults to "false". No
ignorePublicInInterface Specifies whether to ignore the public keyword in interface definitions. Defaults to "false". 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
cacheFile Specifies the name of a file that can be used to cache details of files that pass checkstyle. This can signicantly increase the speed of checkstyle on successive runs. No
checkUnusedThrows Specifies whether to check if unused @throws tags are subclasses of java.lang.RuntimeException. Defaults to "false". No
classpath The classpath to use when looking up classes. Defaults to the current classpath. No
basedir Specifies a base directory for reporting file names relative to. Defaults to "". No

Older versions of the checkstyle task supported the boolean parameters relaxJavadoc and ignoreJavadoc. These parameters have been removed because they were conflicting and the semantics of relaxJavadoc was not clearly specified. The two parameters have been replaced by the javadocScope parameter. Instead of ignoreJavadoc="true" you can now use javadocScope="nothing". The behaviour of relaxJavadoc="true" is roughly the same as javadocScope="protected".

Nested Elements

This task supports the nested elements <fileset>, <classpath> and <formatter>. 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

Examples

Run checkstyle on a single file

  <checkstyle file="Check.java"/>

Run checkstyle on a set of Java files in directory

  <checkstyle>
    <fileset dir="src/checkstyle" includes="**/*.java"/>
  </checkstyle>

Run checkstyle on a set of Java files and allow tabs

  <checkstyle allowTabs="yes">
    <fileset dir="src/checkstyle" includes="**/*.java"/>
  </checkstyle>

Run checkstyle on a set of files and output messages to standard output in plain format, and a file in XML format

  <checkstyle>
    <fileset dir="src/checkstyle" includes="**/*.java"/>
    <formatter type="plain"/>
    <formatter type="xml" toFile="build/checkstyle_errors.xml"/>
  </checkstyle>

Run checkstyle on a set of Java files and disable pattern matching

  <checkstyle allowTabs="yes"
              paramPattern="."
              constPattern="."
              staticPattern="."
              memberPattern=".">
    <fileset dir="src/checkstyle" includes="**/*.java"/>
  </checkstyle>

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



Copyright © 2001 Oliver Burn. All rights Reserved.