ant build时修改代码的最佳实践 [英] Best practice for code modification during ant build

查看:26
本文介绍了ant build时修改代码的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

承认,这听起来完全不是最佳实践,但让我解释一下.在构建过程中,我们需要将构建号和系统版本粘贴到一个类中,该类的唯一目的是包含这些值并使其可访问.

Admitted, this doesn't sound like a best practice altogether, but let me explain. During the build, we need to paste the build number and the system version into a class whose sole purpose is to contain these values and make them accessible.

我们的第一个想法是使用系统属性,但由于部署环境的易变性(另一种说法是系统管理员正在做奇怪的、邪恶的、令人毛骨悚然的事情"),我们希望对它们进行硬编码.

Our first idea was to use system properties, but due to the volatility of the deployment environment (an other way of saying "the sysadmins are doing weird unholy creepy things") we would like to have them hard-coded.

基本上我看到在 ant 中实现它的 4 种可能性:

Essentially I see 4 possibilities to achieve it in ant :

  1. 在类中的标记上使用

这种方法的问题是文件被改变了,所以你必须在编译后用 <replaceregexp> 替换令牌......太丑了,我不想使用正则表达式触摸源代码.加上时间依赖性.

The problem with this approach is that the file is changed, so you have to replace the token back after compilation with a <replaceregexp>...sooo ugly, I don't want to touch source code with regex. Plus temporal dependencies.

拷贝文件,在拷贝上进行替换,编译拷贝,删除拷贝

必须注意顺序 - 必须首先编译原始类才能被副本覆盖.时间依赖性也很丑陋.

One one has to mind the sequence - the original class has to be compiled first in order to be overwritten by the copy. Temporal dependencies are ugly too.

复制文件,替换原件上的token,编译,用副本替换染色的原件

除非嵌入在编译目标中,否则相同的时间依赖性问题.这也很丑陋,因为我们所有的构建文件都使用相同的导入编译目标.

Same temporal dependency issue unless embedded in the compile target. Which is ugly too, because all our build files use the same imported compile target.

在构建脚本中从头开始创建文件/将文件存储在源路径之外

是对前三个的改进,因为没有时间依赖性,但是编译器/IDE 非常不满意,因为它忽略了这个类.红色标记非常难看.

Is an improvement over the first three as there are no temporal dependencies, but the compiler/IDE is very unhappy as it is oblivious of the class. The red markers are disturbingly ugly.

您对替代方案有何看法?

What are your thoughts on the alternatives?

是否有任何最佳做法?

我当然希望我错过了一个完全理智的方法.

I sure hope I have missed a perfectly sane approach.

谢谢

编辑我们最终使用清单将内部版本号和系统版本存储在 Implementation-Version 属性中,取消了 MyClass.class.getPackage().getImplementationVersion().我发现这个解决方案是 <的答案之一strong>this thread,由 andersoj 发布在评论中

EDIT We ended up using the manifest to store the build number and system version in the Implementation-Version attribute, unsing MyClass.class.getPackage().getImplementationVersion(). I have found this solution was one of the answers to this thread, which was posted in the comment by andersoj

推荐答案

我认为更简单的方法是让您的 Version.java 类从包含在 JAR 中的简单 .properties 文件中读取,并在 Ant 构建中的构建时生成这个 .properties 文件.例如只是生成:

I think a simpler approach would be to have your Version.java class read from a simple .properties file included in the JAR, and just generate this .properties file at build-time in the Ant build. For example just generate:

build.number = 142
build.timestamp = 5/12/2011 12:31

Ant 中的 内置 任务已经有一半了(见第二个例子).

The built-in <buildnumber> task in Ant does half of this already (see the second example).

这篇关于ant build时修改代码的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆