【问题标题】:Trying to modularize Gradle SpringBoot Project but getting "Error: cannot find symbol" when trying to build?尝试模块化 Gradle SpringBoot 项目,但在尝试构建时出现“错误:找不到符号”?
【发布时间】:2019-12-27 06:34:27
【问题描述】:

我有一个有效的单模块 Java 项目,我试图将其分解为两个模块:一个库和一个应用程序模块。我从最顶层的build.gradle中抽出Java库,塞进库build.gradle中,并将SpringBoot/Docker/MySQL相关的依赖保存在应用构建文件中。运行 ./gradlew build 会由于应用程序中的 lombok 内容上的“找不到符号”错误而导致错误。

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

settings.gradle:

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

库/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 生成的方法上出现“错误:找不到符号”

【问题讨论】:

标签: spring-boot gradle dependency-management modularity


【解决方案1】:

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

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

附:不要忘记在 IDE 中打开注释处理。


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

【讨论】:

  • 为什么不建议将“compileOnly”改为“compile”?这个练习的重点是能够从应用程序项目中提取所有库。也许我被误导了?
  • 您正在使用 spring-boot 插件,它将在 build(assemble) 任务期间为您创建可执行 jar。如果你使用compile配置,这个依赖会在创建的jar中(但你不需要),compileOnly的依赖不会包含在创建的jar中。
  • Here 是关于 compileOnly 依赖项的一些细节。 Compile-only dependencies are distinctly different than regular compile dependencies. They are not included on the runtime classpath and they are non-transitive, meaning they are not included in dependent projects.
猜你喜欢
  • 2021-12-21
  • 2019-06-17
  • 1970-01-01
  • 2016-08-11
  • 1970-01-01
  • 2016-09-02
  • 2020-06-16
  • 1970-01-01
  • 2022-07-11
相关资源
最近更新 更多