This project provides an SBT 0.13+ plugin for running Checkstyle over Java source files. For more information about Checkstyle, see http://checkstyle.sourceforge.net/
This plugin uses version 6.5 of Checkstyle.
This is a fork of Etsy's sbt-checkstyle-plugin found here.
Add the following lines to project/plugins.sbt
addSbtPlugin("com.etsy" % "sbt-checkstyle-plugin" % "0.5.2")
Then add the following line to build.sbt
:
com.etsy.sbt.Checkstyle.checkstyleSettings
You can run Checkstyle over your Java source files with the checkstyle
task.
You can run Checkstyle over your Java tests with the test:checkstyle
task.
The Checkstyle configuration file is ./checkstyle-config.xml
by default.
This can be changed by setting the value of checkstyleConfig
.
By default test:checkstyle
uses the same configuration file,
but this can be changed by setting the value of checkstyleConfig in Test
.
The Checkstyle report from executing checkstyle
is output to target/checkstyle-report.xml
by default.
This can be changed by setting the value of checkstyleTarget
.
test:checkstyle
outputs to target/checkstyle-test-report.xml
, but this can be changed by
setting the value of checkstyleTarget in Test
.
You can set checkstyleConfig
like so in build.sbt
:
com.etsy.sbt.Checkstyle.CheckstyleTasks.checkstyleConfig := scala.xml.XML.loadFile(file("checkstyle-config.xml"))
You can also use URLs like so:
com.etsy.sbt.Checkstyle.CheckstyleTasks.checkstyleConfig :=
scala.xml.XML.load("https://raw.githubusercontent.com/checkstyle/checkstyle/master/config/checkstyle_checks.xml")
The xsltTransformations
setting allows applying XSLT transformations to the XML report generated by Checkstyle.
For instance, this could be used to generate a more readable HTML report.
This setting takes values of Option[Set[XSLTSettings]]
, so multiple transformations can be applied.
You can set xsltTransformations
like so in build.sbt
:
xsltTransformations := {
Some(Set(XSLTSettings(baseDirectory(_ / "checkstyle-noframes.xml").value, target(_ / "checkstyle-report.html").value)))
}
If you want to fail the build when Checkstyle issues are found, you can execute checkstyle-check
.
Same applies for checking tests: test:checkstyle-check
.
You can also control what levels of severity should break the build use checkstyleCheckSeverityLevel
like so:
com.etsy.sbt.Checkstyle.CheckstyleTasks.checkstyleCheckSeverityLevel := Set("ignore", "info", "warning", "error")
Default value is:
com.etsy.sbt.Checkstyle.CheckstyleTasks.checkstyleCheckSeverityLevel := Set("error")
Sample output:
...
[info] Will fail the build if errors are found in Checkstyle's XML report.
[warn] Checkstyle error found in [...]/src/main/java/com/etsy/sbt/TestClass.java: Utility classes should not have a public or default constructor.
[warn] Checkstyle error found in [...]/src/main/java/com/etsy/sbt/TestClass.java: File contains tab characters (this is the first instance).
...
[error] (compile:checkstyle) java.lang.IllegalStateException: Issue(s) found in Checkstyle report: [...]/checkstyle-check/target/checkstyle-report.xml. Failing build!
[error] Total time: 0 s, completed Sep 4, 2015 1:16:23 PM
If you wish to run Checkstyle on Integration tests, you can do the following in your build.sbt
:
lazy val root = (project in file(".")).configs(IntegrationTest)
Defaults.itSettings
import com.etsy.sbt._
com.etsy.sbt.Checkstyle.checkstyleSettings ++ Seq(
Checkstyle.CheckstyleTasks.checkstyleConfig := scala.xml.XML.loadFile(file("my-checkstyle-config.xml")),
Checkstyle.CheckstyleTasks.checkstyle in IntegrationTest <<= Checkstyle.checkstyleTask(IntegrationTest),
Checkstyle.CheckstyleTasks.checkstyleCheck in IntegrationTest <<= Checkstyle.checkstyleCheckTask(IntegrationTest),
Checkstyle.CheckstyleTasks.checkstyleTarget in IntegrationTest <<= target(_ / "checkstyle-integration-test-report.xml")
)
You can then run the tasks it:checkstyle
and it:checkstyle-check
.