12

I want to put the current git branch into the version field in pom xml. I found some post in the internet suggesting to do something like:

<version>${scm.version}</version>

but this seems not to work with git. Are there any other solutions?

markus
  • 6,258
  • 13
  • 41
  • 68
  • Possible duplicate of [Deriving maven artifact version from git branch](https://stackoverflow.com/questions/13583953/deriving-maven-artifact-version-from-git-branch) – Aldian Sep 17 '18 at 15:33

2 Answers2

18

Yes, use the git commit id plugin for maven

It's pretty straightforward. You can use it to get the git branch with

${git.branch}

So in your case it would go:

<version>${git.branch}</version>
Arnaud Potier
  • 1,750
  • 16
  • 28
  • 9
    Unfortunately it won't work in a multi module maven project because the reactors executes before any plugin is executed and it needs the version to be set to a constant or it fails – Aldian Jun 21 '17 at 14:58
  • Works Great! Thanks – Deepanshu Rathi Dec 22 '21 at 05:28
  • this is actually not supported, here is a workaround but not suggested https://github.com/git-commit-id/git-commit-id-maven-plugin/issues/256#issuecomment-321476196 – Qianlong Mar 13 '23 at 17:01
0

i created a plugin for this behavior (and by extend to link sonar to my maven projects).

The only thing you need to do is

add following plugin to your pom.xml

<plugin>
    <groupId>com.viae-it.maven</groupId>
    <artifactId>sonar-maven-plugin</artifactId>
     <version>LATEST</version>
</plugin>

call the plugin to set the git branch

mvn com.viae-it.maven:sonar-maven-plugin:set-git-branch

then you can use the sonar.branch property

Vandeperre Maarten
  • 556
  • 1
  • 8
  • 21