I am looking for the best way to integrate Git with Ant. Is there a widely used Ant task for Git? Does anyone have any experience using Git through Ant (e.g. dedicated task, exec call, etc)?
-
3A more fine grained question is http://stackoverflow.com/questions/2974106/how-to-lookup-the-latest-git-commit-hash-from-an-ant-build-script. The answers there might also be helpful here. – koppor Dec 16 '12 at 16:23
7 Answers
Ant supports the exec command that you can use to pass any command (including Git) to the command line for execution. You can always fall back on that.

- 3,856
- 1
- 21
- 12
Doesn't look like there were a set of Ant tasks for Git.
This blog talks about some rudimentary tasks for working with Git.

- 11,875
- 18
- 85
- 108

- 62,090
- 32
- 125
- 150
-
1here is Git Ant Tasks via JGit http://aniszczyk.org/2011/05/12/git-ant-tasks-via-jgit/ – Mike Henke Sep 27 '11 at 14:14
-
Here is Git Ant Tasks via JGit: http://aniszczyk.org/2011/05/12/git-ant-tasks-via-jgit/ .

- 11,875
- 18
- 85
- 108

- 864
- 1
- 8
- 22
Look at JGit-Ant. Unfortunately jgit-ant tasks project hasn't all main git actions, you can find additional info here.
For java developers: you can easily write git-ant-commands yourself with using jgit as in this examples.

- 11,875
- 18
- 85
- 108

- 2,261
- 2
- 28
- 28
It looks like there has been some additional, unofficial work done on Ant tasks for git:
- http://github.com/newtriks/Ant-Funk (and blog post http://www.newtriks.com/?p=910)
- http://github.com/FrancisVarga/ant-git-macros
I don't have experience with these, but they appear more fleshed out than tlrobinson's.

- 4,572
- 4
- 39
- 49
Use a combination of the JGit library with some <script language="javascript">
code (I used Rhino lubrary but you could equally use Groovy, etc).

- 6,887
- 2
- 50
- 45
Time ago I have unsuccessfully looked for ready in use ways to integrate Git and Ant. I needed possibility to create a build with the name of the Git branch. Finally I came to the following solution:
The excerpt from the real build.xml
file:
<target name="-check-git-branch-name"
if="using.git"
>
<exec executable="bash" logError="true" failonerror="true"
outputproperty="git-branch-name">
<arg value="./bin/git-branch-name.sh" />
</exec>
</target>
The whole content of the file ./bin/git-branch-name.sh
#!/bin/bash
# This script is the part of integration GIT to ANT. Once launched it
# should return the name of the current branch or the current commit (if
# GIT is the detached HEAD mode). Further the printed name is appended to
# the name of the resulting directory. To initialize this feature you need
# to run ANT with the option "-Dusing.git=".
exec 2>/dev/null
git rev-parse --abbrev-ref HEAD | grep -v HEAD || git rev-parse HEAD
Invocation is similar to:
ant TARGET options -Dusing.git=
When ${using.git}
is declared, Ant calls the task -check-git-branch-name
to gather the name of a branch (or a commit's number if Git is in detached mode) and generates the build with the appended name of the Git branch (or commit's hnumber), for example build/TARGET-${git-branch-name}
.

- 1,097
- 11
- 28