Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use ANT to update build number and inject into source code

In my build.xml file I am incrementing a build version number in a property file like so:

<target name="minor">
     <propertyfile file="build_info.properties">
         <entry key="build.minor.number" type="int" operation="+" value="1" pattern="00" />
         <entry key="build.revision.number" type="int" value="0" pattern="00" />
     </propertyfile>
</target>

I also have similar entries for the major and revision. (from Build numbers: major.minor.revision)

This works great. Now I would like to take this incremented build number and inject it into my source code:

    //Main.as
    public static const VERSION:String = "@(#)00.00.00)@";

By using:

<target name="documentVersion">
    <replaceregexp file="${referer}" match="@\(#\).*@" replace="@(#)${build.major.number}.${build.minor.number}.${build.revision.number})@" />
</target>

Now this sorta works. It does indeed replace the version but with the outdated version number. So whenever I run the ANT script the build_info.properties is updated to the correct version but my source code file is using the pre updated value.

I have echoed to check that indeed I am incrementing the build number before I call the replace and I have noticed that echoing:

<echo>${build.minor.number}</echo> 
//After updating it still shows old non updated value here but the new value in the property file.

So is there a way to retrieve the updated value in the property file so I can use it to inject into my source code?

Cheers

like image 264
Allan Avatar asked Aug 09 '10 12:08

Allan


People also ask

How do you trigger an Ant build?

To run the ant build file, open up command prompt and navigate to the folder, where the build. xml resides, and then type ant info. You could also type ant instead. Both will work,because info is the default target in the build file.

Why do we use Ant build?

The main known usage of Ant is the build of Java applications. Ant supplies a number of built-in tasks allowing to compile, assemble, test and run Java applications. Ant can also be used effectively to build non Java applications, for instance C or C++ applications.


2 Answers

So after spending hours not being able to solve this, I post this question and then figure it out 20 minutes later.

The problem was I had this at the top of my build file:

<property file="build_info.properties"/>

I guess it was due to scoping and that properties are immutable thus I was never able to update the value. Removing that line and then adding the following got it working perfectly:

<target name="injectVersion">
     <property file="build_info.properties"/>
     <replaceregexp file="${referer}" match="@\(#\).*@" replace="@(#)${build.major.number}.${build.minor.number}.${build.revision.number})@" />
</target>
like image 113
Allan Avatar answered Oct 14 '22 02:10

Allan


Why not just use <buildnumber/>?

like image 39
Øyvind Nordhagen Avatar answered Oct 14 '22 00:10

Øyvind Nordhagen