Checkstyle

Performs quality checks on Java source files using Checkstyle and generates reports from these checks.

CheckstyleModule

To use this plugin in a Java/Scala module,

  1. Extend mill.contrib.checkstyle.CheckstyleModule.

  2. Define a configuration file checkstyle-config.xml.

  3. Run the checkstyle command.

checkstyle

  • flags

// if an exception should be raised when violations are found
checkstyle --check

// if Checkstyle output report should be written to System.out
checkstyle --stdout
  • sources (optional)

// incorrect paths will cause a command failure
// checkstyle a/b

// you can specify paths relative to millSourcePath
checkstyle src/a/b

// process a single file
checkstyle src/a/B.java

// process multiple sources
checkstyle src/a/b src/c/d src/e/F.java

// process with flags
checkstyle --check --stdout src/a/b src/c/d

// process all module sources
checkstyle

Shared configuration

To share checkstyle-config.xml across modules, adapt the following example.

import mill._
import mill.contrib.checkstyle.CheckstyleModule
import mill.scalalib._

object foo extends Module {

  object bar extends MyModule
  object baz extends Module {
    object fizz extends MyModule
    object buzz extends MyModule
  }

  trait MyModule extends JavaModule with CheckstyleModule {

    override def checkstyleConfig = T {
      api.PathRef(T.workspace / "checkstyle-config.xml")
    }
  }
}

Limitations

  • Version 6.3 or above is required for plain and xml formats.

  • Setting checkstyleOptions might cause failures with legacy versions.

CheckstyleXsltModule

This plugin extends the mill.contrib.checkstyle.CheckstyleModule with the ability to generate reports by applying XSL Transformations on a Checkstyle output report.

Auto detect XSL Transformations

XSLT files are detected automatically provided a prescribed directory structure is followed.

/**
 * checkstyle-xslt
 *  ├─ html
 *  │   ├─ xslt0.xml
 *  │   └─ xslt1.xml
 *  └─ pdf
 *      ├─ xslt1.xml
 *      └─ xslt2.xml
 *
 * html/xslt0.xml -> xslt0.html
 * html/xslt1.xml -> xslt1.html
 * pdf/xslt1.xml  -> xslt1.pdf
 * pdf/xslt2.xml  -> xslt2.pdf
 */

Specify XSL Transformations manually

For a custom setup, adapt the following example.

import mill._
import mill.api.PathRef
import mill.contrib.checkstyle.CheckstyleXsltModule
import mill.contrib.checkstyle.CheckstyleXsltReport
import mill.scalalib._

object foo extends JavaModule with CheckstyleXsltModule {

    override def checkstyleXsltReports = T {
      Set(
        CheckstyleXsltReport(
          PathRef(millSourcePath / "checkstyle-no-frames.xml"),
          PathRef(T.dest / "checkstyle-no-frames.html"),
        )
      )
  }
}