Search code examples
antbuild-process

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


Solution

  • 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>