How to execute a shell script from inside a Test task?

I have a “Exec” type task, which executes a shell script, but this script gets executed for all tasks like “clean”, and all my “Test” type tasks, even though Im calling the Exec task only from inside of my Test task?

How to ensure it runs only on specified Test task but not for every task?

Please share either the relevant buildscript snippet or (even better) a small example project showing the problem. What you describe sounds like your buildscript has one of the following errors:

  • all tasks depend on the exec task
  • calls the exec method at configuration time


Below is the extract from my build.gradle. My task shellTest, even though called only from my test task “testProvisioningUI”, would run before all the other tasks like clean, testSimpleTest etc.

I run ./gradle clean, it would still run my shellTest task.
I run ./gradle testSimpleTest, it would still run my shellTest task, eventhough shellTest is not called from inside testSimpleTest

task shellTest(type :Exec){
commandLine ‘sh’,’./’

task testProvisioningUI(type: Test) {
** tasks.shellTest.execute()**
** useTestNG() {**
** includeGroups ‘watchDog’**
** //includeGroups ‘negativeHome’**
** }**

task testSimpleTest(type: Test) {
** useTestNG() {**
** includeGroups ‘runSimpleTest’**
** }**

You are calling execute() on that Exec task during the configuration phase. This is the phase when Gradle evaluates your build script and configures the task graph. You should not be executing things during that phase.

What you want is a task dependency:

task testProvisioningUI(type: Test) {
  dependsOn shellTest

I recommend you to have a look at the Buildscript Basics in the user guide.

Apologize for the late comments.
The suggestion works for me, by adding dependsOn .
Thanks a lot, @st_oehme
SO many working solutions, this makes this forum a great place to learn n teach!!!