Ant构建过程中code修改最佳实践 [英] Best practice for code modification during ant build

查看:192
本文介绍了Ant构建过程中code修改最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

录取,这听起来不像是一个最好的做法完全,但让我解释一下。在构建,我们需要将版本号和系统版本粘贴到一个类,其唯一目的就是遏制这些价值观,让他们访问。

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.

我们的第一个想法是使用系统属性,但由于部署环境的波动,我们希望让他们的硬codeD(称系统管理员在做奇怪的邪恶令人毛骨悚然的东西的另一种方式)

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.

基本上我看到4种可能性来实现它在蚂蚁:

Essentially I see 4 possibilities to achieve it in ant :


  1. 使用<更换> 在类的标记

  1. use <replace> on a token in the class

这种方法的问题在于,该文件被改变了,所以你有一个&LT编译后回更换令牌; replaceregexp&GT; ... SOOO丑,我不想触及源$ C ​​$ C与正则表达式。加上时序依赖。

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.

复制文件,替换原来的令牌,编译,用副本替换原有染色

相同的时间依赖的问题,除非嵌入在编译的目标。这是丑陋的也一样,因为我们所有的构建文件使用相同的进口compile目标。

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.

你对替代品的想法?

是否有任何的最佳做法吗?

Are there any best practices for this?

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

I sure hope I have missed a perfectly sane approach.

感谢您

修改
我们结束了使用清单存储在实施-版本属性中的版本号和系统版本,unsing MyClass.class.getPackage()。getImplementationVersion ()。我发现这个解决方案是答案之一<一个href=\"http://stackoverflow.com/questions/690419/build-and-version-numbering-for-java-projects-ant-cvs-hudson\"> 此线程 ,这是张贴在被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

推荐答案

我觉得一个更简单的方法是将有从一个简单的.properties读你的 Version.java 类包括在JAR文件中,只是生成此的.properties在编译时文件中的Ant构建。例如刚才生成的:

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

借助内置的&LT; buildnumber&GT; 任务中蚂蚁一半做这个已经(见第二个例子)。

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

这篇关于Ant构建过程中code修改最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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