gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 03:42:32 25 4
gpt4 key购买 nike

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

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com