멀티 모듈 구성하기
Last update: a year ago by nowwaterReading time: 2 min
초기 멀티 모듈 구조 설계
회원, 게시판, 댓글 기능을 제공하는 커뮤니티 사이트를 구축하려고 한다.
프로젝트 구조
- Root Project :
gallery
- Sub project(module) :
batch
,admin
core
pc-web
📌 core
가 다른 프로젝트에서 사용할 수 있는 중요한 클래스 및 공통 클래스를 모아놓은 프로젝트다.
- 루트 프로젝트의 src 디렉터리 내용을 모두
batch
모듈 하위로 옮기고gallery
의 src는 삭제한다.
2) gallery
의 build
디렉터리는 필요없으므로 삭제한다.
4) build.gradle
에 모듈별로 설정을 추가한다.
루트 프로젝트의
build.gradle
에 모든 모듈의 라이브러리 설정 내용을 작성해도 되고, 각각의 모듈별build.gradle
에 관련 내용을 작성해도 된다.
여기선 루트 프로젝트에서 내용을 작성하겠다.
buildscript {ext {springBootVersion = '2.4.2'}repositories {mavenCentral()}dependencies {classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")}}allprojects {repositories {mavenCentral()}}subprojects {apply plugin: 'java'apply plugin: 'org.springframework.boot'apply plugin: 'io.spring.dependency-management'group = 'com.gongsung.gallery'version = '0.0.1-SNAPSHOT'sourceCompatibility = '11'dependencies {implementation 'org.springframework.boot:spring-boot-starter-web'implementation 'org.springframework.boot:spring-boot-starter-aop'implementation 'org.springframework.boot:spring-boot-starter'// use lombokcompileOnly 'org.projectlombok:lombok'annotationProcessor 'org.projectlombok:lombok'// commons-ioimplementation group: 'commons-io', name: 'commons-io', version: '2.6'// jackson-annotationsimplementation 'com.fasterxml.jackson.core:jackson-annotations'// testtestCompile group: 'junit', name: 'junit', version: '4.12'testImplementation 'org.springframework.boot:spring-boot-starter-test'}test {useJUnitPlatform()useJUnit()}}def executableProjects = [project(':admin'), project(':batch'), project(':pc-web')]def jarProjects = [project(':core')]configure(executableProjects) {dependencies {// executable projects에서 필요한 것들}}configure(jarProjects) {dependencies {// jar projects에서 필요한 것들}}project(':pc-web') {dependencies {compile project(':core')}}project(':admin') {dependencies {compile project(':core')}}project(':batch') {dependencies {compile project(':core')}}
5) batch
, admin
, pc-web
, 수정
모듈 별로 구동하기 위해
module/src/java/
에SpringBoot Application
을 작성해준다.@SpringBootApplicationpublic class batch {public static void main(String[] args) {SpringApplication.run(batch.class, args);}}@SpringBootApplicationpublic class admin {public static void main(String[] args) {SpringApplication.run(admin.class, args);}}@SpringBootApplicationpublic class pc-web {public static void main(String[] args) {SpringApplication.run(pc-web.class, args);}}
- 멀티 모듈 작동 확인
디폴트로 실행할
MainClass
를 찾는데, 작업의 classpath 에서 디렉토리에 있는 메서드들 중public static void main(String [] args)
메서드를 가진 클래스를 자동으로 탐색해서 찾는다.