How to execute shell command? source or dot doesn't work with exec

(vipss78) #1

Can not execute the following task

exec {

commandLine ‘source’

args “/home/user/mySource”

} I was expecting it to do similar to

source /home/user/mySource pr

. /home/user/mySource

but for both the operation it throws the following exception

Caused by: Cannot run program “source” (in directory “/home/vbharakhada/sandbox/b-pubmind/srcroot/pubmindApp”): error=2, No such file or directory

I also tried it with String.execute, I am getting the same exception

(Peter Niederwieser) #2

As explained in the DSL reference, ‘commandLine’ is the full command line. What you want is ‘executable’.

(vipss78) #3

so just replacing commandLine with executable should work? Can it execute shell builtin? Its not a program like -ls

(vipss78) #4

It seems that

exec {

executable ‘ls’

args ‘-ltrh’


This thing works, however,

exec {

executable ‘source’

args “${root}/etc/localBuildPorts”


this doesn’t work

(Peter Niederwieser) #5

A shell builtin is not an executable. In that case the executable is the shell.

(vipss78) #6

Can you elaborate? are you saying I should have

executable ‘shell’


(vipss78) #7

yeah… but those source values are only available within that subprocess… for example

$ sh -c ‘source $ROOT/buildlocalBuildPorts’ $ echo $FINAL_LOCALTEST_TOMCAT_HTTP_PORT

$ bash -c ‘source $ROOT/buildlocalBuildPorts’ $ echo $FINAL_LOCALTEST_TOMCAT_HTTP_PORT

$ zsh -c ‘source $ROOT/buildlocalBuildPorts’ $ echo $FINAL_LOCALTEST_TOMCAT_HTTP_PORT

$ source $ROOT/buildlocalBuildPorts $ echo $FINAL_LOCALTEST_TOMCAT_HTTP_PORT 9090

the source values is not available outside. So even If I try it with

exec {
     executable "sh"
    args "-c","source ${root}/etc/localBuildPorts"
 println System.getenv()['FINAL_LOCALTEST_TOMCAT_HTTP_PORT']

the sourced variables wont be available to gradle. Correct me If I am wrong.

(Peter Niederwieser) #8

Sourcing a script to make environment variables visible to Gradle won’t work, because Gradle is the parent process.

(vipss78) #9

All right… Thank you…

(Peter Niederwieser) #10

“sh”, “bash”, “zsh”, or whatever shell you want to use. For example:

task hello(type: Exec) {
  executable "sh"
  args "-c", "echo 'hello from your shell'"

For more information, see the documentation of your shell.