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?
To enable it in the current project, go to File→Settings... →google-java-format Settings (or IntelliJ IDEA→Preferences... →Other Settings→google-java-format Settings on macOS) and check the Enable google-java-format checkbox. (A notification will be presented when you first open a project offering to do this for you.)
You can implement a Gradle plugin in any language you like, provided the implementation ends up compiled as JVM bytecode. In our examples, we are going to use Java as the implementation language for standalone plugin project and Groovy or Kotlin in the buildscript plugin examples.
It serves as the basis for many of the other JVM language Gradle plugins. You can find a comprehensive introduction and overview to the Java Plugin in the Building Java Projects chapter. As indicated above, this plugin adds basic building blocks for working with JVM projects.
google-java-format. google-java-format is a program that reformats Java source code to comply with Google Java Style. Using the formatter from the command-line. Download the formatter and run it with:
Let's open this maven repository page. It's the protobuf-java artifact of com.google.protobuf package. Let's select the latest version: 3.10.0 Click on the Gradle tab and copy the setting, then paste it into the dependencies block of our build.gradle file:
If a dependent project has changed in an ABI -compatible way (only its private API has changed), then Java compilation tasks will be up-to-date. This means that if project A depends on project B and a class in B is changed in an ABI-compatible way (typically, changing only the body of a method), then Gradle won’t recompile A.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With