Gradle Kotlin DSL - 当 Parent 中定义 java.sourceCompatibility 时构建脚本失败,但添加到 child 模块时构建成功

Gradle Kotlin DSL - Build script fails when java.sourceCompatibility defined in Parent but build is successful when added to child module

我正在尝试使用 gradle(用于构建脚本的 Kotlin DSL)

为多模块 springBoot 应用程序设置一个新的存储库

作为其中的一部分,我正在尝试声明所有子项目所需的通用配置和依赖项。这样做时,我试图为 parent build.gradle.kts 文件

subprojects 块中的所有 child 项目定义 sourceCompatility

当我尝试使用上述配置编译我的项目时,构建失败并出现以下异常

* What went wrong:
Extension with name 'java' does not exist. Currently registered extension names: [ext]

但是,如果我将行 java.sourceCompatibility = org.gradle.api.JavaVersion.VERSION_11 移动到 child 模块的 build.gradle.kts 文件,那么它编译成功并且应用程序按预期出现。

我不明白我在这里错过了什么。请帮助我理解这一点。

Parent build.gradle.kts

buildscript {
    repositories {
        mavenCentral()
    }
}

plugins {
    id("java")
    id("idea")
    id("war")
    id("io.spring.dependency-management") version "1.0.9.RELEASE"
}

subprojects {
    group = "com.company.example"
    version = "0.0.1"

    java.sourceCompatibility = org.gradle.api.JavaVersion.VERSION_11

    repositories {
        mavenCentral()
        maven { url = uri("http://nexus.pentaho.org/content/groups/omni/") }
    }

    apply() {
        plugin("java")
        plugin("idea")
        plugin("io.spring.dependency-management")
    }

    dependencies {
        implementation("io.jsonwebtoken:jjwt-api:0.10.7")
        implementation("io.jsonwebtoken:jjwt-impl:0.10.7")
        implementation("com.auth0:java-jwt:3.10.3")

        implementation(group= "org.mockito", name= "mockito-core", version= "3.1.0")

        implementation(group= "javax.inject", name= "javax.inject", version= "1")
        implementation(group= "org.springframework", name= "spring-context", version= "5.2.6.RELEASE")
        implementation(group= "org.springframework.security", name= "spring-security-core", version= "4.2.3.RELEASE")

        implementation(group= "com.google.protobuf", name= "protobuf-java", version= "3.12.1")
        implementation("com.google.api.grpc:proto-google-common-protos:1.16.0")
        implementation("com.google.api.grpc:grpc-google-longrunning-v1:0.1.8")
        implementation(group= "org.apache.commons", name= "commons-lang3", version= "3.7")
        implementation(group="com.google.guava", name= "guava", version= "29.0-jre")
        implementation(group= "commons-io", name= "commons-io", version= "2.7")

        testImplementation("org.junit.jupiter:junit-jupiter-api:5.3.1")
        testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.3.1")
    }
}

tasks.getByName<Test>("test") {
    useJUnitPlatform()
}

Child build.gradle.kts

buildscript {
    repositories {
        mavenCentral()
    }
}

plugins {
    id("org.springframework.boot") version "2.3.0.RELEASE"
}

//java.sourceCompatibility = org.gradle.api.JavaVersion.VERSION_11

dependencies {
    
    implementation("org.springframework.boot:spring-boot-starter-actuator")
    implementation("org.springframework.boot:spring-boot-starter-web")

}

val jar by tasks.getting(Jar::class) {
    manifest {
        attributes["Main-Class"] = "com.company.example.module.ExampleApplication"
    }

    from(sourceSets.main.get().output)

    dependsOn(configurations.runtimeClasspath)
    from({
        configurations.runtimeClasspath.get().filter { it.name.endsWith("jar") }.map { zipTree(it) }
    })
}

settings.gradle.kts 项目:

rootProject.name = "project_name"
include(":module_name")

P.S:我也试过使用

configure<JavaPluginConvention>{
    sourceCompatibility = org.gradle.api.JavaVersion.VERSION_11
}

但观察到相同的模式。在这种情况下抛出的错误是

* What went wrong:
Extension of type 'JavaPluginConvention' does not exist. Currently registered extension types: [ExtraPropertiesExtension]
buildscript {
    repositories {
        mavenCentral()
    }
}

plugins {
    id("java")
    id("idea")
    id("war")
    id("io.spring.dependency-management") version "1.0.9.RELEASE"
}

subprojects {
    group = "com.company.example"
    version = "0.0.1"

    // Changed the position of the apply block from below the
    // source captibility to above 
    apply {
        plugin("java")
        plugin("idea")
        plugin("io.spring.dependency-management")
    }

    java.sourceCompatibility = org.gradle.api.JavaVersion.VERSION_11

    repositories {
        mavenCentral()
        maven { url = uri("http://nexus.pentaho.org/content/groups/omni/") }
    }

    dependencies {
        implementation("io.jsonwebtoken:jjwt-api:0.10.7")
        implementation("io.jsonwebtoken:jjwt-impl:0.10.7")
        implementation("com.auth0:java-jwt:3.10.3")

        implementation(group= "org.mockito", name= "mockito-core", version= "3.1.0")

        implementation(group= "javax.inject", name= "javax.inject", version= "1")
        implementation(group= "org.springframework", name= "spring-context", version= "5.2.6.RELEASE")
        implementation(group= "org.springframework.security", name= "spring-security-core", version= "4.2.3.RELEASE")

        implementation(group= "com.google.protobuf", name= "protobuf-java", version= "3.12.1")
        implementation("com.google.api.grpc:proto-google-common-protos:1.16.0")
        implementation("com.google.api.grpc:grpc-google-longrunning-v1:0.1.8")
        implementation(group= "org.apache.commons", name= "commons-lang3", version= "3.7")
        implementation(group="com.google.guava", name= "guava", version= "29.0-jre")
        implementation(group= "commons-io", name= "commons-io", version= "2.7")

        testImplementation("org.junit.jupiter:junit-jupiter-api:5.3.1")
        testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.3.1")
    }
}

tasks.getByName<Test>("test") {
    useJUnitPlatform()
}