Gradle Kotlin DSL:如何使用类型安全配置子项目api

Gradle Kotlin DSL: how to configure subprojects using typesafe api

Gradle6.1.1

我一直在尝试使用 Kotlin DSL 以 类型安全 的方式转换我的项目的 Gradle 文件,但到目前为止失败了。我的所有项目都是 Java 中的多项目构建。这个想法是 factorize/deduplicate 子模块的通用配置(无论是在根模块的 'subprojects' 块中还是在单独的 'kts' 文件中我都不在乎)。 官方文档指出无法使用 'subprojects' 和 apply(from = 'shared.gradle.kts') (https://docs.gradle.org/current/userguide/kotlin_dsl.html).

实现类型安全

它的工作原理如下,但有点烦人:


plugins {
    idea
    eclipse
}

subprojects {
    apply(plugin = "java")

    dependencies {
       "implementation"("com.google.guava:guava:28.1-jre")
       //...
    }
}

有没有办法以类型安全的方式分解所有子模块的模块配置?如果不是... gradle 计划允许吗?

Gradle 6.1.1 Type-safe model accessors 读取:

Only the main project build scripts and precompiled project script plugins have type-safe model accessors. Initialization scripts, settings scripts, script plugins do not. These limitations will be removed in a future Gradle release.

无论 "future release" 可能意味着什么......而 Cross-configuring projects 读取:

Cross project configuration is a mechanism by which you can configure a project from another project’s build script. A common example is when you configure subprojects in the root project build script. Taking this approach means that you won’t be able to use type-safe accessors for model elements contributed by the plugins. You will instead have to rely on string literals and the standard Gradle APIs.