我尝试使用用于不同层(Web,业务,dataAccess,common)的子项目来创建带有gradle的spring boot项目。
我遵循了官方gradle指南Creating Multi-Projects builds的指示。
两个子项目最初都是使用Spring.io创建的。
问题
我尝试在其他子项目 dataAccess 中使用子项目 common 的User
类。导入在Intellij中工作正常。但是,当我运行gradlew :dataAccess:compileJava
时,任务失败,因为找不到User
:
任务:dataAccess:compileJava失败 ... \ dataAccess \ src \ main \ java \ ch \ wprogLK \ MyProject \ backend \ dataAccess \ MyProjectDataApplication.java:23:错误:找不到符号 用户u = new User(); ^ 符号:类User 位置:类MyProjectDataApplication)
common 项目在 dataAccess 项目之前进行编译。我猜这是一个gradle配置问题,即 dataAccess 项目在编译时对 common 项目一无所知(也许是类路径问题?)。我很高兴有任何帮助。谢谢。
项目设置
代码
MyProject /build.gradle
plugins {
id 'org.springframework.boot' version '2.1.8.RELEASE'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id 'java'
}
group = 'ch.wprogLK'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
allprojects {
apply plugin: 'java'
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
}
MyProject /settings.gradle
rootProject.name = 'MyProject'
include 'common'
include 'dataAccess'
MyProject / 常见 /build.gradle
plugins {
id 'org.springframework.boot'
id 'io.spring.dependency-management'
id 'java'
}
group = 'ch.wprogLK'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
}
MyProject / 常见 /settings.gradle
rootProject.name = 'common'
MyProject / 常见 /src/main/java/ch/wprogLK/myProject/backend/common/entities/User.java
package ch.wprogLK.myProject.backend.common.entities;
public class User
{
private Long userId;
private String username;
}
MyProject / 数据访问 /build.gradle
plugins {
id 'org.springframework.boot'
id 'io.spring.dependency-management'
id 'java'
}
group = 'ch.wprogLK'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
compile project(":common")
}
MyProject / 数据访问 /settings.gradle
rootProject.name = 'dataAccess'
MyProject / dataAccess /src/main/java/ch/wprogLK/myProject/backend/dataAccess/MyProjectDataApplication.java
package ch.wprogLK.myProject.backend.dataAccess;
import ch.wprogLK.myProject.backend.common.entities.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MyProjectDataApplication
{
public static void main(String[] args)
{
SpringApplication.run(MyProjectDataApplication.class, args);
}
public MyProjectDataApplication()
{
User u = new User(); //This line is the problem when running gradle :dataAccess:javaCompile
}
}
答案 0 :(得分:2)
缺少两个设置来表明common
是一个库。因此,从项目的根目录添加以下配置以及compile
和run
。
common build.gradle
bootJar {
enabled = false
}
jar {
enabled = true
}