简体   繁体   English

将带有 Gradle 的单片 Spring 引导应用程序转换为多模块失败

[英]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.实际上,我有一个带有 gradle 的单片 spring 引导应用程序,我想将其转换为多模块。 It just a simple app to do CRUD book.它只是一个简单的应用程序来做 CRUD 书。

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一步一步我将所有 package 分成模块,然后我的文件夹结构更改为如下所示

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.我不明白为什么它会变成那样的错误,因为 model package 是存在的。 Hope one of you can give an solution.希望你们中的一个可以提供解决方案。

thanks before...之前谢谢...

edited:

on repository/build.gradle在存储库/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在模型/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'
}

I finally can solve the error, after trying to understand other similar topic.在尝试了解其他类似主题后,我终于可以解决错误。

So my first syntax on root/build.gradle are as follows所以我在root/build.gradle上的第一个语法如下

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然后我通过在root/build.gradlesubprojects/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.基于https://spring.io/guides/gs/multi-module/上的指南,该代码用于告诉 Gradle 不要为项目构建可执行的 jar。

I don't know why I miss this step before.我不知道为什么我之前错过了这一步。 So, my last root/build.gradle will look like所以,我最后一个root/build.gradle看起来像

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.然后我通过添加依赖项标签来更新我的所有subproject/build.gradle For example repository/build.gradle例如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... :)这就是我所有的问题解释...... :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM