Over a million developers have joined DZone.
Platinum Partner

Use a Single Version Number for Ant and Java (Bonus: GWT)

· Java Zone

The Java Zone is brought to you in partnership with AppDynamics. Discover how AppDynamics steps in to upgrade your performance game and prevent your enterprise from these top 10 Java performance problems.

Problem: If your application has a version number, it should be accessible during runtime from Java (e.g., to display it in an “About this application” dialog) and during build time from Ant (e.g. to include it in file names). The solution is as follows.

Access the version from Java

Create the following properties file src/de/hypergraphs/hyena/core/client/bundle/BuildConstants.properties and put it into the class path.

    buildVersion=0.2.0
Access BuildConstants.properties as a Java resource. I usually construct the resource path relative to a Java class (a sibling of the file). That way the path to the properties file will always stay up-to-date, as long as I move the Java class with it.

Ant

Ant can read external property files as variable with the following statement.
    <property file="src/de/hypergraphs/hyena/core/client/bundle/BuildConstants.properties">
Additionally, you can insert the value of $buildVersion into a file while copying it, by using a filterset.
    <copy file="${data.dir}/index.html" todir="${version.dir}">
<filterset>
<!-- Replace @VERSION@ with the version -->
<filter token="VERSION" value="${buildVersion}">
</filterset>
</copy>
GWT

For client-side GWT, you can use constants. Then the version number is compiled directly into the JavaScript code. To do so, you add the following interface as a sibling of BuildConstants.properties.
package de.hypergraphs.hyena.core.client.bundle;

import com.google.gwt.i18n.client.Constants;

public interface BuildConstants extends Constants {
String buildVersion();
}

From http://2ality.blogspot.com/2010/07/use-single-version-number-for-ant-and.html

The Java Zone is brought to you in partnership with AppDynamics. AppDynamics helps you gain the fundamentals behind application performance, and implement best practices so you can proactively analyze and act on performance problems as they arise, and more specifically with your Java applications. Start a Free Trial.

Topics:

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}