Spring Boot와 Maven을 활용한 다모듈 프로젝트 구성

이 문서에서는 Eclipse IDE를 사용하여 Spring Boot 기반의 다모듈 구조를 갖춘 프로젝트를 설정하는 방법을 설명합니다. 전체 프로젝트 이름은 student이며, 다음과 같은 세 가지 하위 모듈로 구성됩니다: student-web, student-setting, student-util.

  • student-web: 웹 애플리케이션의 진입점으로, war 형식으로 빌드되며 스프링 부트 애플리케이션을 실행합니다.
  • student-setting: 비즈니스 로직 처리 및 데이터 조작 기능을 포함하며, jar 형태로 패키징되어 다른 모듈에서 의존성으로 사용됩니다.
  • student-util: 공통 유틸리티 클래스(예: 날짜 포맷팅)를 보유하며, 다른 모듈에서 재사용 가능합니다.

특히 student-setting 모듈은 student-util의 도구 클래스를 직접 참조할 수 있도록 구성됩니다.

1. 부모 프로젝트 생성

메뉴 File > New > Maven Project를 선택하고, maven-archetype-quickstart 템플릿을 사용하여 기본 프로젝트를 생성합니다.

생성 후, srctarget 디렉토리를 삭제하고, 남은 pom.xml 파일만 유지합니다.

부모 프로젝트의 pom.xml을 아래와 같이 수정합니다:

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.0</version>
        <relativePath/>
    </parent>

    <groupId>com.demo.student</groupId>
    <artifactId>student</artifactId>
    <name>student</name>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencyManagement>
        <dependencies>
            <!-- 의존성 관리 영역 -->
        </dependencies>
    </dependencyManagement>
</project>

2. student-web 모듈 설정

student-web 모듈은 웹 애플리케이션의 시작점이므로, war 패키지 형식으로 설정해야 합니다.

새 모듈 생성 시 maven-archetype-quickstart를 선택하고, 다음을 수행합니다:

  • student-web/pom.xml<packaging>war</packaging> 추가
  • 부모 설정: groupId, artifactId, version을 부모 프로젝트와 일치시킴
  • 프로젝트 루트 패키지에 StudentApplication.java를 위치시키는 것이 중요합니다. 스프링 부트는 이 패키지를 기준으로 컴포넌트 스캔을 수행합니다.

최종 student-web/pom.xml 예시:

<?xml version="1.0"?>
<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.demo.student</groupId>
        <artifactId>student</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <artifactId>student-web</artifactId>
    <name>student-web</name>
    <packaging>war</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>

실행 클래스 예시:

package com.demo.student;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@EnableScheduling
@SpringBootApplication(scanBasePackages = { "com.demo.student" }, exclude = {
    org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration.class,
    org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration.class
})
public class StudentApplication {

    public static void main(String[] args) {
        SpringApplication.run(StudentApplication.class, args);
    }
}

3. student-setting 모듈 구성

학생 정보 관리 기능을 담당하는 모듈입니다. jar로 패키징하고, 부모 프로젝트에서 의존성 관리 대상으로 등록합니다.

  • student-setting/pom.xml<packaging>jar</packaging> 설정
  • student/pom.xml<dependencyManagement> 섹션에 의존성 선언
  • student-web/pom.xml에서 해당 모듈을 의존성으로 추가

기능 검증을 위해 간단한 컨트롤러 추가:

package com.demo.student.setting;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/setting")
public class StudentSettingController {

    @GetMapping("/save")
    public String save() {
        return "저장 완료";
    }
}

4. student-util 모듈 개발

공통 유틸리티 클래스를 포함하는 모듈입니다. 예를 들어, 날짜 관련 기능을 제공합니다.

모듈 생성 후, student-util/pom.xml<packaging>jar</packaging>를 추가하고, 부모 프로젝트에 의존성 등록합니다.

유틸리티 클래스 예시:

package com.demo.student.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

    public static String yesterday() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_YEAR, -1);
        return sdf.format(cal.getTime());
    }

    public static String today() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        return sdf.format(new Date());
    }
}

student-setting 모듈에서 이를 사용하기 위해 student-setting/pom.xml에 의존성 추가:

<dependency>
    <groupId>com.demo.student</groupId>
    <artifactId>student-util</artifactId>
</dependency>

컨트롤러 업데이트 예시:

@GetMapping("/save")
public String save() {
    String today = DateUtil.today();
    String yesterday = DateUtil.yesterday();
    return "오늘: " + today + ", 어제: " + yesterday;
}

5. 빌드 및 실행

모든 모듈의 빌드 플러그인을 올바르게 설정합니다.

부모 프로젝트의 pom.xml에 플러그인 관리 추가:

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

student-web/pom.xml에는 실제 플러그인 정의 포함:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

터미널에서 프로젝트 루트에서 다음 명령어 실행:

mvn clean package -Dmaven.test.skip=true

빌드 완료 후, student/student-web/target/ 폴더 내에 student-web-0.0.1-SNAPSHOT.war 파일 생성됨.

WAR 파일 실행:

java -jar student-web-0.0.1-SNAPSHOT.war

브라우저에서 http://localhost:8080/api/setting/save 접속 시 결과 확인 가능.

태그: Spring Boot maven Multi-module project war packaging jar dependency

7월 12일 22:12에 게시됨