gpt4 book ai didi

gradle - 在Gradle Kotlin DSL中包含复合版本中模仿多项目执行吗?

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

我正在简化设置,以说明问题,但其中包括结构上的复杂性。

使用Gradle的Kotlin DSL,我有一个复合构建,其中根项目为空,两个包含的构建都是并排的多项目构建,它们具有使用“容器”项目(即没有build.gradle的空目录)的不同结构.kts文件)以供组织使用。

.
├── app
│   ├── common
│   │   └── build.gradle.kts
│   ├── js
│   │   └── build.gradle.kts
│   ├── jvm
│   │   └── build.gradle.kts
│   ├── build.gradle.kts
│   └── settings.gradle.kts
├── library
│   ├── core
│   │   ├── common
│   │   │   └── build.gradle.kts
│   │   ├── js
│   │   │   └── build.gradle.kts
│   │   └── jvm
│   │   └── build.gradle.kts
│   ├── other-component
│   │   ├── common
│   │   │   └── build.gradle.kts
│   │   ├── js
│   │   │   └── build.gradle.kts
│   │   └── jvm
│   │   └── build.gradle.kts
│   ├── util
│   │   ├── util1
│   │   │   ├── common
│   │   │   │   └── build.gradle.kts
│   │   │   ├── js
│   │   │   │   └── build.gradle.kts
│   │   │   └── jvm
│   │   │   └── build.gradle.kts
│   │   └── util2
│   │   ├── common
│   │   │   └── build.gradle.kts
│   │   ├── js
│   │   │   └── build.gradle.kts
│   │   └── jvm
│   │   └── build.gradle.kts
│   ├── build.gradle.kts
│   └── settings.gradle.kts
├── build.gradle.kts
└── settings.gradle.kts

我的愿望是能够在IDE(Intellij)的根复合项目中运行构建,并且它模仿多项目执行的行为,其中该项目下的所有内容依次执行任务。

在Groovy中,可以只使用组合项目中includeBuilds * .tasks *上的spread运算符将其连接起来,但是在Kotlin DSL中,我们只能访问task,这是一个TaskReference,无法获取集合。任务(TaskCollection或Task的集合)或TaskReferences的集合。

因此,在复合build.gradle.kts的rootProject中,我有:
tasks {
val clean by getting {
gradle.includedBuilds.forEach { this.dependsOn(it.task(":cleanAll")) }
}

val build by getting {
gradle.includedBuilds.forEach { this.dependsOn(it.task(":buildAll")) }
}
}

然后在包含的builds build.gradle.kts文件之一中,我尝试用两种不同的方式连接它们(很多,但这是两种方法):
// Variation 1
tasks {
val buildAll : GradleBuild by creating {
this.dependsOn(tasks.getByPath(":build"))
}

val cleanAll : Delete by creating {
this.dependsOn(tasks.getByPath(":clean"))
}
}

// Variation 2
tasks {
val buildAll: GradleBuild by creating {
subprojects.forEach {
this.dependsOn(it.tasks.getByPath(":build"))
}
}

val cleanAll: Delete by creating {
subprojects.forEach {
this.dependsOn(it.tasks.getByPath(":clean"))
}
}
}

// Variation 2.b
tasks {
val buildAll: GradleBuild by creating {
this.dependsOn(subprojects.mapNotNull(it.tasks.getByPath(":build")))
}

val cleanAll: Delete by creating {
this.dependsOn(subprojects.mapNotNull(it.tasks.getByPath(":clean")))
}
}

// I even used different ways to try and get the tasks such as it.tasks["root:library:build"], it.tasks[":library:build"], and it.tasks["library:build"] since I know that the included builds are executed in an isolated fashion. None of these worked
// The result was when I used absolute paths, gradle spat back that these tasks didn't exist (I assumed because they were lifecycle tasks).

基本上,尝试上述变体只会构建并清除所包含构建的rootProjects,而不会清除子项目。这是一个错误吗?

我不想诉诸于需要了解所包含构建的基础结构的知识来进行连接。那将是不可持续的。我究竟做错了什么?

最佳答案

好的,不确定发生了什么以及为什么其他方法无效,但是我发现了一种有效的方法,不需要我制造依赖于生命周期任务的综合任务。

组合rootproject build.gradle.kts任务与原始问题中所述的相同:

tasks {
val clean by getting {
gradle.includedBuilds.forEach { this.dependsOn(it.task(":cleanAll")) }
}

val build by getting {
gradle.includedBuilds.forEach { this.dependsOn(it.task(":buildAll")) }
}
}

包含的构建根项目的build.gradle.kts中的任务声明需要通过以下方式收集并依赖任务:
tasks {
val buildAll: GradleBuild by creating {
dependsOn(getTasksByName("build", true))
}

val cleanAll: Delete by creating {
dependsOn(getTasksByName("clean", true))
}
}

这将递归地收集任务。尽管我以前遍历所有子项目的技术也应该可以解决问题,因为子项目包含所有子项目,但由于某种原因,它无法正常工作。这是!希望这可以帮助其他人。

关于gradle - 在Gradle Kotlin DSL中包含复合版本中模仿多项目执行吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51756055/

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