Skip to content

Instantly share code, notes, and snippets.

@alex-ac
Created December 4, 2012 09:40
Show Gist options
  • Save alex-ac/4202217 to your computer and use it in GitHub Desktop.
Save alex-ac/4202217 to your computer and use it in GitHub Desktop.
Ant and git integration.
<?xml version="1.0" encoding='utf-8'?>
<project name="test" default="version">
<taskdef resource="net/sf/antcontrib/antcontrib.properties"/>
<available file=".git" type="dir" property="git.present"/>
<available file="version.properties" type="file" property="version.present"/>
<target name="versiongit" if="git.present">
<exec executable="git" outputproperty="git.commit" failonerror="true">
<arg value="rev-parse"/>
<arg value="HEAD"/>
</exec>
<exec executable="git" outputproperty="temp.git.tag" failonerror="false"
resultproperty="git.describeresult">
<arg value="describe"/>
<arg value="--abbrev=0"/>
</exec>
<if>
<equals arg1="${git.describeresult}" arg2="0"/>
<then>
<exec executable="git" outputproperty="git.tag.commit">
<arg value="rev-parse"/>
<arg value="${temp.git.tag}"/>
</exec>
<if>
<equals arg1="${git.commit}" arg2="${git.tag.commit}"/>
<then>
<property name="temp.git.version" value="${git.tag}"/>
</then>
<else>
<property name="temp.git.version" value="${git.tag}-${git.commit}"/>
</else>
</if>
</then>
<else>
<property name="git.tag" value="none"/>
<property name="temp.git.version" value="${git.commit}"/>
</else>
</if>
<exec executable="git" resultproperty="git.uncommited" failonerror="false">
<arg value="diff-index"/>
<arg value="--quiet"/>
<arg value="--cached"/>
<arg value="HEAD"/>
</exec>
<exec executable="git" resultproperty="git.untracked" failonerror="false">
<arg value="diff-files"/>
<arg value="--quiet"/>
</exec>
<if>
<not>
<and>
<equals arg1="${git.uncommited}" arg2="0"/>
<equals arg1="${git.untracked}" arg2="0"/>
</and>
</not>
<then>
<buildnumber/>
<property name="git.version" value="${temp.git.version}-b${build.number}"/>
</then>
<else>
<delete file="build.number"/>
<property name="build.number" value="0"/>
<property name="git.version" value="${temp.git.version}"/>
</else>
</if>
<propertyfile file="version.properties" comment="Project version information">
<entry key="git.commit" value="${git.commit}"/>
<entry key="git.tag" value="${git.tag}"/>
<entry key="git.version" value="${git.version}"/>
<entry key="build.number" value="${build.number}"/>
</propertyfile>
</target>
<target name="versionproperties" unless="git.present">
<fail unless="version.present" message="No version information found!"/>
<loadproperties srcFile="version.properties"/>
<fail unless="git.commit" message="No git commit information found!"/>
<fail unless="git.tag" message="No git tag information found!"/>
<fail unless="git.version" message="No git version information found!"/>
</target>
<target name="version" depends="versiongit,versionproperties">
<echo>Version: ${git.version}</echo>
<echo>Tag: ${git.tag}</echo>
<echo>Commit: ${git.commit}</echo>
<echo>Build: ${build.number}</echo>
</target>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment