[Spring][프로젝트] Gradle MultiModule 세팅

모두한·2021년 11월 30일
0

쇼핑몰프로젝트

목록 보기
1/3

Gradle MultiModule 세팅하기

그레이들 멀티모듈을 세팅하며 헷갈리고 어려웠던 부분을 정리합니다.

모듈간 참조 그림

누구든 상품을 판매할 수 있는 마켓컨셉의 웹애플리케이션

빌드 에러 발생

위 주제로 프로젝트를 하기로 결정하였습니다.
한개의 단일 애플리케이션으로 띄우기 보다는 애플리케이션의 사이즈를 고려하여 두 개의 모듈로 나누어 진행을 하기로 하였습니다.

모듈들간의 관계는 위 그림과 같습니다.
Root 모듈에서 모든 의존성을 관리합니다.
그리고 module-admin에서 필요한 객체들이 module-web과 같기에 해당 admin은 web을 참조합니다.

공통된 부분을 따로 또 모듈로 빼기보단 module-web에서 공통모듈을 관리하며 module-admin에서 참조하기로 결정했습니다.

그런데 Root 에서 빌드를 진행해보니

$ gradle build
Starting a Gradle Daemon (subsequent builds will be faster)
> Task :bootJar FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':bootJar'.
> Error while evaluating property 'mainClass' of task ':bootJar'
   > Failed to calculate the value of task ':bootJar' property 'mainClass'.
      > Main class name has not been configured and it could not be resolved

* 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

BUILD FAILED in 5s
2 actionable tasks: 2 executed

위와 같은 에러가 발생하였습니다.

해당 에러는 main메서드가 없는 모듈을 빌드하려고 할 때 생기는 에러였습니다.

현재 애플리케이션을 실행하는 main메서드는 module-web 과 module-admin에만 존재하고

root 모듈에는 실제 main메서드가 존재하지 않습니다.

그래서 Root module의 build.gradl에 아래와 같은 구문을 작성해주니 해결되었습니다.

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

module-admin에서 module-web 참조

module-admin 에서 module-web의 클래스파일들을 참조하여 이용할 수 있어야 했기 때문에

Root module의 build.gradle에 아래와 같은 구문을 작성해주었습니다.

project(':module-admin') {
    dependencies {
        implementation project(':module-web')
       }
}

공통 의존성 관리

Root 모듈의 build.gradle에 아래와 같은 구문을 작성해줍니다.
module-admin과 module-web에서 공통으로 필요한 의존성입니다.

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

    dependencies {
        developmentOnly 'org.springframework.boot:spring-boot-devtools'
        implementation 'org.springframework.boot:spring-boot-starter-web'
        implementation 'org.springframework.boot:spring-boot-starter-security'
        implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
        implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'

        implementation 'com.h2database:h2:1.4.200'

        compileOnly 'org.projectlombok:lombok'
        annotationProcessor 'org.projectlombok:lombok'

        testImplementation 'org.springframework.boot:spring-boot-starter-test'
        testImplementation 'org.springframework.restdocs:spring-restdocs-mockmvc'
        testImplementation 'org.springframework.security:spring-security-test'

        implementation 'io.jsonwebtoken:jjwt:0.9.1'
    }
}

To do..

운영 배포시에 DB를 각 module-admin과 module-web이 공유하는 건 문제가 없어보이나,

개발환경에서 module-web과 module-admin이 DB를 공유해야 할 때

H2 in memory 방식의 DB는 깨나 번거로울 것 같습니다

파일 DB로 개발환경을 만들어야 할지 고민해봐야 할 것 같습니다.

profile
개발자 모두한

0개의 댓글