Injecting current git commit id into Java webapp

2019-01-22 08:17发布

问题:

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 was built from git commit " + commitID);

(Obviously real code might be putting this into an HTTP header, logging it on startup, or whatever, that's not important right now)

We are using Ant to build (at least for production builds, it seems some programmers do their testing from inside Eclipse which I know even less about) binaries.

Is there a canonical way to get the commit id for the current git checkout into our Java at build time? If not, can people using Ant to build suggest how they'd do it and we'll see if a canonical solution emerges? I'm sure I can invent something myself entirely from whole cloth, but this seems like a re-usable building block, so I'd rather not.

回答1:

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.



回答2:

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.



回答3:

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.



回答4:

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.



回答5:

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



回答6:

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

For MAVEN build, you could probably use git-commit-id-plugin

    <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 : http://www.baeldung.com/spring-git-information & https://github.com/ktoso/maven-git-commit-id-plugin for more info.



标签: java git ant