24

Suppose I have a gradle build script and want to write a task to clone a remote git repository. How do I do that?

5 Answers 5

26

The cloning can be done using the Gradle-git plugin. To use the plugin you should download it first:

buildscript {
  repositories { mavenCentral() }
  dependencies { classpath 'org.ajoberstar:gradle-git:0.2.3' }
}

Then write a task like this one:

import org.ajoberstar.gradle.git.tasks.*

task cloneGitRepo(type: GitClone) {
        def destination = file("destination_folder")
        uri = "your_git_repo_uri"
        destinationPath = destination
        bare = false
        enabled = !destination.exists() //to clone only once
}
5
  • 1
    is there any way to clone a specific feature branch of the repo? Commented Dec 7, 2015 at 9:39
  • 1
    Is this Answer correct still? Seems like since Gradle 2.7 the Git plug-in is no longer needed. Is that correct? github.com/gradle/gradle/pull/386
    – haysclark
    Commented May 2, 2016 at 22:05
  • 1
    how do you specify the branch to clone? Commented Jul 28, 2016 at 13:51
  • 1
    NOTE: gradle-git is dead. You wanna use grgit from ajoberstar.org/grgit/grgit-gradle.html, and it includes a way to checkout a specific branch after cloning :) Commented Nov 6, 2017 at 19:43
  • Note 2: the grgit author also suggests that plugin is heading towards deprecation and other means should be used: github.com/ajoberstar/grgit Commented Jul 29 at 15:13
19

With newer versions of gradle-git (0.7.0 and up), you would create the task like this:

import org.ajoberstar.grgit.*

task clone << {
  Grgit.clone(dir: file('build/repo'), uri: '[email protected]:user/repo.git')
}
5
  • 1
    is there any way to clone a specific feature branch of the repo? Commented Dec 7, 2015 at 9:39
  • How to use clone operation in custom gradle plugin ?
    – sver
    Commented Feb 18, 2016 at 6:30
  • @ObaidMaroof, may be to checkout operation using will help grgit.checkout(branch: 'existing-branch').
    – Abhijeet
    Commented Apr 27, 2016 at 6:15
  • This is the kind of usage example I’d love to see in the official docs—would have saved me a lot of try-and-error
    – user149408
    Commented Jul 17, 2017 at 20:13
  • unfortunate, page not found anymore. I want to check that solution out, please share if anyone has it. Commented Jun 19, 2023 at 6:25
6

The aforementioned Gradle-git plugin seems to have moved on from providing straightforward "clone this repo to that directory" functionality, so I wrote a simple task that does just this:

https://github.com/palominolabs/gradle-git-clone-task

2
  • Thank you for that. How would I call that inside another task?
    – vsp
    Commented Dec 2, 2014 at 17:43
  • The task-ish way to do this would be to depend on another task that is of type GitCloneTask. However, I don't see any reason why you couldn't create a GitCloneTask object and invoke setUpRepo() if you wanted to! Set properties as desired and fire away: github.com/palominolabs/gradle-git-clone-task/blob/master/src/…
    – mpierce
    Commented Dec 3, 2014 at 22:32
5

There is a Git plugin - docs here: Gradle-git. The plugin has a clone method: GitClone

Probably something along the lines of:

GitClone clone = new GitClone();
clone.setUri("http://remote.repository/");
clone.setDestinationPath("//local/path");
clone.setBare(false);
clone.cloneRepo();
4

The Gradle-git plugin has a GitClone task that should help. I can't help you on how to use it since I don't know Gradle.

1
  • 2
    Yeah, I found it already. You see, I'm a newbie to Gradle, so I need a detailed instruction on how to use the plugin to clone a repo. Gonna upvote your answer anyway, thank you Commented Dec 5, 2012 at 9:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.