Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle: Spotless task not firing when needed

I followed the Spotless plugin's readme and included the following into my build.gradle:

apply plugin: 'java'

spotless {
    java {
        eclipseFormatFile 'my-eclipse-format.xml' 
    }
}

When I run "gradlew build", I expect Spotless to a) format my code automatically using the above format; b) do a check (ie spotlessJavaCheck) to verify it happened.

Instead, I only get the b) part working. How can I make sure a) (formatting) happens automatically when I execute the build step? I don't want to explicitly call "gradlew spotlessApply build" but only "gradlew build".

I tried adding "build { dependsOn spotlessApply } but it says "cannot find property spotlessApply".

like image 817
user3651020 Avatar asked Nov 04 '16 21:11

user3651020


1 Answers

For future reference, Spotless' GitHub issues is the best place to ask about Spotless. Keeps it all in one place.

Spotless' intended behavior is to let you know that there's a problem with spotlessCheck. Then you run spotlessApply yourself manually.

The problem with doing it the other way is that you'll never know about a bad commit. If I commit badly formatted code, the CI server will fix the format with spotlessApply, then confirm the format is fixed in spotlessCheck, and say "this code is good!" when actually somebody committed something with wrong formatting.

If you still want what you want, you can accomplish it like this:

afterEvaluate {
    tasks.getByName('spotlessCheck').dependsOn(tasks.getByName('spotlessApply'))
}
like image 128
Ned Twigg Avatar answered Sep 26 '22 11:09

Ned Twigg