How can I configure Gradle google-java-format plugin to run goJF in the build step?
Asked Answered
Y

1

5

We wired https://github.com/sherter/google-java-format-gradle-plugin into our project per the readme.

We also wired in a pre-commit hook to run the plugin before committing, which ensures that all of the code in a changelist is formatted before pushing it, which avoids errors in Jenkins when we run the verGJF task.

But we have to keep remembering to run goJF locally before running ./gradlew build, or the build fails with formatting errors.

We worked around this by adding the https://plugins.jetbrains.com/plugin/8527-google-java-format and https://plugins.jetbrains.com/plugin/7642-save-actions plugins for IntelliJ, enabling the google-java-format plugin, and configuring the save-actions plugin to format on save.

But that's a lot of extra configuration a developer has to remember to go through, plus it means they can't format code the way they want while working on it, and only have it be reformatted at the point of build or commit.

We'd prefer an all-Gradle solution, so that the goJF task is run before the build task (and before the verGJF task, which is already bound to the build task by the google-java-format Gradle plugin).

We couldn't figure out how to do that. Does someone else know?

Yasukoyataghan answered 4/4, 2020 at 19:24 Comment(2)
Another alternative to adding a task dependency in build.gradle you can consider is to configure Gradle task to run before/after IDE Build, see Configure running triggers for Gradle tasks.Leo
I wound up using Chriki's answer, but being able to wire Gradle tasks into IntelliJ will be useful in other situations--thank you!Yasukoyataghan
G
6

It sounds like you want to essentially always ensure that the code is properly formatted before the verifyGoogleJavaFormat task is run (and could complain). In that case, I’d simply make the googleJavaFormat task a dependency of the verifyGoogleJavaFormat task. In your build.gradle file, after you have applied the google-java-format plugin, simply add the following:

verifyGoogleJavaFormat.dependsOn(tasks.googleJavaFormat)

Alternatively, if you really only want to run the code formatter when the build task is run (as opposed to when the verifyGoogleJavaFormat task is run only), you could add this instead:

build.dependsOn(tasks.googleJavaFormat)
verifyGoogleJavaFormat.mustRunAfter(tasks.googleJavaFormat)
Graphomotor answered 4/4, 2020 at 20:3 Comment(8)
I'd tried mustRunAfter but couldn't get it to work. I'll try your first suggestion.Yasukoyataghan
Trying your first suggestion, it fails the same way as mustRunAfter: * What went wrong: Could not determine the dependencies of task ':verifyGoogleJavaFormat'. > Cannot convert com.github.sherter.googlejavaformatgradleplugin.GoogleJavaFormatExtension_Decorated@4600b9a9 to a task. The following types/formats are supported: - A String or CharSequence task name or path - A TaskReference instance - A Task instance ... - An Iterable, Collection, Map or array instance that contains any of these typesYasukoyataghan
I’m sorry, I should have tried my solution first. I didn’t realize that there is an extension that has the same name as the googleJavaFormat task. I have updated my answer and successfully tested it with Gradle 6.3).Graphomotor
Ah! That worked. I've not seen a task and extension with the same name before. Will add that to the list of things to check next time. Thank you!Yasukoyataghan
Thanks for letting me know, glad to hear it worked! If this was the answer to your question, then I’d be happy if you could mark it as accepted :-)Graphomotor
Thought I'd done that but had only upvoted it. Accepted now. Thanks again--I have this configuration now in our Gradle files at work!Yasukoyataghan
Thanks a lot, Jim!Graphomotor
build.dependsOn(tasks.googleJavaFormat) this gives me "No candidates found for method call build.dependsOn."Alysonalysoun

© 2022 - 2024 — McMap. All rights reserved.