Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 242
Jim Showalter Avatar asked Apr 04 '20 19:04

Jim Showalter


People also ask

How to use google Java format plugin?

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

Can a Gradle plugin be written in Java?

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.

What is the Gradle Java plugin?

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.

How do I reformat Java source code to Google Java style?

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:

How to set Protobuf-Java in Gradle?

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:

Why doesn’t Gradle recompile my project when it changes?

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.


1 Answers

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)
like image 163
Chriki Avatar answered Nov 15 '22 04:11

Chriki