【问题标题】:Gradle dependency for subprojects does not work子项目的 Gradle 依赖项不起作用
【发布时间】:2021-12-31 15:22:23
【问题描述】:

我用 kotlin 和 gradle 创建了一个带有 spring initializr 的项目来研究微服务中的六边形架构。我正在使用带有模块的 IntelliJ 来划分代码,但 spring-jpa 依赖项在模块(或子项目)中不起作用。

开始 build.gradle.kts 是:

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
    
    plugins {
        id("org.springframework.boot") version "2.5.9-SNAPSHOT"
        id("io.spring.dependency-management") version "1.0.11.RELEASE"
        kotlin("jvm") version "1.5.32"
        kotlin("plugin.spring") version "1.5.32"
        kotlin("plugin.jpa") version "1.5.32"
    }
    
    group = "com.donadon.studyhexagonal"
    version = "0.0.1-SNAPSHOT"
    java.sourceCompatibility = JavaVersion.VERSION_11
    
    repositories {
        mavenCentral()
        maven { url = uri("https://repo.spring.io/milestone") }
        maven { url = uri("https://repo.spring.io/snapshot") }
    }
    
    dependencies {
        implementation("org.springframework.boot:spring-boot-starter-data-jpa")
        implementation("org.springframework.boot:spring-boot-starter-data-redis-reactive")
        implementation("org.springframework.boot:spring-boot-starter-oauth2-client")
        implementation("org.springframework.boot:spring-boot-starter-security")
        implementation("org.springframework.boot:spring-boot-starter-web")
        implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
        implementation("io.projectreactor.kotlin:reactor-kotlin-extensions")
        implementation("org.jetbrains.kotlin:kotlin-reflect")
        implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
        implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor")
        runtimeOnly("org.postgresql:postgresql")
        testImplementation("org.springframework.boot:spring-boot-starter-test")
        testImplementation("io.projectreactor:reactor-test")
        testImplementation("org.springframework.security:spring-security-test")
    }
    
    tasks.withType<KotlinCompile> {
        kotlinOptions {
            freeCompilerArgs = listOf("-Xjsr305=strict")
            jvmTarget = "11"
        }
    }
    
    tasks.withType<Test> {
        useJUnitPlatform()
    }

我将 repositoriesdependencies 移动到 subprojects 方法 e 我将一些插件放在一起,但发生了以下错误:

subprojects {
    apply {
        plugin("kotlin")
        plugin("kotlin-jpa")
        plugin("io.spring.dependency-management")
    }

    repositoreis { ... }
    dependencies {
        implementation("org.springframework.boot:spring-boot-starter-data-jpa")
        ...
    }
}

错误:

Configuration with name 'implementation' not found.
    at Program.execute(Unknown Source)
Caused by: org.gradle.api.artifacts.UnknownConfigurationException: Configuration with name 'implementation' not found.
    at Build_gradle$2$2.invoke(build.gradle.kts:29)

我如何使依赖项适用于所有子项目? 我在这里阅读了 gradle doc 和其他问题,但没有任何帮助。 谢谢你。

编辑

我创建了一个名为 database 的模块,当我尝试使用 @Entity 时,找不到它,但如果我在 main src 的某个类中使用它,它会找到注释。

【问题讨论】:

    标签: spring spring-boot kotlin gradle dependencies


    【解决方案1】:

    几件事:

    1. 某些 gradle 配置无法使用subprojects 方法共享。这包括依赖项。查看this StackOverflow 问题以获取有关如何共享依赖版本的信息。
    2. 根据 gradle,不鼓励通过 subprojects 重用逻辑:Another, discouraged, way to share build logic between subproject is cross project configuration via the subprojects {} and allprojects {} DSL constructs. (Link)

    【讨论】:

    • 感谢Gabriel的回答,这就是为什么当我们使用“gradle init”创建带有子模块的项目时,它会为每个子项目创建一个build.gradle而不创建根build.gradle?
    • @Donadon 不,根构建文件在许多情况下仍然有用:子项目之间的插件版本同步,在 ext 中定义依赖版本,定义公共工件属性等。此外,一些 IDE,如 IntelliJ需要一个根 gradle 构建文件来导入子项目。
    • 如果它回答了你的问题,也请接受这个答案。
    • 我知道并标记了答案!我发现一些示例没有父构建文件的链接,并且自己的 gradle init 也不会生成此文件。您的回答帮助我找到了继续的方法,我仍然没有测试,但我得到了这个概念。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-16
    • 2013-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-15
    相关资源
    最近更新 更多