【发布时间】:2018-05-15 21:08:53
【问题描述】:
我正在开发一个包含多个模块的 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')
}
services 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
【问题讨论】:
-
所以核心依赖于数据、服务和api,对吧?您是否也有其中之一取决于核心?你的实际错误是什么,你的依赖树是什么?通常你会有类似 services->data->apis->core 作为依赖链。
-
根据您的描述,服务找不到数据类。所以重要的是你没有发布的服务模块的配置。贴出相关代码,以及准确完整的信息。
-
@JBNizet 我已经添加了错误消息和依赖项。就编译错误而言,这很简单。服务模块代码中使用的数据模块中的每个符号对于编译器来说都是未知的。
-
我猜这是因为您将引导插件应用于所有项目。它们不是启动项目。它们只是普通的 Java 项目。只有顶层(我猜是奇怪的核心,它依赖于其他所有东西,因此是外壳而不是核心)应该是一个引导项目。
-
@JBNizet 我已经尝试过这样做,但是当引导插件不存在时,Gradle 无法识别任何
org.springframework.boot:spring-boot-starter-依赖项。
标签: java spring spring-boot gradle build.gradle