Sundeep Gupta
Sundeep Gupta

Reputation: 1986

How to execute another task from a task with dependencies

I have multiple gradle files to run test suites. Each gradle file has multiple tasks with dependencies defined. And there will be a task with same name as file name to run all those tasks.

Example

foo_test.gradle

  task testBlockA << {
    // do some tests here
  }

  task testBlockB(dependsOn: testBlockC) << {
     // do some tests here
  }

  task testBlockC << {
    // do some stuff here
  }

  task foo_test(dependsOn: testBlockA, testBlockB)

Now I want to write a common test.gradle file which, based on argument provided, loads the given test gradle file and runs the task

gradle -b test.gradle -Ptest_to_run=foo_test

How to I create a task in test.gradle, which will run foo_test task of foo_test.gradle, along with its dependencies (testBlockA-C)

As I read tasks['foo_test'].execute() will not work as it does not execute the dependsOn tasks.

Upvotes: 2

Views: 2129

Answers (1)

Rene Groeschke
Rene Groeschke

Reputation: 28663

In your main build.gradle file, you can read the provided -P attribute in your build.gradle file:

if(project.hasProperty("test_to_run")){
     apply from:test_to_run
     defaultTasks test_to_run
} 

this snippet applies a buildscript based on the input property and also declares a defaultTask to run.

Upvotes: 2

Related Questions