5

Is there a way I can automatically update a pom.xml file on a git commit using a git commit hook?

What I want to do, is I want to replace all instances of the old version, with the new version based on the branch name.

For example.

If I check out from master I might find in my pom.xml

<dependancy>
  <groupId>com.mycompany</groupId>
  <artifactId>my_component</artifactId>
  <version>master-SNAPSHOT</version>
</dependancy>

or

<groupId>com.mycompany.project</groupId>

    <artifactId>mainProject</artifactId>
    <version>master-SNAPSHOT</version>
    <name>mainProject</name>

What I would like to do, is to make sure that everyone who commits a new branch, has that new branch reflected in the pom.xml. So if I create a new branch based on master named "myNewBranch", I would like the pom.xml to be automatically updated to the following:

<dependancy>
  <groupId>com.mycompany</groupId>
  <artifactId>my_component</artifactId>
  <version>myNewBranch-SNAPSHOT</version>
</dependancy>

or

<groupId>com.mycompany.project</groupId>

    <artifactId>mainProject</artifactId>
    <version>myNewBranch-SNAPSHOT</version>
    <name>mainProject</name>
kenorb
  • 155,785
  • 88
  • 678
  • 743
Avik
  • 723
  • 6
  • 16
  • Even if I understand your need, this could lead to painful merges between two branches, since every pom.xml would be modified at the same lines. – Guillaume Darmont Jun 18 '13 at 22:03
  • @noahlz That looks like it would do the trick. How do I pull the git branch name as a variable? – Avik Jun 19 '13 at 06:32
  • @GuillaumeDarmont well right now we are doing it manually, I'd like to automate it. – Avik Jun 19 '13 at 06:33
  • Related: [Update Maven dependencies automatically](https://stackoverflow.com/q/12551031/55075). – kenorb Sep 11 '17 at 18:32

1 Answers1

3

You are looking to do something that is not provided "out of the box" by core Maven or the Maven SCM plugin.

You'll have to write a shell script that runs on post-commit that programmatically obtains (or creates) the branch name as per How to programmatically determine the current checked out Git branch and then calls mvn versions:set

You could also do something like make a custom Maven plugin or maven-ant-run execution to do this, but I think the script approach will be the path of least resistance for you.

Community
  • 1
  • 1
noahlz
  • 10,202
  • 7
  • 56
  • 75