简体   繁体   中英

Running shell command from Gradle script

I am having the darndest time trying to figure out how to run a shell command from Gradle, since it seems like Gradle makes it very difficult to do this.

Here is the command:

git branch --merged | grep -v \* | grep -v master | grep -v develop | grep -v dmz | xargs git branch -D

It's just a convenience command to clean up local branches that have been merged.

Here is the task I created:

task gitCleanLocalBranches {
    doLast {
        exec {
            workingDir '.'
            commandLine 'git branch --merged | grep -v \\* | grep -v master | grep -v develop | grep -v dmz | xargs git branch -D'

        }
    }
}

The task fails with:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':gitCleanLocalBranches'.
> A problem occurred starting process 'command 'git branch -a''

* Try:
Run with --info or --debug option to get more log output.

* Exception is:
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':gitCleanLocalBranches'.
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:100)
        ...
Caused by: org.gradle.process.internal.ExecException: A problem occurred starting process 'command 'git branch --merged | grep -v \* | grep -v master | grep -v develop | grep -v dmz | xargs git branch -D''
        at org.gradle.process.internal.DefaultExecHandle.execExceptionFor(DefaultExecHandle.java:222)
        ... 3 more
Caused by: net.rubygrapefruit.platform.NativeException: Could not start 'git branch --merged | grep -v \* | grep -v master | grep -v develop | grep -v dmz | xargs git branch -D'
        at net.rubygrapefruit.platform.internal.DefaultProcessLauncher.start(DefaultProcessLauncher.java:27)
        ... 4 more
Caused by: java.io.IOException: Cannot run program "git branch --merged | grep -v \* | grep -v master | grep -v develop | grep -v dmz | xargs git branch -D" (in directory "/home/wlaw/sterlib"): error=2, No such file or directory
        at net.rubygrapefruit.platform.internal.DefaultProcessLauncher.start(DefaultProcessLauncher.java:25)
        ... 6 more
Caused by: java.io.IOException: error=2, No such file or directory
        ... 7 more

So I figured that the command is too complicated so I tried something simpler, and changed commandLine to:

commandLine 'git branch -a'

But I got the exact same error. Why is Gradle not able to find anything in the PATH environment variable?

The command to execute and its arguments must be separate parameters to pass to commandLine , like this:

commandLine 'git', 'branch', '-a'

If you want to execute a complicated pipeline as in your first example, you can wrap it in a shell script.

I cannot test this, but I think this should work as well:

commandLine 'sh', '-c', 'git branch --merged | grep -v -e \* -e master -e develop -e dmz | xargs git branch -D'

Note: I took the liberty and simplified the grep a bit.

Lastly, you could also create a Git alias in your .gitconfig to wrap the complex pipeline.

如果您需要保存命令输出,您可以这样做:

def gitBranchA = "git branch -a".execute().text.trim()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM