【问题标题】:Package not found when Spring Boot plugin applied应用 Spring Boot 插件时找不到包
【发布时间】:2021-03-11 22:34:44
【问题描述】:

我有一个包含以下 build.gradel 文件的多模块项目

核心:

repositories {
    mavenCentral()
}

dependencies {
    providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
}

测试模块:

dependencies {
    implementation project(":core")
}

应用程序:

repositories {
    mavenCentral()
}

dependencies {
    implementation project(":core")
    implementation project(":test")
}

springBoot{
    mainClassName = "com.yenovi.dev.Main"
}

然后是根:

buildscript {
    repositories {
        mavenCentral()
    }
}

plugins {
    id "io.spring.dependency-management" version "1.0.4.RELEASE"
    id 'org.springframework.boot' version '2.3.3.RELEASE'
    id 'war'
}

repositories {
    mavenCentral()
}

subprojects {
    apply plugin: 'java'
    apply plugin: 'war'
    apply plugin: 'idea'
    apply plugin: "io.spring.dependency-management"
    apply plugin: 'org.springframework.boot'

    sourceCompatibility = 11

    repositories {
        mavenCentral()
    }

    dependencies {
        compile 'org.springframework.boot:spring-boot-starter-web'
        testImplementation('org.springframework.boot:spring-boot-starter-test') {
            exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
        }
    }
}

核心模块应该代表spring boot应用程序,测试模块通过创建一个通过依赖注入在Core中使用的服务来提供额外的功能。 app 模块有一个调用SpringApplication.run(Core.class, args); 的主类。我认为app 似乎没用,但最后,这个模块将被单独的项目替换,这些项目将使用来自这个项目的模块。

问题是这样test 的编译会失败,并出现一个错误,指出无法找到来自core 的包。经过一番谷歌搜索后,我发现将 spring boot 插件应用于所有模块会导致此问题,因为它禁用了 jar 任务。但是,如果没有该插件,构建将失败并出现错误,指出无法找到 spring boot starter web 依赖项,但我需要在所有模块中使用它,以便可以使用 @Service 注释之类的东西。

我该如何解决这个问题?

【问题讨论】:

    标签: java spring-boot gradle module


    【解决方案1】:

    在 Gradle 中,如果您没有为使用的依赖项指定版本号,则它们需要由其他东西提供。在您的情况下,其他东西是 io.spring.dependency-management 插件。但是,只有在您还应用了 org.springframework.boot 插件时,它才知道 Spring Boot。所以一旦你删除它,依赖管理插件就不能再提供依赖的版本了。

    有很多方法可以解决这个问题。以下是我能想到的主要内容。都在 Groovy DSL 中。我将每种情况都列为一般情况,因此您必须针对您的项目进行一些调整。更具体地说,您应该从根目录中删除所有插件(或将它们设置为apply false)并将它们添加到相关的子项目中。您也可能不需要waridea 插件。

    (顺便说一下,我个人更喜欢选项B。)

    A.单独使用 Spring 依赖管理插件

    列表中有 Spring Boot 插件,但不要应用它。这使得插件类在项目类路径中可用,因此您可以引用它。然后就可以把Spring Boot bom交给依赖管理插件了:

    plugins {
        id 'org.springframework.boot' version '2.4.0' apply false // <- Apply false
        id 'io.spring.dependency-management' version '1.0.10.RELEASE'
        id 'java'
    }
    
    dependencyManagement {
        imports {
            mavenBom org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES
        }
    }
    
    
    dependencies {
        implementation 'org.springframework.boot:spring-boot-starter'
    }
    

    查看更多here

    B.不要使用 Spring 依赖管理插件

    如果您从 Maven 迁移,此插件的优势之一是它反映了依赖管理规则。还有一些其他功能。但是,如果您没有使用这些功能并且没有从 Maven 迁移,则不需要它。只需使用普通的 Gradle 语义:

    plugins {
        id 'org.springframework.boot' version '2.4.0' apply false // <- Apply false
        id 'java'
    }
    
    dependencies {
        implementation platform(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES)
        implementation 'org.springframework.boot:spring-boot-starter'
    }
    
    // OR:
    
    plugins {
        id 'java'
    }
    
    dependencies {
        implementation platform("org.springframework.boot:spring-boot-dependencies:2.4.0")
        implementation 'org.springframework.boot:spring-boot-starter'
    }
    

    C.使用这两个插件,但禁用启动 jar 任务。

    Spring Boot 插件禁用了正常的jar 任务,但您可以重新启用它。因为jarbootJar 中的jar 文件名相同,所以不能同时拥有它们。所以要么禁用bootJar,要么给它一个分类器。这里我禁用了它。

    plugins {
        id 'org.springframework.boot' version '2.4.0' // <- Apply true
        id 'io.spring.dependency-management' version '1.0.10.RELEASE'
        id 'java'
    }
    
    dependencies {
        implementation 'org.springframework.boot:spring-boot-starter'
    }
    
    jar {
        enabled = true
    }
    
    bootJar {
        enabled = false
    }
    

    查看更多here

    对于冗长的回答,我深表歉意。我被带走了:)

    【讨论】:

    • 您好,感谢您的回答。你能提供测试的解决方案吗?必须启用和禁用什么?对我来说,只有 C 选项有效...谢谢您的建议
    • 嗯,我想不出应该专门为测试做些什么。
    猜你喜欢
    • 1970-01-01
    • 2014-12-22
    • 2019-01-17
    • 2018-01-29
    • 2019-06-03
    • 1970-01-01
    • 2020-12-30
    • 1970-01-01
    • 2018-07-06
    相关资源
    最近更新 更多