0

I'm working on setting up an Ant file to grab the commit revision from Bitbucket and outputting that value into a name.properties file where I have a code.version=12345 line of code. I want to update that value with the current revision number. This is what I have right now:

<property name="site" value="${dir}" />
<property name="site.properties" value="../${site}/site.properties" />
<target name="get-core-version">
    <exec executable="git" outputproperty="git.version">
        <arg value="log" />
        <arg value="--pretty=format:%h" />
        <arg value="-1" />
    </exec>
    <propertyfile file="${site.properties}" comment="Core code revision number">
        <entry key="core.version" value="${git.version}" />
    </propertyfile>
</target>

Right now, when I run the above, it returns a 0. But, if I run it through command line, all is well and returns what I expect from it.

The result I'm getting from the above code inside the properties file is:

#Core code revision number
#Fri, 02 Oct 2015 16:10:40 -0400


core.version=b3cd3d7
pingeyeg
  • 632
  • 2
  • 6
  • 23
  • See also http://stackoverflow.com/questions/14401711/how-can-i-get-the-latest-commit-id-of-the-currently-checked-out-branch-in-git-u – Mark O'Connor Oct 03 '15 at 10:43

1 Answers1

1

code.version gets set by result property first (which is the exit code of the exec, btw). Ant doesn't allow property values to be changed so loadfile doesn't reset code.version. Try renaming the resultproperty from code.version to something else and moving the echo after the loadfile will get you what you want.

<project name="project" default="get-core-version" basedir=".">
    <property name="site" value="${dir}" />
    <property name="site.properties" location="${site}/site.properties" />

    <target name="get-core-version">
        <exec executable="git" failonerror="true" resultproperty="get.exit.code" output="${site.properties}">
            <arg value="log" />
            <arg value="--pretty=oneline" />
            <arg value="HEAD" />
            <arg value="-1" />
            <arg value="--" />
            <arg value="${site}" />
        </exec>
        <echo message="Git Status: ${get.exit.code}" />
        <loadfile srcFile="${site.properties}" property="core.version" />
        <echo message="Core Version: ${core.version}" />
    </target>
</project>

Once I change the site.properties as above and remove the property file line when I run: ant -Ddir=rice-tools-test I get:

Buildfile: /r/eghm_rice_20150804/build.xml

get-core-version:
     [echo] Git Status: 0
     [echo] Core Version: 25815faedd7e5c2d4bcf3ab358de36ebb8715def Extract Highlighting from WebDriverUtils to WebDriverHighlightHelper.

BUILD SUCCESSFUL
Total time: 0 seconds
EGHM
  • 2,144
  • 23
  • 37
  • So, I'm a little confused then. What does resultproperty do? I thought it was supposed to store the return value of the git command so I can use that value? – pingeyeg Oct 02 '15 at 00:23
  • @pingeyg It stores the exit status of the executed program (git in this case). 0 means successful. https://en.wikipedia.org/wiki/Exit_status has some general information on exit status. – EGHM Oct 02 '15 at 00:30
  • Ok, I've made some changes and I do get a status code of 0, but the changes don't appear to be made. I've looked on Bitbucket and my local drive, but nothing has changed for the file site.properties. – pingeyeg Oct 02 '15 at 00:47
  • @pingeyeg updated my answer to get closer to what I think you want. What value are you using for ${site}? – EGHM Oct 02 '15 at 01:08
  • ${site} comes in as the directory name. – pingeyeg Oct 02 '15 at 01:25
  • So far, this is what I get from the Ant output: get-core-version: `[echo] Git Status: 0` `[loadfile] Do not set property core.version as its length is 0.` `[echo] Core Version: ${core.version}` – pingeyeg Oct 02 '15 at 13:22
  • I've now reworked my post a little bit based on your possible solution, but I'm still getting the same output. – pingeyeg Oct 02 '15 at 14:00
  • @pingeyeg updated again and included output I get with updates – EGHM Oct 02 '15 at 19:15
  • Thanks @EGHM. I was actually finally able to get the value returned that I want, but now my issue is starting the git command in the correct directory. Got any ideas for that? I've checked out macrodefs, but that seems pretty confusing. Anyway, I've updated my side to reflect what I have now. – pingeyeg Oct 02 '15 at 20:07
  • @pingeyeg have you tried setting the exec task dir attribute? That should control the directory the command is run from. – EGHM Oct 02 '15 at 20:29
  • That worked! Totally missed that attribute. Thanks! – pingeyeg Oct 02 '15 at 21:35