programing

Gradle을 사용한 Spring Boot Multi Module 프로젝트는 구축되지 않음

lastcode 2023. 4. 2. 10:35
반응형

Gradle을 사용한 Spring Boot Multi Module 프로젝트는 구축되지 않음

저는 여러 모듈을 갖춘 Spring Boot 앱을 만들고 있는데 Gradle을 사용하여 만들고 있습니다.유감스럽게도 Gradle 설정을 올바르게 할 수 없습니다.

프로젝트 구조는 다음과 같습니다.

parent
  |
  + build.gradle
  |
  + settings.gradle
  |
  + core
  |   |
  |   + build.gradle
  | 
  + apis
  |   |
  |   + build.gradle
  |
  + services
  |   |
  |   + build.gradle
  | 
  + data
  |   |
  |   + build.gradle

프로젝트를 구축하려고 하면 다음과 같은 컴파일 오류가 발생합니다.error: cannot find symbol서비스에 사용되는 데이터의 클래스는 Import되지 않습니다.모든 모듈에 해당한다고 생각합니다.

부모 build.gradle은 다음과 같습니다.

buildscript {
    ext {
        springBootVersion = '2.0.0.BUILD-SNAPSHOT'
    }
    repositories {
        mavenCentral()
        maven { url "https://repo.spring.io/snapshot" }
        maven { url "https://repo.spring.io/milestone" }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'eclipse'
apply plugin: 'idea'

allprojects {
    apply plugin: 'java'
    apply plugin: 'org.springframework.boot'
    apply plugin: 'io.spring.dependency-management'

    group = 'com.example'
    version = '0.0.1-SNAPSHOT'
    sourceCompatibility = 1.8
    targetCompatibility = 1.8

    repositories {
        mavenCentral()
        maven { url "https://repo.spring.io/snapshot" }
        maven { url "https://repo.spring.io/milestone" }
    }

    dependencies {
        testCompile('org.springframework.boot:spring-boot-starter-test')
    }
}

dependencies {
    compile project(':data')
    compile project(':services')
    compile project(':apis')
    compile project(':core')
}

jar {
    baseName = 'my-jar'
    version = '0.0.1-SNAPSHOT'
}

settings.gradle:

rootProject.name = 'my-app'
include ':apis'
include ':core'
include ':data'
include ':services'

한 아이(핵심)의 빌드.gradle:

dependencies {
    compile('org.springframework.boot:spring-boot-starter-actuator')
    compile('org.springframework.boot:spring-boot-starter-quartz')
    compile('org.springframework.boot:spring-boot-starter-tomcat')
    runtime('com.h2database:h2')

    compile project(':data')
    compile project(':services')
    compile project(':apis')
}

services build.gradle은 다음과 같습니다.

dependencies {
    compile('org.springframework.boot:spring-boot-starter-quartz')
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    runtime('com.h2database:h2')

    compile project(':data')
}

다른 것도 의존관계만 선언합니다.

의존관계 구조는 다음과 같습니다.

core - apis, services, data

apis - services, data

services - data

data -

컴파일 오류의 예:

/my-app/services/src/main/java/com/example/my-app/business/IssuedTokenService.java:3: error: package com.examples.data.dao does not exist import com.examples.data.dao.IssuedToken;

/my-app/services/src/main/java/com/example/my-app/business/IssuedTokenService.java:4: error: package com.examples.data.repository does not exist import com.examples.data.repository.IssuedTokenRepository;

/my-app/services/src/main/java/com/example/my-app/business/IssuedTokenService.java:12: error: cannot find symbol 
    private IssuedTokenRepository issuedTokenRepository;

    symbol:   class IssuedTokenRepository
   location: class IssuedTokenService

/my-app/services/src/main/java/com/example/my-app/business/IssuedTokenService.java:15: error: cannot find symbol
   public void saveToken(IssuedToken issuedToken) {
                                  ^
   symbol:   class IssuedToken
   location: class IssuedTokenService

유틸리티(데이터) 프로젝트에 다음과 같이 입력합니다.

bootJar {
    enabled = false
}

jar {
    enabled = true
}

if kotlin DSL

tasks.getByName<BootJar>("bootJar") {
    enabled = false
}

tasks.getByName<Jar>("jar") {
    enabled = true
}

답은 이것과 비슷합니다.

다중 프로젝트 빌드에 대한 그라들 문서 상태:

"lib" 종속성은 실행 종속성의 특수 형식입니다.그러면 다른 프로젝트가 먼저 빌드되고 다른 프로젝트의 클래스가 포함된 항아리가 클래스 경로에 추가됩니다.

따라서 재포장된 항아리는 문제가 됩니다.

예를 들어, 프로젝터가B프로젝트에 따라 다름A, 및 프로젝트A가 있다org.springframework.boot플러그인이 적용되어 있습니다.compile project(':A')프로젝트 jar가 재패키지 되어 있기 때문에 의존관계가 기능하지 않습니다.bootRepackage또는bootJar작업. 결과적인 지방 항아리는 다른 구조를 가지고 있어 Gradle이 클래스를 로드하는 것을 방해합니다.

이 경우 의존관계는 다음과 같이 기술해야 합니다.

dependencies {
  compile project(':A').sourceSets.main.output
}

소스 세트의 출력을 직접 사용하는 것은 프로젝트의 "정상" 결과물을 사용하는 것과 같습니다.A다시 포장하기 전에.

bootJar {
    enabled = false
}

jar {
    enabled = true
}

난 이거면 돼.제 경우 멀티프로젝트와 그래들이 있는 스프링 클라우드와 함께 스프링 부츠를 사용하면 구축에 실패합니다.이 솔루션이라면 효과가 있습니다!

제외해야 합니다.org.springframework.boot모든 서브모듈(루트)에서build.gradle파일,allProjectsfat-jar에 구축되는 코어 모듈의 의존관계로 간주됩니다.

포함하다dependencyManagementspring-boot 관리 서브모듈에 대한 설정을 실시합니다.

dependencyManagement {
    imports {
        mavenBom "org.springframework.boot:spring-boot-starter-parent:${springBootVersion}"
    }
}

문제의 원인은 코어 모듈 fat-jar 내에 컴파일된 jar 파일이 없기 때문입니다.

언급URL : https://stackoverflow.com/questions/47598848/spring-boot-multi-module-project-with-gradle-doesnt-build

반응형