Skip to content

Steve Hill

Failing on Unwanted Package Imports

java, checkstyle, gradle1 min read

We can fail the build when unwanted packages (e.g., junit.framework) are imported through Gradle's built-in checkstyle plugin. Setup is different for solo-project and multi-project builds. See the following links for a git diff of what is needed to enable this behavior:

solo-project
e4aa2758
multi-project
cd195391

Context

I never want to bring in anything from the junit.framework package when writing new code. Gradle's checkstyle plugin and the IllegalImport rule can help me formalize this intention as part of my build.

JUnit IDE Import Choices

The build file is the best place to capture intentions like this because it is shared by everyone who touches the project. While it is possible to configure IDEs to exclude unwanted packages from import, the default behavior is that terrible choice above - where most of the options are wrong.

Solo-Project Setup

We'll start with a simple example project that has imported a method from our unwanted package: sghill/fail-unwanted-package-imports.

Apply the plugin:

1apply plugin: 'checkstyle'

The plugin is looking for configuration in ${project.projectDir}/config/checkstyle/checkstyle.xml by default.

1<?xml version="1.0" encoding="UTF-8"?>
2<!DOCTYPE module PUBLIC
3 "-//Puppy Crawl//DTD Check Configuration 1.3//EN"
4 "http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
5
6<module name="Checker">
7 <module name="TreeWalker">
8 <module name="IllegalImport">
9 <property name="illegalPkgs" value="junit.framework"/>
10 </module>
11 </module>
12</module>

Now running ./gradlew build fails our new :checkstyleTest task:

1FAILURE: Build failed with an exception.
2
3* What went wrong:
4Execution failed for task ':checkstyleTest'.
5> Checkstyle rule violations were found. See the report at: file:///Users/sghill/code/build-well/fail-unwanted-package-imports/build/reports/checkstyle/test.html

Multi-Project Setup

We're going to reuse the solo-project configuration in the same location for our multi-project setup. The default checkstyle file path is relative to the project instead of the rootProject, so we'll need to do some task configuration. This is the way to share a checkstyle configuration:

1allprojects {
2 apply plugin: 'checkstyle'
3
4 tasks.withType(Checkstyle) {
5 def path = "${rootProject.projectDir}/config/checkstyle/checkstyle.xml"
6 config resources.text.fromFile(path)
7 }
8}

Now running ./gradlew build --continue gives us errors from foundation and acceptance:

1FAILURE: Build completed with 2 failures.
2
31: Task failed with an exception.
4-----------
5* What went wrong:
6Execution failed for task ':acceptance:checkstyleTest'.
7> Checkstyle rule violations were found. See the report at: file:///Users/sghill/code/build-well/fail-unwanted-package-imports/acceptance/build/reports/checkstyle/test.html
8
9==============================================================================
10
112: Task failed with an exception.
12-----------
13* What went wrong:
14Execution failed for task ':foundation:checkstyleTest'.
15> Checkstyle rule violations were found. See the report at: file:///Users/sghill/code/build-well/fail-unwanted-package-imports/foundation/build/reports/checkstyle/test.html

Reports

By default reports are produced in xml and html formats:

  • ${project.buildDir}/reports/checkstyle/*.xml
  • ${project.buildDir}/reports/checkstyle/*.html

Example Checkstyle HTML Report

Versions

This example has been tested against the following versions:

DateGradleCheckstyleSolo-ProjectMulti-Project
2017-06-204.06.19outputoutput
2017-06-203.5.16.19outputoutput
2017-06-203.4.15.9outputoutput
2017-06-203.35.9outputoutput