Put current git branch to project version

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?

  • git author rename not working
  • Failed to copy Manifest
  • get commits since a particular date in git based on commit date
  • Git-Bash Command Prompt won't open up after upgrading to Windows 10
  • Using matlab package folder notation in Atlassian Bitbucket
  • git successfully push changes to main repository but still see no changes in the files
  • How to handle Asian characters in file names in Git on OS X
  • When I saved in Windows 7, git diff shows all the lines with ^M
  • Gitolite One User - Many Keys - Different usernames
  • Jenkins fails building
  • Importing with EGit clones repository but doesn't let me import project
  • How do I push my git repository to dokku?
  • 2 Solutions collect form web for “Put current git branch to project version”

    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>
    

    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

    Git Baby is a git and github fan, let's start git clone.