tri*_*oid 8 gradle gradle-kotlin-dsl
示例项目(使用 kotlin DSL 在 Gradle 8.1.1 中编写):
https://github.com/tribbloid/scaffold-gradle-kts/tree/16bf3acffca7b83a7e64dbb248a9512caba87e71
该项目有 3 个以上子模块:
include(":lightweight-dependency:core")
include(":lightweight-dependency:extra") // depends on :lightweight-dependency:core
include(":core") // depends on lightweight-dependency:extra
...
编译的时候,出现了如下错误:
FAILURE: Build failed with an exception.
* What went wrong:
Circular dependency between the following tasks:
:core:compileJava
+--- :core:compileJava (*)
+--- :core:compileKotlin
|    +--- :core:compileJava (*)
|    +--- :core:compileKotlin (*)
|    \--- :core:compileScala
|         +--- :core:compileJava (*)
|         +--- :core:compileKotlin (*)
|         \--- :core:compileScala (*)
\--- :core:compileScala (*)
(*) - details omitted (listed previously)
这是有问题的,因为:lightweight-dependency:core并且:core应该是不同的项目。如何保证Gradle能够区分它们?
当两个子项目名称相同时,即使路径不同,Gradle 也会出现解决依赖关系的问题。
\n这是因为 Gradle 使用子项目的组和工件 ID 来区分子项目间的依赖关系。
\n有以下三种选择:
\n更新子项目路径以使其不同。这是我的首选。
\n.\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 my-project/\n    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 core/\n    \xe2\x94\x82   \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 build.gradle.kts\n    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 lightweight-dependency/\n    \xe2\x94\x82   \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 lightweight-dependency-core/\n    \xe2\x94\x82       \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 build.gradle.kts\n    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 build.gradle.kts\n    \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 settings.gradle.kts\n// settings.gradle.kts\n\ninclude(\n  ":core",\n  ":lightweight-dependency:lightweight-dependency-core",\n)\n结果更冗长,但更明确,而且我认为它最严格地遵循了最少惊讶的原则。子项目文件目录与工件 ID 匹配是件好事,这有助于理解结构,即使不加载任何 Gradle 配置也是如此。
\n每个子项目使用不同的组。
\n// ./core/build.gradle.kts\n\ngroup = "my.project"\n// ./lightweight-dependency/core/build.gradle.kts\n\ngroup = "my.project.lightweight-dependency"\n现在两个子项目的坐标是不同的。
\n这也是一个方便的解决方案,尽管需要注意确保子项目组不会发生冲突,或者要了解子项目 ID 或组是否已更新的要求。
\nsettings.gradle.kts使用以下命令包含项目时更新项目的路径ProjectDescriptor
// settings.gradle.kts\n\ninclude(":core")\ninclude(":lightweight-dependency:core")\n\nproject(":core").name = "core"\nproject(":lightweight-dependency:core").name = "lightweight-dependency-core"\n我不推荐这种方法。我认为以这种方式更新项目名称很令人困惑,而且非常规。这可能会导致意外的问题,因为尽管它是有效的,但其他 Gradle 插件可能没有意识到这是可能的,并且会期望子项目位置与子项目名称匹配。
\n\n\n\n保留子项目的默认项目名称:可以在设置文件中配置自定义项目名称。然而,对于开发人员来说,跟踪哪个项目属于哪个文件夹是不必要的额外工作。
\n
| 归档时间: | 
 | 
| 查看次数: | 108 次 | 
| 最近记录: |