如何使用gradle解析git clone的输出

时间:2018-11-03 22:30:09

标签: git gradle

我需要从命令执行git clone https://user:pass@domain.com/testing/project.git的输出中解析出项目名称,然后像Gradle中的 cloned project:“ project-name” 一样重新打印。

当我在没有gradle的终端中简单地运行命令时,会得到类似 Cloning into'project'... 的输出模式,后面是一些不相关的行。项目名称始终以单引号引起来。

我检查了其他线程中提到的一些建议,但默认输出仍在终端上打印出来。所以我无法从那里获取项目名称。

这是我的build.gradle:

def getProjectName = { ->
    def cloneOutput = new ByteArrayOutputStream()
    exec {
        commandLine "git", "clone", "https://user:pass@domain.com/testing/project.git"
        standardOutput = cloneOutput
    }

    return cloneOutput.toString().trim()
}

task printProjectName {
    doLast {
        println getProjectName()
    }
}

有什么主意吗?

1 个答案:

答案 0 :(得分:0)

可以使用Gradle-git plugin进行克隆。要使用该插件,您应该先下载它:

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

然后编写这样的任务:

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
}