gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 06:27:01 25 4
gpt4 key购买 nike

我正在尝试使用 gradle 为多模块 springBoot 应用程序设置一个新的存储库(用于构建脚本的 Kotlin DSL)
作为其中的一部分,我试图声明所有子项目所需的通用配置和依赖项。在这样做时,我试图定义 sourceCompatility对于 subprojects 中的所有子项目父 block build.gradle.kts文件
当我尝试使用上述配置编译我的项目时,构建失败并出现以下异常

* What went wrong:
Extension with name 'java' does not exist. Currently registered extension names: [ext]
但如果我移动 java.sourceCompatibility = org.gradle.api.JavaVersion.VERSION_11 行到子模块的 build.gradle.kts文件然后它编译成功并且应用程序按预期出现。
我无法理解我在这里缺少什么。请帮助我理解这一点。
家长 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()
}

关于java - Gradle Kotlin DSL - 当在 Parent 中定义 java.sourceCompatibility 时构建脚本失败,但在添加到子模块时构建成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62498917/

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