Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does gradle not run my shell script?

I'm almost certain i am overlooking something.

I have an android gradle project with a build.gradle file. Inside here, I specify the task:

task doSomething(type: Exec) {
    println("okay clearly you have got to be getting in here")
    commandLine 'sh /Users/dzt/Desktop/create_a_file_on_desktop.sh'
}

and that doesn't run at all. the shell file just literally does:

#!/bin/sh
echo "hi" > /Users/dzt/Desktop/i_am_a_byproduct.txt

and i ran chmod u+x on it so it is executable (i double checked on regular bash shell).

I also tried to use the groovy command:

"cd ../ && sh /Users/dzt/Desktop/create_a_file_on_desktop.sh".execute()

which does not work either. I'm a little stumped. i do NOT see the output file. however, i do see my print statement in the gradle console.

What is going on here?

** EDIT **

okay, i drilled it down more ->

cd ../ does not work at all. why is this? i need to use a relative path, at least relative to this directory

like image 233
David T. Avatar asked May 15 '15 03:05

David T.


People also ask

How do I run a script in Gradle?

Running Gradle Commands 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.

Why is Gradle not working?

In some cases when your Gradle files are deleted or corrupted you will not be able to download new Gradle files in android studio. In this case, we have to delete the Gradle files which are present already and then again sync your project to download our Gradle files again.

How do I run Gradle in terminal?

Press ⌃⌃ (macOS), or Ctrl+Ctrl (Windows/Linux), type "gradle" followed by the gradle task name or names. We can, of course, run Gradle commands from the terminal window inside IntelliJ IDEA. Open this with ⌥F12 (macOS), or Alt+F12 (Windows/Linux).

Does Gradle run also build?

You can execute multiple tasks from a single build file. Gradle can handle the build file using gradle command. This command will compile each task in such an order that they are listed and execute each task along with the dependencies using different options.


1 Answers

The call must be

commandLine 'sh', '/Users/dzt/Desktop/create_a_file_on_desktop.sh'

or else this is considered one command. But you want to start the sh with the script as param. On the other hand, since you have set the execute-bit, you can as well just call the shell script directly.

See http://gradle.org/docs/current/dsl/org.gradle.api.tasks.Exec.html

Running cd like you want with cd ../ && sh script does also not work like this, since && is a shell script command. If you want to run like this, you have to run the shell and make it run as a command. E.g.

commandLine 'sh', '-c', 'cd ~/scripts && sh myscript.sh'
like image 159
cfrick Avatar answered Sep 25 '22 16:09

cfrick