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

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: java.io.IOException: Cannot run program “source” (in directory “/home/vbharakhada/sandbox/b-pubmind/srcroot/pubmindApp”): java.io.IOException: error=2, No such file or directory

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

1 Like

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

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

It seems that

exec {

executable ‘ls’

args ‘-ltrh’

}

This thing works, however,

exec {

executable ‘source’

args “${root}/etc/localBuildPorts”

}

this doesn’t work

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

Can you elaborate? are you saying I should have

executable ‘shell’

??

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.

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

All right… Thank you…

“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.