简体   繁体   English

Java版本控制系统与蚂蚁

[英]java version control system with ant

I have an ANT-Skript that builds my release. 我有一个构建我的发行版的ANT-Skript。 It has to use the version number, so it is typed in the ANT-Script. 它必须使用版本号,因此必须在ANT脚本中键入。

At the same time there is a Java file with contains the same version number, this number is needed by the software internally. 同时,有一个Java文件,其中包含相同的版本号,软件内部需要此号。

Is there any way to just have one place to store the version number? 有什么办法可以只在一个地方存储版本号吗?

您可以使用过滤器替换任务将其写入应用程序的类路径上可用的资源文件(例如,专用属性文件)。

An easy solution: write a small ant task that reads the version number from the version class (I guess it has a static getter method) and stores the version number in an ant property. 一个简单的解决方案:编写一个小的ant任务,该任务从版本类读取版本号(我猜它具有静态的getter方法),并将版本号存储在ant属性中。 You only have to make sure, the version file is on ant's classpath so that the task can access it: 您只需确保版本文件位于ant的classpath上,以便任务可以访问它:

public GetVersion extends Task {
  public void execute() {
    getProject().setProperty("VERSIONNUMBER", Version.getNumber());
  }
}

In the ant script, define the task ( <taskdef> ), execute it and find the versionnumber in property ${VERSIONNUMBER} 在ant脚本中,定义任务( <taskdef> ),执行该任务,然后在属性${VERSIONNUMBER}找到版本号。

Maybe the ANT BuildNumber task is what you are looking for? 也许您正在寻找ANT BuildNumber任务? It maintains single file with the version number. 它维护带有版本号的单个文件。 No problem to read it from java ... 从Java读取它没问题...

McDowell gave the "standard" answer of using Ant to insert the version number into a Java file and/or classpath resource. McDowell给出了使用Ant将版本号插入Java文件和/或类路径资源的“标准”答案。 However, I've never liked that approach. 但是,我从未喜欢过这种方法。

An alternative, one that leverages Java's "it's ready to run as soon as it's compiled" behavior, is to give your Version class a main() : 一种替代方法是利用Java的“它在编译后即可运行”的行为,为您的Version类提供一个main()

public class Version
{
   public final static int MAJOR    = 1;
   public final static int MINOR    = 0;
   public final static int PATCH    = 0;

   public final static String VERSION_STRING = MAJOR + "." + MINOR + "." + PATCH;

   public static void main(String[] argv)
   throws Exception
   {
      System.out.println(VERSION_STRING);
   }
}

You would then invoke this with Ant's <java> task, saving the output in a property. 然后,您将使用Ant的<java>任务调用此方法,将输出保存在属性中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM