Spring Boot和Gradle多模块项目无法正确加载依赖项 [英] Spring boot and Gradle multi-modules project, failed to load dependencies correctly

查看:915
本文介绍了Spring Boot和Gradle多模块项目无法正确加载依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我有一个使用Gradle构建的spring boot项目. 该项目有一个包含另一个4个子模块的根项目. 根项目settings.gradle如下所示:

Basically I have a spring boot project build with Gradle. The project has a root project that contains another 4 sub-modules. The root project settings.gradle looks like this:

rootProject.name = 'proj'

include 'proj-app'
include 'proj-integration-tests'
include 'proj-model'
include 'proj-service'

该应用程序模块包含spring-boot-gradle-plugin并公开了一些api.

The app module contains the spring-boot-gradle-plugin and exposes some api's.

我想要做的是创建仅包含集成测试的proj-integration-tests子模块.问题从这里开始,因为我需要proj-app依赖项.

What I wanted to do was to create proj-integration-tests sub-module that only contain the integration tests. The problem start here since I needed the proj-app dependency.

因此在项目集成测试中,我的build.gradle包含:

So in proj-integration-tests I have the build.gradle that contains:

dependencies {
  testCompile('org.springframework.boot:spring-boot-starter-web')
  testCompile('org.springframework.boot:spring-boot-starter-test')
  testCompile project(':proj-app')
  testCompile project(':proj-model')
}

自集成测试以来,我需要proj-app依赖项:

and I needed the proj-app dependency since the integration test:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = ProjApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)

要求Spring启动应用程序启动位于proj-app模块中的(ProjApplication.class).

required the Spring boot application to start(ProjApplication.class) that is located in proj-app module.

我从Gradle中得到的错误是:找不到符号ProjApplication".

The error that I got from Gradle is: "cannot find symbol ProjApplication".

为什么Gradle无法正确管理proj-app依赖项? 在此先感谢;)

Why Gradle could not manage properly the proj-app dependency? Thanks in advance ;)

推荐答案

似乎proj-app依赖项是以spring boot方式构建的.这意味着所获得的工件是可执行的spring boot far jar.这就是为什么proj-integration-tests在编译时无法从proj-app中找到类的原因. 因此,为了维护可执行jar,并使proj-app作为proj-integration-tests模块中的依赖项,我修改了proj app的build.gradle来创建两个jar:以春季启动方式和标准版:

It seems that the proj-app dependency is build in a spring boot fashion way. This means that the artifact obtained is a executable spring boot far jar.This is why tha proj-integration-tests could not found the classes from proj-app at the compile time.
So in order to maintain the executable jar, and to have proj-app as a dependency in proj-integration-tests module, I've modified the build.gradle from proj app to create both jars: in a spring boot fashion way and the standard version:

bootJar {
baseName = 'proj-app-boot'
enabled = true
}

jar {
baseName = 'proj-app'
enabled = true
}

这篇关于Spring Boot和Gradle多模块项目无法正确加载依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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