jslint4java ant task

jslint4java provides an ant task so that it can be invoked automatically as part of your build procedure. For more detail on precisely what is checked, please see the original documentation. To get started quickly, see the examples.

jslint4java requires Java 5 and ant 1.7.


Attribute Description Required?
encoding Specify the encoding of the JavaScript files. Defaults to system encoding. No
haltOnFailure Should the build stop if JSLint reports an error? Defaults to true. No
jslint The path to an alternative jslint.js. If you want to use a newer version than the bundled one. No
options A comma separated list of options to pass to JSLint. No

The valid list of options is defined by the Option enum, and comprises of:

If adsafe should be enforced
If bitwise operators should not be allowed
If the standard browser globals should be predefined
If upper case html should be allowed
If css workarounds should be tolerated
If debugger statements should be allowed
If logging should be allowed (console, alert, etc.)
If === should be required
If eval should be allowed
If for in statements must filter
If html fragments should be allowed
If immediate invocations must be wrapped in parens
The number of spaces used for indentation (default is 4)
If line breaks should not be checked
The maximum number of warnings reported (default is 50)/dd>
If constructor names must be capitalized
If names should be checked
If html event handlers should be allowed
If only one var statement per function should be allowed
If the scan should stop on first error
If increment/decrement should not be allowed
If the . should not be allowed in regexp literals
If the rhino environment globals should be predefined
If use of some browser features should be restricted
If the system object should be predefined
Require the "use strict"; pragma
If all forms of subscript notation are tolerated
If variables should be declared before used
If strict whitespace rules apply
If the yahoo widgets globals should be predefined

If a parameter is required (as in the case of indent), then it can be supplied by appending an equals and the value. e.g. indent=2.

Parameters specified as nested elements

resource collection

Any kind of resource collection may be specified as a subelement. Most of the time, this will mean a fileset.


Zero or more formatter elements may be specified in order to control output from jslint. Each formatter element has two attributes.

Attribute Description Required
type Either plain or xml. Yes
destfile A location to write the output to. Defaults to the console. No

If no formatter elements are present, then no output will be produced. However, the build will still fail if the validation fails.


You can specify a comma separated list of global variables inside a <predef> element. This can be useful for functions that you know are globally available in your environment, e.g. jQuery.


First, you need to define the task in your build file.

  <taskdef name="jslint"
           classpath="/path/to/jslint4java-1.3.3.jar" />

You may also use the antlibs facility to pull in JSLint.

  <project xmlns:jsl="antlib:com.googlecode.jslint4java">

Doing so will require you to access the jslint task as jsl:jslint. This also means that you have to install jslint4java-1.3.3.jar in ~/.ant/libs.

If you want to keep the jslint4java jar checked in as part of your project, you have to combine the above two approaches. For example, assuming you have placed the jar in lib-build:

<project xmlns:jsl="antlib:com.googlecode.jslint4java">
  <path id="ant.tasks.classpath">
    <fileset dir="lib-build" include="*.jar" />
  <taskdef uri="antlib:com.googlecode.jslint4java"
           classpathref="ant.tasks.classpath" />

Again, this means you have to refer to the task as jsl:jslint.

To check a directory for all .js files, and emit all errors to the console:

    <formatter type="plain" />
    <fileset dir="web/js" includes="*.js" />

To check a directory for all .js files, excluding packed files. Send any problems to a file jslint.out.

    <formatter type="plain" destfile="${build.dir}/jslint.out" />
    <fileset dir="web/js" includes="**/*.js" excludes="**/*.pack.js" />

To check a directory of JavaScript files, whilst warning about whitespace issues and undefined variables, as well as predefining a couple of global variables we know we'll be using. Send the errors to the console, and also emit an XML report to the build directory.

  <jslint options="undef,white">
    <formatter type="plain" />
    <formatter type="xml" destfile="${build.dir}/jslint.xml" />
    <fileset dir="web/js" includes="**/*.js" excludes="**/*.pack.js" />

NB: In order to specify a dollar sign, it has to be doubled so that ant doesn't attempt to treat it as a property. If you wanted to list the $$ function that Prototype uses, you'd have to say <predef>$$$$</predef>!