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?
To run a Gradle command, open a command window on the project folder and enter the Gradle command. Gradle commands look like this: On Windows: gradlew <task1> <task2> … e.g. gradlew clean allTests.
Go to Gradle console -> Gradle icon that says Execute Gradle task -> a pop-up opens -> attach your project -> write the task in the second field -> ok. Show activity on this post. Click "View" option top on the Android studio then click "Tool Windows" after "Gradle"; that's it.
The gradle command will run Gradle on the gradle build script located in the same directory as the command prompt is located in. That means, that to run gradle on a specific gradle build script you must change directory in the command prompt into the directory where the build script is located.
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.
If you need to save the command output you could do this:
def gitBranchA = "git branch -a".execute().text.trim()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With