Skip to content

Instantly share code, notes, and snippets.

@arnobroekhof
Last active May 9, 2022 13:41
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save arnobroekhof/9454645 to your computer and use it in GitHub Desktop.
Save arnobroekhof/9454645 to your computer and use it in GitHub Desktop.
Maven pre commit hook
#!/bin/bash
# save the file as <git_directory>/.git/hooks/pre-commit
echo "Running Maven clean test for errors"
# retrieving current working directory
CWD=`pwd`
MAIN_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# go to main project dir
cd $MAIN_DIR/../../
# running maven clean test
mvn clean test
if [ $? -ne 0 ]; then
"Error while testing the code"
# go back to current working dir
cd $CWD
exit 1
fi
# go back to current working dir
cd $CWD
@pancanin
Copy link

@arnobroekhof Hosting provider? If you collaborate with other people they will not develop on a host machine, they will do on their machines and the only way to make sure that the tests are run before commit or push is to have something programatically hook to that event and execute tests. It has to know what is the platform to run them. So if you could elaborate more on this would be helpful.

@arnobroekhof
Copy link
Author

@vallerious with hosting provider I mean bitbucket, github, gitlab or whatever repository hosting provider you are using...

Or read the manual on howto configure post receive hooks

@pancanin
Copy link

@arnobroekhof I understand that you are talking about a pipeline which runs when your code is on your git provider, but it is a good idea to have this step on every developer's machine so that code that does not pass the tests is not pushed at all.
Once it is on gitlab, github, etc. it is clear how it can be setup.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment