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 |
failureProperty | The name of a property to set in case of build failure. You should also set haltOnFailure="false" . |
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 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
.
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 | One of plain , xml or junit . The xml format is a custom invention of this project; it's probably more useful to use the junit format as it has interoperability with existing tools. |
Yes |
destfile | A location to write the output to.
|
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" classname="com.googlecode.jslint4java.ant.JSLintTask" classpath="/path/to/jslint4java-${project.version}.jar" />
You may also use the antlibs facility to pull in JSLint.
<project xmlns:jsl="antlib:com.googlecode.jslint4java"> … </project>
Doing so will require you to access the jslint
task as
jsl:jslint
. This also means that you have to install
jslint4java-${project.version}.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" /> </path> <taskdef uri="antlib:com.googlecode.jslint4java" resource="com/googlecode/jslint4java/antlib.xml" classpathref="ant.tasks.classpath" /> … </project>
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:
<jslint> <formatter type="plain" /> <fileset dir="web/js" includes="*.js" /> </jslint>
To check a directory for all .js
files, excluding packed files.
Send any problems to a file jslint.out
.
<jslint> <formatter type="plain" destfile="${build.dir}/jslint.out" /> <fileset dir="web/js" includes="**/*.js" excludes="**/*.pack.js" /> </jslint>
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 JUnit XML reports to the build directory.
<jslint options="undef,white"> <predef>jQuery,$$</predef> <formatter type="plain" /> <formatter type="junit" destfile="${build.dir}/test-reports" /> <fileset dir="web/js" includes="**/*.js" excludes="**/*.pack.js" /> </jslint>
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>
!