Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
@spg

This comment has been minimized.

Copy link

spg commented Oct 1, 2014

Also, don't forget to chmod +x pre-commit, otherwise the hook won't execute

@hshyamh4

This comment has been minimized.

Copy link

hshyamh4 commented Jun 12, 2015

I recently installed git in my ubuntu machine but i can't find the location .git
Where can i find .git directory in ubuntu ? i am unable to see.

@hshyamh4

This comment has been minimized.

Copy link

hshyamh4 commented Jun 12, 2015

HOW TO DO IT IN GRADLE ?

@hshyamh4

This comment has been minimized.

Copy link

hshyamh4 commented Jun 12, 2015

It is possible to check commit message format ?

@gmjonker

This comment has been minimized.

Copy link

gmjonker commented Nov 11, 2015

I think you forgot echo in line 14

@FeanorsCurse

This comment has been minimized.

Copy link

FeanorsCurse commented Dec 15, 2015

I get an error in line 8: `$(' is not allowed as a global variable name

@jbarotin

This comment has been minimized.

Copy link

jbarotin commented Jul 13, 2017

Thanks, I use it for a pre-push hook it work very well !

@eonezhang

This comment has been minimized.

Copy link

eonezhang commented Nov 6, 2017

how can I share with my team, except the copy/paste way.

@Vallerious

This comment has been minimized.

Copy link

Vallerious commented Mar 17, 2020

This approach has no value except if you work on your own. There has to be a way to do this as part of the maven.
For example, in nodejs world you have 'husky' library which listens for git events and executes tests, it is included in package.json and exists in git.

@arnobroekhof

This comment has been minimized.

Copy link
Owner Author

arnobroekhof commented Mar 17, 2020

@Vallerious, I encourage you to read the manual of git and your hosting provider on how to create global git hooks

@Vallerious

This comment has been minimized.

Copy link

Vallerious commented Mar 17, 2020

@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

This comment has been minimized.

Copy link
Owner Author

arnobroekhof commented Mar 17, 2020

@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

@Vallerious

This comment has been minimized.

Copy link

Vallerious commented Mar 17, 2020

@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
You can’t perform that action at this time.