如何从具有依赖项的任务执行另一个任务

时间:2014-07-07 06:05:49

标签: gradle

我有多个gradle文件来运行测试套件。每个gradle文件都有多个任务,并定义了依赖项。并且将有一个与文件名同名的任务来运行所有这些任务。

实施例

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)

现在我想写一个常见的test.gradle文件,它根据提供的参数加载给定的测试gradle文件并运行任务

gradle -b test.gradle -Ptest_to_run = foo_test

如何在test.gradle中创建一个任务,该任务将运行foo_test.gradle的foo_test任务及其依赖项(testBlockA-C)

当我阅读任务[&#39; foo_test&#39;]时,执行()将不起作用,因为它不执行dependsOn任务。

1 个答案:

答案 0 :(得分:2)

在main build.gradle文件中,您可以在build.gradle文件中读取提供的-P属性:

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

此代码段基于input属性应用了一个buildscript,并声明了要运行的defaultTask。

相关问题