Trying to modularize Gradle SpringBoot Project but getting "Error: cannot find symbol" when trying to build?

Trying to modularize Gradle SpringBoot Project but getting "Error: cannot find symbol" when trying to build?

我有一个正在运行的单模块 Java 项目,我正试图将其分成两个模块:一个库和一个应用程序模块。我把顶层build.gradle的Java库拉出来塞进了库build.gradle,并且在应用构建文件中保留了SpringBoot/Docker/MySQL相关的依赖。 运行 ./gradlew build 由于 "cannot find symbol" 应用程序中的 lombok 内容错误而导致错误。

我添加了一个顶层settings.gradle(下面的代码),取出所有Java库并放入library/build.gradle(下面的代码),并在 application/build.gradle 中添加了对库的引用(代码如下)。

settings.gradle:

rootProject.name = 'order-routing'
include 'library'
include 'application'

library/build.gradle:

buildscript {
    repositories { mavenCentral() }
}

plugins {
    id "io.spring.dependency-management" version "1.0.5.RELEASE"
}

apply plugin: 'project-report'
apply plugin: 'java'

ext { springBootVersion = '2.1.6.RELEASE' }

jar {
    baseName = 'order-routing-library'
    version = '0.0.1-SNAPSHOT'
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    compileOnly 'org.projectlombok:lombok'
    testCompileOnly 'org.projectlombok:lombok'
    compileOnly 'com.google.code.gson:gson:2.8.5'
    runtimeOnly 'com.h2database:h2:1.4.197'
    compile 'mysql:mysql-connector-java'
    annotationProcessor 'org.projectlombok:lombok'
    testAnnotationProcessor 'org.projectlombok:lombok'
    compile('org.springframework.boot:spring-boot-starter')
    testCompile('org.springframework.boot:spring-boot-starter-test')
    compile 'org.springframework.boot:spring-boot-starter-web'
    compile 'com.google.cloud.sql:mysql-socket-factory-connector-j-8:1.0.14'
}

和application/build.gradle(注意我加了"compile project(':library')"):

buildscript {
    ext { springBootVersion = '2.1.6.RELEASE' }
    repositories { mavenCentral() }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath('se.transmode.gradle:gradle-docker:1.2')
    }
}

plugins {
    id "io.spring.dependency-management" version "1.0.5.RELEASE"
    id 'org.springframework.boot' version '2.1.6.RELEASE'
    id 'java'
    id "org.flywaydb.flyway" version "5.2.4"
}

apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'project-report'
apply plugin: "maven"
apply plugin: 'docker'
repositories {
    mavenCentral()
}

dependencies {
    compile 'mysql:mysql-connector-java'
    compile('org.springframework.boot:spring-boot-starter-actuator')
    compile('org.springframework.boot:spring-boot-starter-web')
    testCompile('org.springframework.boot:spring-boot-starter-test')
    compile project(':library')
}

sourceCompatibility = 8
targetCompatibility = 8

compileJava.options.compilerArgs.add '-parameters'
compileTestJava.options.compilerArgs.add '-parameters'

configurations {
    all*.exclude group: "org.hibernate", module: "hibernate-entitymanager"
    all*.exclude group: "org.apache.tomcat", module: "tomcat-jdbc"
}

flyway {
    url = 'jdbc:mysql://localhost/ordb'
    user = 'flyway'
    table = 'schema_version'
}

bootJar {
    baseName = 'order-routing-application'
    version = '0.0.1-SNAPSHOT'
    mainClassName = 'com.pokemonmerch.orderrouting.OrderRoutingApplication'
}

预期结果:运行 './gradlew build' 并看到构建成功。 实际:在一堆 lombok 生成的方法上获取 "error: cannot find symbol"

据我所知,您正试图在 application 模块中使用 lombok,但您将其作为 compileOnly 依赖项添加到 模块。因此,因为它是 compileOnly,所以它不会作为传递依赖项传递给 application 模块。

请直接将lombok依赖添加到application模块或将compileOnly更改为compile(我不建议这样做)。

P.S。不要忘记在 IDE.

中打开注释处理

实际上,我的建议是在根文件夹 (order-routing/build.gradle) 中创建 build.gradle 并将通用依赖项移动到 subprojects 部分下(https://docs.gradle.org/current/userguide/multi_project_builds.html#sec:subproject_configuration)