本文介绍了将当前的 git commit id 注入 Java webapp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个 git 存储库,其中包含一些相关 Java WAR 和 JAR 的源代码.如果 Java 代码能以某种方式实现就好了:

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

(显然,真正的代码可能会将其放入 HTTP 标头中,在启动时记录它,或者其他什么,现在这并不重要)

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

我们正在使用 Ant 来构建(至少对于生产构建,似乎有些程序员从 Eclipse 内部进行测试,而我对此知之甚少)二进制文件.

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.

是否有一种规范的方法可以在构建时将当前 git checkout 的提交 ID 获取到我们的 Java 中?如果没有,人们可以使用 Ant 构建建议他们如何做,我们将看看是否会出现规范的解决方案?我确信我可以完全用整块布自己发明一些东西,但这似乎是一个可重复使用的构建块,所以我宁愿不这样做.

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.

推荐答案

我不知道 git 有没有 Ant 任务(我在 google 上搜索了一下没有成功),反正 Ant 可以使用 Piotr 的选项更新属性文件(git rev-parse HEAD) 然后在运行时使用该属性来获取修订号.这比让 Ant 生成 .java 文件更干净,IDE 友好.

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.

这篇关于将当前的 git commit id 注入 Java webapp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 14:29