Skip to content

Instantly share code, notes, and snippets.

@kjivan
Created July 9, 2021 17:19
Show Gist options
  • Save kjivan/b4479a66224f185c6d3cd5ea5db9744f to your computer and use it in GitHub Desktop.
Save kjivan/b4479a66224f185c6d3cd5ea5db9744f to your computer and use it in GitHub Desktop.

Gradle Linting Plugins

Gradle Dependency Linting

Install

Add the following to the plugins section of build.gradle

id 'nebula.lint' version '16.17.0'

Add the following for gradle dependency linting

gradleLint {
    criticalRules=['all-dependency']
    reportFormat = 'text'
}

Dealing With Errors

Errors

error   unused-dependency                  one or more classes in org.springframework:spring-core:5.3.3 are required by your code directly (no auto-fix available)

Means you have to add the specified dependency since you depend on it

error   unused-dependency                  this dependency is unused and can be removed
build.gradle:24
compile 'com.querydsl:querydsl-jpa:4.1.4'

Means you can delete the dependency since it doesn't see it being used in the jar(be careful if it is being used for code gen)

error   unused-dependency                  this dependency should be moved to configuration implementation
build.gradle:30
compile 'org.apache.commons:commons-lang3:3.7'
error   unused-dependency                  this dependency is a service provider unused at compileClasspath time and can be moved to the runtimeOnly configuration (no auto-fix available)
build.gradle:34
compile 'org.hibernate:hibernate-java8:5.0.12.Final'

Means you should adjust the dependency to only be used at the right time

error     unused-dependency                  this dependency should be removed since its artifact is empty (no auto-fix available)
build.gradle:36
compile 'org.springframework.boot:spring-boot-starter-cache:2.4.2'

Means you can remove the dependency

Ignoring Errors

If you need to disable linting due to code gen, poorly designed packages or some other reason you can add the following

gradleLint.ignore('dependency-parentheses', 'dependency-tuple') {
  // here I want to use a non-idiomatic dependency syntax, but lint for everything else
}

If you want to ignore all issues

gradleLint.ignore {
  // my build script code that makes the linter suspicious...
}

Spotless (Consistent Formatting)

Install

Add the following the plugins section of build.gradle

id "com.diffplug.spotless" version "5.11.0"

Add the following for java and groovy gradle formatting

spotless {
    //ratchetFrom 'origin/develop' - Only check changes from develop

    format 'misc', {
        target '*.gradle', '*.md', '.gitignore'

        trimTrailingWhitespace()
        indentWithSpaces(4)
        endWithNewline()
    }
    java {
        removeUnusedImports()
        googleJavaFormat().aosp()
    }
    groovyGradle {
        target '*.gradle'
        greclipse()
    }
}

Formatting

Checking for Formatting Issues

./gradlew spotlessCheck

Will pass or fail if the formatting is correct and will show you what needs to be formatted

Fixing Formatting Issues

./gradlew spotlessApply

Will fix any formatting issues

  • Provides diagnostics to show invalid formatting (with quick fixes)
  • Provides a Spotless fixAll code action (Format on Save)
  • Provides a Spotless formatter (Format Document)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment