How to parse output from git clone with gradle

917 views git
4

I need to parse out the project name from the output of the command execution git clone https://user:pass@domain.com/testing/project.git and reprint it like cloned project: "project-name" in gradle.

When I run the command simply in terminal without gradle, I get the output pattern like Cloning into 'project'... followed by some additional irrelevant lines. Project name is always in single quotation.

I checked some suggestions mentioned in other threads, but still the default output is getting printed out on the terminal. So I can't grab the project name from there.

Here is my 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()
    }
}

Any idea?

answered question

1 Answer

4

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
}

posted this

Have an answer?

JD

Please login first before posting an answer.