2

I need to trigger a maven build on a local repository before committing the repository to a Git.

What are the things that I'll need?

I am new to Git so do give me suggestions on how to preform the said operation. Thanks in advance

Naveen Dennis
  • 1,223
  • 3
  • 24
  • 39

1 Answers1

1

You can try and set up a pre-commit hook

The pre-commit hook is run first, before you even type in a commit message. It’s used to inspect the snapshot that’s about to be committed, to see if you’ve forgotten something, to make sure tests run, or to examine whatever you need to inspect in the code.

In your case, the .git/hook/pre-commit script can trigger a maven build: if that build fails, exit with status 1 and the commit won't be done.

Even though you had trouble making a git pre-commit hook run before, it is possible to call a python script from a hook, even on Windows.
See this Atlassian example for instance.

#!/bin/sh

"C:\Python27\python.exe" "C:\path\to\mytriggers.py" myParams
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • So I can call a maven build on a local repository from a pre-commit hook written in python? – Naveen Dennis Aug 10 '14 at 04:15
  • 1
    @dennis yes: your hook can be written in any language/script you want. As long as `yourLocalRepo/.git/hook/pre-commit` is executable, it will work. – VonC Aug 10 '14 at 04:16
  • @dennis I have edited the answer to include an example of how to call a python script from a git hook. – VonC Aug 10 '14 at 05:07
  • The pre-commit script can be triggered only from a CI like Jenkins or Bamboo? Can't I build using maven without using the CI? Won't the CI work with the committed repository? I want to stop the commit if the code fails to compile and to pass the various code quality checks and my assumption was that I could add those as maven goals and trigger a maven build from the pre-commit script written in python. Is this practical? Has it been done before? – Naveen Dennis Aug 10 '14 at 11:40
  • @dennis sure, you can build using anything you want: as long as that build tries to do a commit, it will trigger the pre-commit hook script. Then, you can integrate that into a CI. – VonC Aug 10 '14 at 11:55
  • @VonC i was going through the post. I would like to run the mvn compile in the pre-commit hook in the local development boxes. This is to ensure that other developers are warned of any test failures when they try to commit locally. npm package.json has a pre-commit key which can be defined to do this. I'm wondering if we can do a similar thing? – user320550 Jun 08 '16 at 17:55
  • @user320550 You can indeed do a similar pre-commit operation here. If you have any issue with it, describe it in a new question for Stack Overflow users to help: that will be more visible than buried in a comment section. – VonC Jun 08 '16 at 19:31