Include git commit hash in jar version

后端 未结 2 1814
慢半拍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:

    <plugin>
        <groupId>pl.project13.maven</groupId>
        <artifactId>git-commit-id-plugin</artifactId>
        <version>${git-commit-id-plugin.version}</version>
        <executions>
            <execution>
                <id>get-the-git-infos</id>
                <goals>
                    <goal>revision</goal>
                </goals>
                <phase>validate</phase>
            </execution>
        </executions>
        <configuration>
            <dotGitDirectory>${project.basedir}/.git</dotGitDirectory>
        </configuration>
    </plugin>
    

    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:

    <build>
        <finalName>${project.artifactId}-${project.version}-${git.commit.id.describe-short}</finalName>
        <!-- your list of plugins -->
    </build>
    

    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):

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
            <archive>
                <manifestEntries>
                    <SCM-Revision>${git.commit.id.describe-short}</SCM-Revision>
                </manifestEntries>
            </archive>
        </configuration>
    </plugin>
    

    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).

    0 讨论(0)
  • 2021-02-01 19:33

    The above accepted answer didn't work for me. I found the link https://dzone.com/articles/maven-git-commit-id-plugin, from where I copied the plugin code below. It worked first time for me. I now have the git.properties file automatically included in my target JAR file. Very useful for tracking.

    <plugin>
    <groupId>pl.project13.maven</groupId>
    <artifactId>git-commit-id-plugin</artifactId>
    <version>2.2.4</version>
    <executions>
        <execution>
            <id>get-the-git-infos</id>
            <goals>
                <goal>revision</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <dotGitDirectory>${project.basedir}/.git</dotGitDirectory>
        <prefix>git</prefix>
        <verbose>false</verbose>
        <generateGitPropertiesFile>true</generateGitPropertiesFile>
        <generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
        <format>json</format>
        <gitDescribe>
            <skip>false</skip>
            <always>false</always>
            <dirty>-dirty</dirty>
        </gitDescribe>
    </configuration>
    

    Add finalName to build section to also have the version in the target file name

    <build>
    
    <finalName>${project.artifactId}-${project.version}-${git.commit.id.describe-short}</finalName>
    
    ...
    
    </build>
    
    0 讨论(0)
提交回复
热议问题