Skip to content

Instantly share code, notes, and snippets.

@shravankb
Forked from franklsm1/INSTRUCTIONS.md
Created September 9, 2021 16:38
Show Gist options
  • Select an option

  • Save shravankb/ebd8711f38d5346f2d295d33091c0f9e to your computer and use it in GitHub Desktop.

Select an option

Save shravankb/ebd8711f38d5346f2d295d33091c0f9e to your computer and use it in GitHub Desktop.
Auto configure pre-commit hooks for a Gradle Spring App

Steps

  1. create a file called "pre-commit" with no extension in the project directory root
  2. add the code from the pre-commit file below into that new file
  3. create a new file called "preCommit.gradle" inside of the root gradle folder
  4. add the code from the preCommit.gradle file below into that new file
  5. add the following line at the end of the build.gradle file:
    • apply from: "$projectDir/gradle/preCommit.gradle"

Now when you run the ./gradlew build command the git pre-commit hook will be automatically setup. This will prevent commits from being committed without all the tests passing locally.

Note: to skip this hook add the --no-verify flag to the git commit command

#!/bin/bash
echo "Running git pre-commit hook"
./gradlew clean build
RESULT=$?
# return 1 exit code if running checks fails
[ $RESULT -ne 0 ] && exit 1
exit 0
task installGitHook(type: Copy) {
from new File(projectDir, 'pre-commit')
into { new File(projectDir, '.git/hooks') }
fileMode 0777
}
assemble.dependsOn installGitHook
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment