Gradle: Spotless task not firing when needed
Asked Answered
M

1

7

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".

Mele answered 4/11, 2016 at 21:57 Comment(0)
T
8

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'))
}
Taffy answered 4/11, 2016 at 23:48 Comment(2)
This was breaking way too often on completely trivial issues since some of the checks are very opinionated about e.g. new lines. Very disruptive to discover that CI failed over a newline. What matters is that this cleans itself up, eventually, provided you run gradle build locally once in a while. That is good enough. The end result is the same: clean code. I actually have intellij clean my code as well but it keeps the newlines.Lukas
Eventually consistent code formatting is good enough for me.Inoue

© 2022 - 2024 — McMap. All rights reserved.