Franz Kafka
Franz Kafka

Reputation: 10831

java version control system with ant

I have an ANT-Skript that builds my release. It has to use the version number, so it is typed in the ANT-Script.

At the same time there is a Java file with contains the same version number, this number is needed by the software internally.

Is there any way to just have one place to store the version number?

Upvotes: 1

Views: 451

Answers (4)

kdgregory
kdgregory

Reputation: 39606

McDowell gave the "standard" answer of using Ant to insert the version number into a Java file and/or classpath resource. 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():

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.

Upvotes: 1

Jan Zyka
Jan Zyka

Reputation: 17888

Maybe the ANT BuildNumber task is what you are looking for? It maintains single file with the version number. No problem to read it from java ...

Upvotes: 1

Andreas Dolk
Andreas Dolk

Reputation: 114777

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. You only have to make sure, the version file is on ant's classpath so that the task can access it:

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}

Upvotes: 1

McDowell
McDowell

Reputation: 108899

You could use a filter or replace task to write it to a resource file (e.g. a dedicated properties file) that would be available on your application's classpath.

Upvotes: 2

Related Questions