应用Spring Boot插件时找不到包 [英] Package not found when Spring Boot plugin applied

查看:158
本文介绍了应用Spring Boot插件时找不到包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a multi-module project with the following build.gradel files

核心:

repositories {
    mavenCentral()
}

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

测试模块:

dependencies {
    implementation project(":core")
}

app:

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中使用)来提供额外的功能.该应用程序模块具有一个主类,该主类调用 SpringApplication.run(Core.class,args); .我认为 app 似乎没有用,但最后,该模块将被单独的项目替换,这些项目将使用该项目中的模块.

The core module is supposed to represent the spring boot application, the test module provides extra functionality by creating a Service that is used in Core via dependency injection. The app module has a main class that calls SpringApplication.run(Core.class, args);. I that app seems useless but at the end, this module will be replaced by separate projects that will use modules from this project.

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

The problem is that this way the compilation of test fails with an error saying that the package from core can not be found. After some googling I've found that applying the spring boot plugin to all modules causes this issue because it disables the jar task. However without that plugin, the build fails with an error that says that the spring boot starter web dependency can not be found but I need that in all my modules so I can use stuff like the @Service annotation.

我该如何解决?

推荐答案

在Gradle中,如果您没有为使用的依赖项指定版本号,则它们需要由其他方式提供.您的情况是 io.spring.dependency-management 插件.但是,只有知道您是否还应用了 org.springframework.boot 插件,它才能了解Spring Boot.因此,一旦删除该依赖项,插件管理插件将无法再提供依赖项的版本.

In Gradle, if you don't specify a version number to the dependencies you use, they need to be supplied by something else. In your case, that something else is the io.spring.dependency-management plugin. However, it only knows about Spring Boot if you also have the org.springframework.boot plugin applied. So once you remove that, the dependency management plugin cannot supply versions for the dependencies anymore.

有很多方法可以解决此问题.这是我能想到的主要内容.全部都在Groovy DSL中.我将它们作为一般情况列出,因此您将不得不对其进行一些调整以适合您的项目.更具体地说,您应该从根目录删除所有插件(或将它们设置为 apply false ),然后将其添加到相关的子项目中.您也可能不需要 war idea 插件.

There are many ways to get around this. Here are the main ones I can think of. All are in Groovy DSL. I list each as a general case, so you will have to adapt it a bit for your project. More specifically, you should remove all plugins from the root (or set them to apply false) and add them to the relevant sub-projects instead. You also likely won't need the war and idea plugins.

(我个人更喜欢选项B.)

(I personally prefer option B by the way.)

在列表中有Spring Boot插件,但不要应用它.这使插件类在项目类路径上可用,因此您可以对其进行引用.然后,您可以将Spring Boot Bom提供给依赖项管理插件:

Have the Spring Boot plugin in the list, but don't apply it. This makes the plugin classes available on the project classpath so you can reference it. Then you can give the Spring Boot bom to the dependency management plugin:

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'
}

查看更多此插件的优势之一是是否要从Maven迁移,因为它反映了依赖项管理规则.还有其他一些功能.但是,如果您没有使用这些功能并且没有从Maven迁移,则不需要它.只需使用正常的Gradle语义即可:

One of the strength of this plugin is if you are migrating from Maven as it mirrors the dependency management rules. There are also a few other features. However, if you are not using those features and are not migrating from Maven, you don't need it. Just use normal Gradle semantics:

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 任务,但是您可以重新启用它.因为jar文件的名称在 jar bootJar 中都相同,所以不能同时使用它们.因此,也可以禁用 bootJar 或为其指定分类器.在这里,我已将其禁用.

C. Use both plugins, but disable the boot jar task.

The Spring Boot plugin disables the normal jar task, but you can just re-enable it. Because the jar file name is the same in both jar and bootJar, you can't have them both at the same time. So either also disable bootJar or give it a classifier. Here I have disabled it.

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
}

此处.

很抱歉,我的回答很长.我被带走了:)

I apologize for the long answer. I got carried away :)

这篇关于应用Spring Boot插件时找不到包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆