尝试模块化Gradle SpringBoot项目,但在尝试构建时出现“错误:找不到符号”?

时间:2019-08-22 00:59:47

标签: spring-boot gradle dependency-management modularity

我有一个正在工作的单模块Java项目,我试图将其分解为两个模块:一个库和一个应用程序模块。我从顶层build.gradle中提取了Java库,并将它们填充到了build.gradle库中,并将SpringBoot / Docker / MySQL相关的依赖项保留在应用程序构建文件中。运行./gradlew构建会由于应用程序中的龙目岛内容上出现“找不到符号”错误而导致错误。

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

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

1 个答案:

答案 0 :(得分:1)

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

请直接将龙目岛依赖项添加到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)下将常见的依赖项移至该目录。 / p>