Search code examples
spring-bootgradledependency-managementmodularity

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


I had a working single-module Java project I am trying to break up into two modules: a library, and an application module. I pulled out the Java libraries from the top level build.gradle and stuffed them in the library build.gradle, and kept the SpringBoot/Docker/MySQL related dependencies in the application build file. Running ./gradlew build causes errors due to "cannot find symbol" errors on the lombok stuff in the App.

I added a top level settings.gradle (code below), pulled out all the Java libraries and put into the library/build.gradle (code below), and added a reference to library in the application/build.gradle (code below).

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'
}

and application/build.gradle (notice I added "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'
}

Expected result: run './gradlew build' and see a successful build. Actual: Getting "error: cannot find symbol" on a bunch of lombok generated methods


Solution

  • As I understood you're trying to use lombok in the application module, but you added it as a compileOnly dependency to the library module. So, because it's compileOnly it's not passed as a transitive dependency to the application module.

    Please add lombok dependecies to the application module directly or change compileOnly to compile(what I don't recommend to do).

    P.S. Don't forget to turn on Annotation Processing in your IDE.


    Actually, what I suggest - it's to create build.gradle in the root folder (order-routing/build.gradle) and move common dependencies there, under the subprojects sections (https://docs.gradle.org/current/userguide/multi_project_builds.html#sec:subproject_configuration)