Skip to content

Using Lint

Danny Thomas edited this page Mar 17, 2017 · 19 revisions

Configuring

To apply this plugin:

plugins {
  id 'nebula.lint' version '6.1.4'
}

Alternatively:

buildscript {
  repositories { jcenter() }
  dependencies {
    classpath 'com.netflix.nebula:gradle-lint-plugin:latest.release'
  }
}

apply plugin: 'nebula.lint'

Define which rules you would like to lint against:

gradleLint.rules = ['all-dependency'] // add as many rules here as you'd like

You can define critical rules that will fail the build in the event that they are violated by the build:

gradleLint {
  rules  = ['dependency-parentheses']
  criticalRules = ['unused-dependency'] // <-- this will fail the build in the event of a violation
}

For an enterprise build, we recommend defining the lint rules in a init.gradle script or in a gradle script that is included via the Gradle apply from mechanism.

For multimodule projects, we recommend applying the plugin in an allprojects block:

allprojects {
  apply plugin: 'nebula.lint'
  gradleLint.rules = ['all-dependency'] // add as many rules here as you'd like
}

Running

When nebula.lint is applied, build scripts will be automatically linted by a task called lintGradle after the last task in the task graph executes. Results are reported in the console (with ANSI coloring when the console is a TTY).

gradle-lint output

So as not to interfere with common informational tasks, linting does not run if the only tasks in the task graph are 'dependencies', 'dependencyInsight', 'help', 'components', 'projects', 'model', or 'properties'.

For multi-project builds, the lint task will only run once, and will examine every subproject plus the root project.

Disabling automatic lint

If you do not wish to automatically run autoLintGradle after most tasks, you can set gradleLint.alwaysRun = false and run it manually at your convenience. Alternatively, you can set gradleLint.autoLintAfterFailure = false to only run lint after a successful build.

Running a one-off rule

Run ./gradlew fixGradleLint -PgradleLint.rules=minimum-version-rule to run a single rule (or multiple rules, comma-delimited) overriding the rule set defined in the Gradle build file itself.

Auto-fixing violations

Run ./gradlew fixGradleLint (or ./gradlew fixLintGradle, which is an alias) to automatically fix your build scripts! The auto-fix process lists all violations and how they were fixed (when a fix was possible):

gradle-lint output

Generating a lint report

Run ./gradlew generateGradleLintReport to generate a separate report. By default, this task is configured to generate an HTML report. You can change the default by setting:

gradleLint.reportFormat = 'xml' // or 'text' or the default of 'html'