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
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
}
-
1is there any way to clone a specific feature branch of the repo? Commented Dec 7, 2015 at 9:39
-
1Is 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 Commented May 2, 2016 at 22:05
-
1
-
1NOTE:
gradle-git
is dead. You wanna usegrgit
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
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')
}
-
1is there any way to clone a specific feature branch of the repo? Commented Dec 7, 2015 at 9:39
-
-
@ObaidMaroof, may be to checkout operation using will help grgit.checkout(branch: 'existing-branch').– AbhijeetCommented 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 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
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:
-
-
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/…– mpierceCommented Dec 3, 2014 at 22:32
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();
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.
-
2Yeah, 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