在" connectedAndroidTest"之后运行gradle任务X.任务成功

时间:2016-01-14 18:38:44

标签: android gradle android-gradle

我希望在taskX任务完成后运行我的gradle connectedAndroidTest,但只有在connectedAndroidTest传递所有测试时才会运行。

我怎么能做到这一点?

2 个答案:

答案 0 :(得分:3)

您需要利用finalizedBy以及特定任务的状态检查。以下是它的完成方式:

task connectedAndroidTest << {
  logger.lifecycle("Running $name")
  if (project.hasProperty('lol')) {
    throw new Exception('lol')
  }
}

task taskX << {
  def failure = tasks.connectedAndroidTest.state.failure
  if(!failure) {
    logger.lifecycle("$name is finalizer")
  } else {
    logger.lifecycle("$tasks.connectedAndroidTest.name failed, nothing to do.")
  }
}

connectedAndroidTest.finalizedBy(taskX)

现在如果运行:

gradle cAT

输出将是:

:connectedAndroidTest
Running connectedAndroidTest
:taskX
taskX is finalizer

BUILD SUCCESSFUL

Total time: 1.889 secs

This build could be faster, please consider using the Gradle Daemon: https://docs.gradle.org/2.8/userguide/gradle_daemon.html

当:

gradle cAT -Plol
运行

,然后输出为:

:connectedAndroidTest
Running connectedAndroidTest
:connectedAndroidTest FAILED
:taskX
connectedAndroidTest failed, nothing to do.

FAILURE: Build failed with an exception.

* Where:
Build file '/Users/opal/tutorial/stackoverflow/34797260/build.gradle' line: 4

* What went wrong:
Execution failed for task ':connectedAndroidTest'.
> lol

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 1.931 secs

Here可以找到一个演示。

  [1]: https://docs.gradle.org/current/javadoc/org/gradle/api/Task.html#finalizedBy(java.lang.Object...)

答案 1 :(得分:0)

您正在描述任务依赖项。您可以定义依赖于另一个的任务,因此在执行时会触发另一个任务:

task taskX(dependsOn: 'connectedAndroidTest') << {
   //do something
}

然后只需运行您的任务,connectedAndroidTest将首先执行:

gradle taskX

Task Dependency docs