Ant Tasks for Git

Ant has tasks for CVS and Subversion, but none that I could find for Git. I threw together these simple Ant macros to get started:

<macrodef name = "git">
    <attribute name = "command" />
    <attribute name = "dir" default = "" />
    <element name = "args" optional = "true" />
    <sequential>
        <echo message = "git @{command}" />
        <exec executable = "git" dir = "@{dir}">
            <arg value = "@{command}" />
            <args/>
        </exec>
    </sequential>
</macrodef>

<macrodef name = "git-clone-pull">
    <attribute name = "repository" />
    <attribute name = "dest" />
    <sequential>
        <git command = "clone">
            <args>
                <arg value = "@{repository}" />
                <arg value = "@{dest}" />
            </args>
        </git>
        <git command = "pull" dir = "@{dest}" />
    </sequential>
</macrodef>

The first one, “git” just runs git with whatever command you provide to it (clone, pull, etc) along with any arguments you pass to it. Clone:

<git command = "clone">
    <args>
        <arg value = "git://github.com/280north/ojunit.git" />
        <arg value = "ojunit" />
    </args>
</git>

And pull:

<git command = "pull" dir = "repository_path" />

(Other git command will likely work, these are just the ones I needed)

The second one, “git-clone-pull” uses the first one to clone a repository then pull from it. This effectively clones the repository if it hasn’t already been cloned, otherwise it pulls. However, since ant is fairly limited in what sorts of conditional execution you can perform, it just does both (clone will fail if it’s already been cloned, and pull will always be executed, even immediately after a the initial clone). Obviously not ideal, but it works, and I couldn’t figure out a better way without writing actual code.

<git-clone-pull repository="git://github.com/280north/ojunit.git" dest="ojunit" />

There is plenty of room for improvement, but I suspect a proper Ant task written in Java is the right way to go.

  • Markus

    is there going to be an appstore / cydia / installer app with your wifi-acelerometer transmittance thing? That looks really cool, and I am sure people would appreciate it. I certainly would, there’s nothing like this around yet!! Been looking a while, though.

    Thanks,

    Markus

  • Laurent Pireyn

    Nice post!

    My two cents:
    – What if you need to authenticate with the remote repository? There is no parameter to do that – and that would not be secure anyway. A SSH agent would work but only if the server authorizes your public key. At the very least, you could have Ant ask input from the user, but that prevents unattended runs.
    – An Ant task would either call the git executable, with all the issues that it implies (differences between platforms, availability of the executable, etc.), or it could use the only Java (partial) implementation of git I’m aware of at this time: egit.
    – Some CI programs (Hudson, Cruise Control) have a git plugin. Since they’re open source, it might be worth a look… My guess is that they simply call the executable.

  • http://www.cousinit.org/ Tech Support

    I am able to use svn checkout from commandline from a url. But the same task when i add in my ant build file, the build file fails telling me that the url@HEAD does not exist.

  • Timo

    I have thrown together something similar, a macro for figuring out the revision and whether the working copy is clean:

  • Will Milspec

    You know you’ve written a good post if 3 years later, google lists it in its top search results.

    from our limited experiencing, adding the following attribute makes for “least surprising” builds:
       
         …
         
        

    Most times I dont care if the git command fails or succeeds, but sometimes I want it to “fail noisily”, e.g. if ant can’t fetch/merge from git:

     
     

    • Kepler Sticka-Jones

      Holy Shit! Five years later and still up there.

  • Justin

    Great Post! Cheers this has helped me out a lot!

  • Jay Patel

    Nice Macros. Really helpful. I however, am not getting the output from git when I do git clone something.

    Like remote: counting Objects, etc. etc.

    Is it possible to redirect output from git to console, just so that we can see if there is some progress here, and is not stuck somewhere.

  • jp

    Very nice example!

    • Haritha

      I have followed your post and executed like below, but it is throwing an exception like below:
      C:UsersHarithaDesktopH2Hconfbuild.xml:23:
      The directory
      C:UsersHarithaDesktopH2Hconfhttps:github.comlibgit2libgit2.git
      does not exist ,

      am sending input https:github.comlibgit2libgit2.git but windows path is getting added, how to solve this issue

      • Rajashekar Reddy

        Hi,

        i am also geeing same issue, please share me the result if resolved the issue.
        Thanks,
        Raj

  • Haritha

    I have followed your post and executed like below, but it is throwing an exception like below:

    C:UsersHarithaDesktopH2Hconfbuild.xml:23:
    The directory C:UsersHarithaDesktopH2Hconfhttps:github.comlibgit2libgit2.git
    does not exist ,

    am sending input https:github.comlibgit2libgit2.git but windows path is getting added, how to solve this issue

  • GaryP

    Here’s some more targets that I found handy. We name our tags with XXXX_FFFFF_NNN where NNN is a number that increases. This looks up the last tag, you can do a next-tag with will increment it, and then a remove last tag. (If it botches it,)

    Probably could be better, but I’m only just learning ant, so I didn’t figure out how to store the results…optionally.

    Updating environment with git repository

    Fetching the last tag in this branch.

    Fetching the next tag in this branch.

    Fetching the next tag in this branch.

    • GaryP

      (use the & quote ; without the spaces for the double quotes.)