이전에 스프링으로 서버 운영시 모든 소스를 하나의 모듈에 묶어 운영하여 유지보수가 어려웠다.
또 프로파일 설정으로 각 서버에 적용되어야 하는 파일을 분리하여 적용하려고 하니 참 무식했다...
필자도 멀티 모듈을 적용하여 서버를 운영하고자한다.
그래들 설정에 대해 아직 기본도 잘 모르는 초짜이기에 일단 설정을 따라해본다.
우선 start.spring.io 로 프로젝트를 생성하고 진행해보자
루트 build.gradle 설정을 아래와 같이 해주자
buildscript {
ext {
springBootVersion = '2.3.9'
}
repositories {
mavenCentral()
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
classpath "io.spring.gradle:dependency-management-plugin:1.0.11.RELEASE"
}
}
subprojects {
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group = 'me.zeroest'
version = '0.0.1'
sourceCompatibility = 11
repositories {
mavenCentral()
}
task initSourceFolders {
sourceSets*.java.srcDirs*.each {
if( !it.exists() ) {
it.mkdirs()
}
}
sourceSets*.resources.srcDirs*.each {
if( !it.exists() ) {
it.mkdirs()
}
}
}
dependencies {
compileOnly('org.projectlombok:lombok')
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
}
channel-core의 build.gradle에 아래 dependencies와 같이 해당 모듈에 필요한 라이브러리를 추가하고
core 모듈의 경우 단순 참조용 모듈이기 때문에 아래bootJar와 같이 설정한다.
bootJar{
enabled = false;
}
jar {
enabled = true;
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
runtimeOnly 'com.h2database:h2'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
}
main 메서드가 없는 모듈이어도 테스트 코드를 돌리기 위해서 아래와 같이 스프링컨테이너를 띄워줘야
테스트 코드에서 스프링 빈 주입이 된다.
package me.zeroest.channel;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
class ChannelApplicationTests {
@Test
void contextLoads() {
}
}
package me.zeroest.channel.repository;
import me.zeroest.channel.domain.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest
class UserRepositoryTest {
@Autowired
private UserRepository userRepository;
@Test
void getTest(){
User user = new User("test", "userName");
userRepository.save(user);
User findUser = userRepository.findById(user.getId()).get();
assertNotNull(findUser);
}
}
코어 모듈은 여기까지 정리하고 api 모듈을 위와같이 추가한다.
setting.gradle에 include가 되었는지 확인한다.
package me.zeroest.channel;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ChannelApplication {
public static void main(String[] args) {
SpringApplication.run(ChannelApplication.class, args);
}
}
api 모듈에는 위와 같이 main메서드를 위치시켜 컨테이너를 띄우고 api 서버를 운영하겠다.
마지막으로 루트 build.gradle에 아래와 같이 의존성에 맞게 모듈을 추가해준다.
project(':channel-api'){
dependencies {
compile project(':channel-core')
}
}
github.com/zeroest/channel-onetoone/commit/df2969767fb92cb830885ab8e6caca0f74aba376
Apply multi modules · zeroest/channel-onetoone@df29697
Permalink This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository. Browse files Apply multi modules Loading branch information Showing 19 changed files with 363 additions and 34 deletions. +57 −15
github.com
참고 자료
woowabros.github.io/study/2019/07/01/multi-module.html
'Spring' 카테고리의 다른 글
Zipkin - Docker MySQL (0) | 2021.07.25 |
---|---|
Spring boot 2.5 이상 docker를 위한 unpack (0) | 2021.07.17 |
Mapstruct & Lombok 적용 (3) | 2021.04.16 |
[Jpa] ddl-auto: create 시 foreign key 제거 (0) | 2020.11.17 |
JPA - 클라이언트 파라미터에 따른 동적 데이터베이스 연결 (AbstractRoutingDataSourceAbstractRoutingDataSource) (0) | 2020.10.30 |