Gradle: how to clone a git repo in a task?

2019-01-17 11:16发布

问题:

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

回答1:

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
}


回答2:

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: 'git@github.com:user/repo.git')
}


回答3:

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



回答5:

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.



标签: git gradle