gpt4 book ai didi

java - 如何从父项目定义 Gradle kotlin dsl 中的公共(public)依赖项?

转载 作者:行者123 更新时间:2023-12-02 12:41:11 25 4
gpt4 key购买 nike

我正在使用 Kotlin DSL 设置多模块 Gradle 构建。下面是我正在使用的顶级 build.gradle.kts 文件。

subprojects {
repositories {
mavenCentral()
}

group = "com.example"
version = "1.0.0"

plugins.withType<JavaPlugin> {
extensions.configure<JavaPluginExtension> {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11

dependencies {
implementation(platform("org.springframework.boot:spring-boot-dependencies:2.2.2.RELEASE"))
}

tasks.withType<Test> {
useJUnitPlatform()
}
}
}
}

我得到以下错误

* What went wrong:
Script compilation error:

Line 15: implementation(platform("org.springframework.boot:spring-boot-dependencies:2.2.2.RELEASE"))
^ Unresolved reference: implementation

1 error

问题

  • 如何从根 build.gradle.kts 设置公共(public)依赖项?

最佳答案

我建议您阅读 Gradle Kotlin DSL Primer . “了解类型安全模型访问器何时可用”部分解释了为什么您不应期望在此处定义实现(强调我的):

The set of type-safe model accessors available is calculated right before evaluating the script body, immediately after the plugins {} block. Any model elements contributed after that point do not work with type-safe model accessors. For example, this includes any configurations you might define in your own build script.

因此,您不能在顶级 build.gradle 中为 implementation 使用类型安全访问器,除非您要应用 java 插件在其 plugins{} block 中(不建议这样做,除非根项目本身包含源)。

相反,使用不安全的访问器,如 Understanding what to do when type-safe model accessors are not available 部分所示,像这样:

plugins.withType<JavaPlugin> {
extensions.configure<JavaPluginExtension> {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11

dependencies {
"implementation"(platform("org.springframework.boot:spring-boot-dependencies:2.2.2.RELEASE"))
}

tasks.withType<Test> {
useJUnitPlatform()
}
}
}

关于java - 如何从父项目定义 Gradle kotlin dsl 中的公共(public)依赖项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59545185/

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