使用Gradle的Spring Boot多模块项目无法构建

我正在使用具有多个模块的Spring Boot应用程序,我们正在使用Gradle来构建它。 不幸的是我无法正确使用Gradle配置。

项目结构如下:

parent | + build.gradle | + settings.gradle | + core | | | + build.gradle | + apis | | | + build.gradle | + services | | | + build.gradle | + data | | | + build.gradle 

当我尝试构建项目时,我收到错误等编译错误error: cannot find symbol说明,不会导入服务中使用的数据类。 我认为所有模块之间都是如此。

我的父build.gradle看起来像这样:

 buildscript { ext { springBootVersion = '2.0.0.BUILD-SNAPSHOT' } repositories { mavenCentral() maven { url "https://repo.spring.io/snapshot" } maven { url "https://repo.spring.io/milestone" } } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") } } apply plugin: 'eclipse' apply plugin: 'idea' allprojects { apply plugin: 'java' apply plugin: 'org.springframework.boot' apply plugin: 'io.spring.dependency-management' group = 'com.example' version = '0.0.1-SNAPSHOT' sourceCompatibility = 1.8 targetCompatibility = 1.8 repositories { mavenCentral() maven { url "https://repo.spring.io/snapshot" } maven { url "https://repo.spring.io/milestone" } } dependencies { testCompile('org.springframework.boot:spring-boot-starter-test') } } dependencies { compile project(':data') compile project(':services') compile project(':apis') compile project(':core') } jar { baseName = 'my-jar' version = '0.0.1-SNAPSHOT' } 

settings.gradle:

 rootProject.name = 'my-app' include ':apis' include ':core' include ':data' include ':services' 

其中一个孩子(核心)有这个在它的build.gradle:

 dependencies { compile('org.springframework.boot:spring-boot-starter-actuator') compile('org.springframework.boot:spring-boot-starter-quartz') compile('org.springframework.boot:spring-boot-starter-tomcat') runtime('com.h2database:h2') compile project(':data') compile project(':services') compile project(':apis') } 

服务build.gradle看起来像这样:

 dependencies { compile('org.springframework.boot:spring-boot-starter-quartz') compile('org.springframework.boot:spring-boot-starter-data-jpa') runtime('com.h2database:h2') compile project(':data') } 

其他的也只声明依赖。

依赖结构如下所示:

 core - apis, services, data apis - services, data services - data data - 

编译错误的示例:

 /my-app/services/src/main/java/com/example/my-app/business/IssuedTokenService.java:3: error: package com.examples.data.dao does not exist import com.examples.data.dao.IssuedToken; /my-app/services/src/main/java/com/example/my-app/business/IssuedTokenService.java:4: error: package com.examples.data.repository does not exist import com.examples.data.repository.IssuedTokenRepository; /my-app/services/src/main/java/com/example/my-app/business/IssuedTokenService.java:12: error: cannot find symbol private IssuedTokenRepository issuedTokenRepository; symbol: class IssuedTokenRepository location: class IssuedTokenService /my-app/services/src/main/java/com/example/my-app/business/IssuedTokenService.java:15: error: cannot find symbol public void saveToken(IssuedToken issuedToken) { ^ symbol: class IssuedToken location: class IssuedTokenService 

在您的实用程序(数据)项目中放置:

 bootJar { enabled = false } jar { enabled = true } 

答案与此类似。

有关多项目构建的Gradle 文档说明:

“lib”依赖项是执行依赖项的一种特殊forms。 它首先构建另一个项目,并将jar与另一个项目的类添加到类路径中。

因此,重新包装的jar子造成了问题。

例如,如果项目B依赖于项目A ,并且项目A应用了org.springframework.boot插件,则简单的compile project(':A')依赖项将无法工作,因为项目jar在bootRepackage期间被重新打包或bootJar任务。 生成的胖jar具有不同的结构,阻止Gradle加载其类。

在这种情况下,依赖项应按以下方式编写:

 dependencies { compile project(':A').sourceSets.main.output } 

直接使用源集的输出等同于在重新打包之前使用项目A的“正常”生成的jar。

 bootJar { enabled = false } jar { enabled = true } 

这适用于我的。 在我的情况下使用弹簧云与多项目的spring启动和gradle无法建立。 有了这个解决方案!