Add Git SHA1 property in Apache ANT build.xml

less than 1 minute read

Create git.SHA1 property in build.xml file.

<available file=".git" type="dir" property="git.present"/>
<target name="" description="Store git info" if="git.present">

    <exec executable="git" outputproperty="git.SHA1" failifexecutionfails="false" errorproperty="">
        <arg value="log"/>
        <arg value="--pretty=oneline"/>
        <arg value="-n1"/>
    <condition property="git.version" value="${git.SHA1}" else="unknown">
            <isset property="git.SHA1"/>
            <length string="${git.SHA1}" trim="yes" length="0" when="greater"/>

    <echo message="print git log : " />
    <echo message="${git.SHA1}" />


How to lookup the latest git commit hash from an ant build script

Leave a Comment