Search code examples
javaspringspring-bootgradlemulti-module

Failed on Converting Monolithic Spring Boot App with Gradle into Multi Module


Actually I have an monolithic spring boot app with gradle and I want to covert it into multi module. It just a simple app to do CRUD book.

My app structure like the following,

simple-app
| src/main/java/example/simpleapp
| -------------------------------/controller`
| -------------------------------/exception
| -------------------------------/model
| -------------------------------/repository
| -------------------------------/service
| -------------------------------SimpleAppAplication
| build.gradle
| settings.gradle

step by step I separate all the package into module, then my folder structure changed to like the following

simple-app
| controller
| | src
| | build.gradle
| exception
| | src
| | build.gradle
| main-app
| | src
| | build.gradle
| model
| | src
| | build.gradle
| repository
| | src
| | build.gradle
| service
| | src
| | build.gradle
| build.gradle
| settings.gradle

if I run the project, It working just fine and return the result that should be return.

But, when I build the project, It turn into error like this.

PC:~/simple-app$ ./gradlew build
> Task :repository:compileJava FAILED
/simple-appp/repository/src/main/java/example/simpleapp/repository/BookRepository.java:3: error: package nanihutagaol.simpleapp.model does not exist
import example.simpleapp.model.Book;
                               ^
simple-app/repository/src/main/java/example/simpleapp/repository/BookRepository.java:9: error: cannot find symbol
public interface BookRepository extends JpaRepository<Book, Long> {
                                                  ^
symbol: class Book
2 errors
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':repository:compileJava'.
> Compilation failed; see the compiler error output for details.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.4.1/userguide/command_line_interface.html#sec:command_line_warnings
BUILD FAILED in 2s
3 actionable tasks: 1 executed, 2 up-to-date

I don't understand why it turn error like that, because the model package was exist. Hope one of you can give an solution.

thanks before...

edited:

on repository/build.gradle

group = 'example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
  mavenCentral()
}
dependencies {
  implementation project(":model")
  implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
  implementation 'org.springframework.boot:spring-boot-starter-web'
}

on model/build.gradle

group = 'example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
  mavenCentral()
}
configurations {
  compileOnly {
    extendsFrom annotationProcessor
  }
}
dependencies {
  implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
  implementation 'org.springframework.boot:spring-boot-starter-web'
  compileOnly 'org.projectlombok:lombok'
  annotationProcessor 'org.projectlombok:lombok'
  runtimeOnly 'org.postgresql:postgresql'
}

Solution

  • I finally can solve the error, after trying to understand other similar topic.

    So my first syntax on root/build.gradle are as follows

    plugins {
        id 'org.springframework.boot' version '2.3.0.RELEASE'
        id 'io.spring.dependency-management' version '1.0.9.RELEASE'
        id 'java'
    }
    group = 'example'
    version = '0.0.1-SNAPSHOT'
    sourceCompatibility = '1.8'
    
    repositories {
        mavenCentral()
    }
    
    configurations {
        compileOnly {
            extendsFrom annotationProcessor
        }
    }
    
    dependencies {
        implementation project(":main-app")
    }
    
    test {
        useJUnitPlatform()
    }
    
    subprojects {
        apply plugin: 'org.springframework.boot'
        apply plugin: 'io.spring.dependency-management'
        apply plugin: 'java'
    }
    

    Then I update it by adding the following code, on the root/build.gradle and subprojects/build.gradle

    bootJar {
      enabled = false
    }
    
    jar {
      enabled = true
    }
    

    Based on guide on https://spring.io/guides/gs/multi-module/, that code used to tell Gradle to not build an executable jar for the Library project.

    I don't know why I miss this step before. So, my last root/build.gradle will look like

    plugins {
        id 'org.springframework.boot' version '2.3.0.RELEASE'
        id 'io.spring.dependency-management' version '1.0.9.RELEASE'
        id 'java'
    }
    
    group = 'example'
    version = '0.0.1-SNAPSHOT'
    sourceCompatibility = '1.8'
    
    bootJar {
        enabled = false
    }
    
    jar {
        enabled = true
    }
    
    repositories {
        mavenCentral()
    }
    
    dependencies {
        implementation project(":main-app")
    }
    
    test {
        useJUnitPlatform()
    }
    
    subprojects {
        apply plugin: 'org.springframework.boot'
        apply plugin: 'io.spring.dependency-management'
        apply plugin: 'java'
    
        group = 'example'
        version = '0.0.1-SNAPSHOT'
        sourceCompatibility = '1.8'
    
        repositories {
            mavenCentral()
        }
        
        bootJar {
            enabled = false
        }
    
        jar {
            enabled = true
        }
    }
    

    Then I update all my subproject/build.gradle, by just adding dependencies tag. For example repository/build.gradle

    dependencies {
        implementation project(":model")
        implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
        implementation 'org.springframework.boot:spring-boot-starter-web'
    }
    

    That's all my problem explanation... :)