gradle - 如何创建递归的Gradle任务?

标签 gradle

我想在根项目中创建一个任务,以便在执行任务时,它仅对具有该名称任务的子项目在子项目中执行相同名称的任务。

我不想对任务名称进行硬编码,因为以后可能会添加更多子项目。

task checkArtifacts
subprojects.findAll { subproject ->
  subproject.getTasksByName('checkArtifacts', false)
}.forEach { task ->
  checkArtifacts.dependsOn(task)
}

无法正常运行,因为它似乎迫使配置阶段结束并中断了一些可以更改配置阶段的插件。
task checkArtifacts
afterEvaluate {
  subprojects.findAll { subproject ->
    subproject.getTasksByName('checkArtifacts', false)
  }.forEach { task ->
    checkArtifacts.dependsOn(task)
  }
}

发出Could not determine the dependencies of task ':checkArtifacts'.

最佳答案

DomainObjectCollection中有一些方法可应用于将来对该集合的更新。

例如all

Executes the given closure against all objects in this collection, and any objects subsequently added to this collection. The object is passed to the closure as the closure delegate. Alternatively, it is also passed as a parameter.



matching

Returns a collection which contains the objects in this collection which meet the given closure specification. The returned collection is live, so that when matching objects are added to this collection, they are also visible in the filtered collection.



所以像
subprojects {
    tasks.matching { it.name == 'checkArtifacts' }.all { task ->
        rootProject.tasks.checkArtifacts.dependsOn task
    } 
} 

关于gradle - 如何创建递归的Gradle任务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43767241/

相关文章:

gradle - GOCD-我们可以为gradle/gradlew任务运行一个工作目录吗

Android Studio Gradle - 无法解析依赖项 : acra

java - 在命令行中运行 "./gradlew <task>"时,读取的是什么文件?

java - 在停止并重新启动我的应用程序时,我收到 JMX 地址已在使用中的错误

java - Gradle -> 如何从 WEB-INF/lib 中省略一些 jar

android - 在 Android Studio 2.2.3 中导入 aar 文件时出错

android - 是否可以添加一个外部维护的项目库而不进行复制?

android - Butterknife 使用 Gradle 插件 3.0 失败

android - 将jar文件与phonegap/cordova插件一起使用

android - 构建 apk 时如何减小 xml Assets 的大小