簡體   English   中英

如何從父項目定義 Gradle kotlin dsl 中的常見依賴項?

[英]How to define common dependencies in Gradle kotlin dsl from the parent project?

我正在使用 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設置公共依賴build.gradle.kts

我建議您閱讀Gradle Kotlin DSL Primer “了解何時可以使用類型安全模型訪問器”一節解釋了為什么您不應期望在此位置定義implementation (重點是我的):

可用的類型安全模型訪問器集是在評估腳本主體之前計算的,緊接在 plugins {} 塊之后。 在此之后貢獻的任何模型元素都不適用於類型安全的模型訪問器。 例如,這包括您可能在自己的構建腳本中定義的任何配置。

因此,您不能在頂級build.gradle使用類型安全訪問器進行implementation ,除非您將在其plugins {}塊中應用java插件(不建議這樣做,除非根項目本身包含源)。

相反,請使用不安全的訪問器,如了解當類型安全模型訪問器不可用時該做什么部分中所演示的,如下所示:

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()
            }
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM