Dark Star1
Dark Star1

Reputation: 7413

How to run execute a command in project directory in gradle?

I have the following task

task runSomeTool(type: JavaExec, dependsOn: 'jar') {
....
    "git config remote.origin.url".execute().text.trim()
....
}

which returns and empty string.
When I change the command string to list all properties (git config --list), I see that it is listing the global config properties. This led me to discover that it runs in ${user.dir}/.gradle/daemon/6.8.3/ directory.
Is there anyway I can execute the command in $projectDir?

Thanks very much

Upvotes: 0

Views: 363

Answers (2)

Dark Star1
Dark Star1

Reputation: 7413

I ended up using the ProcessBuilder instead

public String gitProperty(String command, String... argsArray){
    def commandArgs = ["git", command]
    if (argsArray.length > 0)
        commandArgs+=argsArray.flatten()ProcessBuilder pb = new ProcessBuilder(commandArgs)
    // To make sure it executes in the correct directory
    pb.directory(new File ("${wDir}"))
    Process p = pb.start()
    //Wait for the process to finish
    p.waitFor()
    String result = new String(p.getInputStream().readAllBytes())
    return result.trim()
}

wDir, in this case, is the working directory one specifies.

Upvotes: 0

User51
User51

Reputation: 1109

You can check out the workingDir param, described here for a couple tasks:

task runSomeTool(type: Exec) {
    workingDir "${rootProject.projectDir}"
    commandLine 'git'
    args "status"
}

Upvotes: 2

Related Questions