Best way to integrate Git with Ant?

2020-02-17 05:43发布

问题:

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)?

回答1:

Doesn't look like there were a set of Ant tasks for Git.

This blog talks about some rudimentary tasks for working with Git.



回答2:

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:

Here is Git Ant Tasks via JGit: http://aniszczyk.org/2011/05/12/git-ant-tasks-via-jgit/ .



回答4:

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.



回答5:

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.



回答6:

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



回答7:

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}.