Include git commit hash in jar version

后端 未结 2 1815
慢半拍i
慢半拍i 2021-02-01 18:56

I\'m using maven and my goal is to include the git commit hash in the version number. Something like : 1.1.{git_hash}.

I\'m trying to follow this tutorial.

Q: i

2条回答
  •  温柔的废话
    2021-02-01 19:24

    One way to achieve this is to use the git-commit-id-plugin. Add this to the list of plugins in the build section of your pom.xml:

    
        pl.project13.maven
        git-commit-id-plugin
        ${git-commit-id-plugin.version}
        
            
                get-the-git-infos
                
                    revision
                
                validate
            
        
        
            ${project.basedir}/.git
        
    
    

    Note, that I've changed the phase to validate, so the revision number property is already available in when the artifact is packaged.

    Then, add the following to the build section:

    
        ${project.artifactId}-${project.version}-${git.commit.id.describe-short}
        
    
    

    The git.commit.id.describe-short property is produced by the git-commit-id-plugin. It contains current git revision number (shortened to 7 digits) and an optional dirty indicator.

    The produced artifact will look like this: examplelib-1.0.2-efae3b9.jar (or examplelib-1.0.2-efae3b9-dirty.jar in case there are uncommitted changes on your repository).

    Additionally, you might also want to put this information to the MANIFEST.MF of your artifact. In such case add this to your list of plugins (the example assumes the artifact is a jar):

    
        org.apache.maven.plugins
        maven-jar-plugin
        
            
                
                    ${git.commit.id.describe-short}
                
            
        
    
    

    Additional remarks:

    1. I've shown a simple configuration of the git-commit-id-plugin. On their site you may find more options and properties. In addition to properties, that can be used inside pom.xml, the plugin can also generate a properties file containing information about revision.

    2. As an alternative to git-commit-id-plugin, you might prefer buildnumber-maven-plugin. In order to get revision numbers this plugin requires a SCM plugin also configured in your pom.xml.

    3. This setup may interfere with other plugins that transform or rename your artifacts (in my case it was the maven-shade-plugin - one of the sources jar it produces did not contain proper revision number).

提交回复
热议问题