Search code examples
androidgradlepre-commitgoogle-java-format

Sherter Gradle plugin for pre-commit


I am trying to add sherter gradle plugin for code style. I want to run this plugin in pre-commit script file.

I added sherter in gradle as,

repositories {
   jcenter()
   maven { url "https://plugins.gradle.org/m2/" }
}

dependencies {
   classpath 'gradle.plugin.com.github.sherter.google-java-format:google-java-format-gradle-plugin:0.6'
}

apply plugin: 'com.github.sherter.google-java-format'

My pre-commit file is in a directory called "scripts". In pre-commit file if I added a line like this

./gradlew verifyGJF

it is failing there. when I go to project root directory and run the same command is working. Why is it so? Whether I have to make any link to the gradle file.

Note: Gradle file is executable and it is running fine from project root directory, but from pre-commit file or even in terminal if I go to the "scripts" folder the verifyGJF is not running.


Solution

  • This is happening because gradlew is actually a script at the root of your project directory (see folder hierarchy below).

    gradlew
    /scripts
       pre-commit-script
    /src
    

    Running ./gradlew {task} from your pre-commit-script file instructs it to look for gradlew in the same directory (here that is /scripts).

    If you are trying to invoke gradlew from inside a subdirectory, you need to access it with the relative path: ../gradlew verifyGJF.

    Hope it helps 😊