Injecting current git commit id into Java webapp

后端 未结 6 1162
抹茶落季
抹茶落季 2020-12-08 16:14

We have a git repository which contains source for a few related Java WARs and JARs. It would be nice if the Java code could somehow:

System.err.println(\"I          


        
相关标签:
6条回答
  • 2020-12-08 16:33

    You can get the last commit SHA with

    git rev-parse HEAD
    

    but it's generally a lot more useful to use

    git describe
    

    which will give you something that looks like this:

    v0.7.0-185-g83e38c7
    

    This works if you have tags - it will tell you how many commits from the last valid tag your current checkout is at plus a partial SHA for that commit, so you can use it to base a checkout off of later. You can use this identifier just like a SHA in most circumstances, but it's much more human readable.

    0 讨论(0)
  • 2020-12-08 16:40

    First, you can use ident gitattribute with $Id$ keyword (although it is not probably what you want; it is hash of file contents, and has nothing to do with current project version).

    Second, you can do it the way Linux kernel and Git itself do it: in Makefile (in your case: in Ant file) there is rule which replaces some placeholder, usually '@@VERSION@@' (but in case of Perl it is '++VERSION++') by result of GIT-VERSION-GEN, which in turn uses "git describe". But for that to be useful you have to tag your releases (using annotated / signed tags).

    0 讨论(0)
  • 2020-12-08 16:47

    If it helps for someone else. I know yours is ANT

    For MAVEN build, you could probably use git-commit-id-plugin in your pom.xml file

        <plugin>
            <groupId>pl.project13.maven</groupId>
            <artifactId>git-commit-id-plugin</artifactId>
            <version>2.2.0</version>
            <executions>
                <execution>
                    <goals>
                        <goal>revision</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <dotGitDirectory>${project.basedir}/.git</dotGitDirectory>
                <generateGitPropertiesFile>true</generateGitPropertiesFile>
                <generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
            </configuration>
        </plugin>
    

    Please go through :
    1. http://www.baeldung.com/spring-git-information &
    2. https://github.com/ktoso/maven-git-commit-id-plugin for more info.

    0 讨论(0)
  • 2020-12-08 16:48

    I wrote an Ant task to get the buildnumber using JGit API (without git command line app), see jgit-buildnumber-ant-task. Then you can store this buildnumber inside MANIFEST.MF file and get it from the classpath on runtime.

    0 讨论(0)
  • 2020-12-08 16:52

    I don't know if there are any Ant task for git (I googled a bit without success), anyway Ant can update a properties file with Piotr's option (git rev-parse HEAD) and then in runtime use that properties to get the revision number. This is cleaner and IDE friendly than having Ant generating a .java file.

    0 讨论(0)
  • 2020-12-08 16:53

    git rev-parse HEAD will print what you probably want (e.g. id of HEAD commit). You can make ant generate a simple Java class with this id as a static constant.

    0 讨论(0)
提交回复
热议问题