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?

Chriki
  • 15,638
  • 3
  • 51
  • 66
Jim Showalter
  • 550
  • 3
  • 8
  • 20
  • 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](https://www.jetbrains.com/help/idea/work-with-gradle-tasks.html#config_triggers_gradle). – Andrey Apr 05 '20 at 09:49
  • I wound up using Chriki's answer, but being able to wire Gradle tasks into IntelliJ will be useful in other situations--thank you! – Jim Showalter Apr 05 '20 at 20:01

1 Answers1

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)
Chriki
  • 15,638
  • 3
  • 51
  • 66
  • I'd tried mustRunAfter but couldn't get it to work. I'll try your first suggestion. – Jim Showalter Apr 04 '20 at 23:06
  • 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 types – Jim Showalter Apr 04 '20 at 23:14
  • 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). – Chriki Apr 05 '20 at 05:32
  • 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! – Jim Showalter Apr 05 '20 at 20:00
  • 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 :-) – Chriki Apr 06 '20 at 09:52
  • 1
    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! – Jim Showalter Apr 06 '20 at 14:19
  • Thanks a lot, Jim! – Chriki Apr 06 '20 at 15:18