How can I automatically run google-java-format as part of my Gradle build?

1.5k Views Asked by At

Google-java-format-gradle-plugin integrates with Gradle, but how can I run it automatically as part of the normal build?

2

There are 2 best solutions below

0
On

DependsOn plugin's task that you need from a task that relates to a normal build, for example, you can use preBuild task:

tasks.findByName("preBuild").dependsOn(YOUR_TASK_FROM_PLUGIN)

or shorter

preBuild.dependsOn(YOUR_TASK_FROM_PLUGIN)

Also you can choose another task instead of preBuild.

0
On

Sherter gradle plugin is automatically integrated to "gradle build". When you run it, it will run "gradle verifyGoogleJavaFormat". In case of violations, the build will fail.

We are using it on jenkins and it works. You will only need dependency to build.gradle file:

compile group: 'com.github.sherter.google-java-format', name: 'com.github.sherter.google-java-format.gradle.plugin', version: '0.8', ext: 'pom'

And also add plugin:

id 'com.github.sherter.google-java-format' version '0.8'

Then just run "gradle build" and you can see in the console, that verifyGoogleJavaFormat was executed.