๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Spring

Spring ์ธํ…”๋ฆฌ์ œ์ด Service, Common, Batch ๋ชจ๋“ˆ ๋ถ„๋ฆฌํ•˜๊ธฐ (Maven)

by ํ‘์‹œ๋ฐ” 2023. 9. 17.

๐Ÿ™„ ๋ฐฐ๊ฒฝ

ํ† ์ด ํ”„๋กœ์ ํŠธ์™€ ํšŒ์‚ฌ ํ”„๋กœ์ ํŠธ์—์„œ Spring, IntelliJ, Maven์„ ๊ธฐ๋ฐ˜์œผ๋กœ ๋ชจ๋“ˆ ๋ถ„๋ฆฌ๋ฅผ ํ•˜๋ฉด์„œ ์•Œ๊ฒŒ๋œ ๋ชจ๋“ˆ ๋ถ„๋ฆฌ ๋ฐฉ๋ฒ•๊ณผ ๋ชจ๋“ˆ ๋ถ„๋ฆฌ ๊ณผ์ •์—์„œ ๊ฒช์„ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ์„ค์ • ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ๊ณต์œ ํ•˜๊ณ ์ž ํ•œ๋‹ค.

๐Ÿ‘ ๋ชจ๋“ˆ ๋ถ„๋ฆฌ๋ฅผ ํ•ด์•ผ ํ•˜๋Š” ์ด์œ ?

- ์˜์กด์„ฑ ์ตœ์†Œํ™”

์˜ˆ๋ฅผ ๋“ค์–ด, ๋™์ผํ•œ ๋ชจ๋“ˆ์—์„œ ๋ฐฐ์น˜ ์„œ๋น„์Šค๋ฅผ ๊ฐœ๋ฐœํ•  ๋•Œ๋Š” ์Šคํ”„๋ง ์‹œํ๋ฆฌํ‹ฐ๊ฐ€ ํ•„์š” ์—†์„ ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ด๋•Œ ๋™์ผํ•œ ๋ชจ๋“ˆ์ด๋ผ๋Š” ์ด์œ ๋กœ ๊ฐ•์ œ๋กœ ์Šคํ”„๋ง ์‹œํ๋ฆฌํ‹ฐ ์˜์กด์„ฑ์„ ํฌํ•จํ•˜๊ฒŒ ๋˜๋ฉด์„œ ๊ฐœ๋ฐœ/ํ…Œ์ŠคํŠธ ๊ณผ์ •์—์„œ ์‹œํ๋ฆฌํ‹ฐ ๋•Œ๋ฌธ์— ์˜ํ–ฅ์„ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.

 

๋ชจ๋“ˆ์„ ๋ถ„๋ฆฌํ•˜๋ฉด ํ•„์š”ํ•œ ์ตœ์†Œ ์˜์กด์„ฑ๋งŒ ๊ด€๋ฆฌํ•จ์œผ๋กœ์จ ์ด๋Ÿฌํ•œ ์˜์กด์„ฑ ๊ด€๋ จ ๋ฌธ์ œ/์˜ํ–ฅ์„ ๊ณ ๋ คํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค.

- ์œ ์ง€๋ณด์ˆ˜

ํŠน์ • ๋ชจ๋“ˆ์— ์žฅ์• ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค๋ฉด ํ•ด๋‹น ๋ชจ๋“ˆ๋งŒ ํ™•์ธํ•˜๋ฉด ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์‰ฌ์›Œ์ง„๋‹ค. ๋˜ํ•œ, ์žฅ์• ๋กœ ํ•ด๋‹น ๋ชจ๋“ˆ์ด ๊ฐ‘์ž‘์Šค๋Ÿฝ๊ฒŒ ์ข…๋ฃŒ๋œ๋‹ค๊ณ  ํ•ด๋„ ๋‹ค๋ฅธ ์„œ๋น„์Šค ๋ชจ๋“ˆ๋“ค์€ ์˜ํ–ฅ์„ ๋ฐ›์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ํ”ผํ•ด๋ฅผ ์ตœ์†Œํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์žฅ์ ๋„ ์žˆ๋‹ค.

0. ์ค€๋น„

ํฌ์ŠคํŒ…์— ์‚ฌ์šฉ๋œ ํ™˜๊ฒฝ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

 

- JDK17

- IntelliJ

- Maven

- Spring Boot 3.1.3

 

๊ฐ€์žฅ ํ”ํ•˜๊ณ  ์˜ˆ์‹œ๋กœ ์ ์ ˆํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋Š” ์„œ๋น„์Šค, ๊ณตํ†ต, ๋ฐฐ์น˜ ๋ชจ๋“ˆ 3๊ฐœ๋กœ ๋ถ„๋ฆฌํ•ด๋ณด๋ ค๊ณ  ํ•œ๋‹ค.

1. ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ

2. ๋ชจ๋“ˆ ์ƒ์„ฑํ•˜๊ธฐ

File > New > Module > New Module์— ์ ‘๊ทผํ•ด์„œ service, batch, common์„ ์ƒ์„ฑํ•œ๋‹ค.

 

 

๋ชจ๋“ˆ์„ ๋ชจ๋‘ ์ƒ์„ฑํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ๊ตฌ์กฐ๊ฐ€ ๋œ๋‹ค.

 

3. ๊ฐ ๋ชจ๋“ˆ์— ์˜์กด์„ฑ ์ถ”๊ฐ€ํ•˜๊ธฐ

๊ฐ ๋ชจ๋“ˆ์— ํ•„์š”ํ•œ ์˜์กด์„ฑ์„ pom.xml์— ์ถ”๊ฐ€ํ•œ๋‹ค.

 

์ด๋ฆ„ ์ถ”๊ฐ€ํ•œ ๋ชจ๋“ˆ
service web, security, security-test, common
batch batch, batch-test, common
common data-jpa, mariadb, h2
parent lombok, starter-test

lombok๊ณผ starter-test๋Š” ์–ด๋””์—์„œ๋„ ์“ธ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— parent ๋ชจ๋“ˆ์— ์˜์กด์„ฑ์„ ์ถ”๊ฐ€ํ–ˆ๋‹ค.

4. ํ”Œ๋Ÿฌ๊ทธ์ธ ์ถ”๊ฐ€ํ•˜๊ธฐ

spring-boot-maven-plugin ํ”Œ๋Ÿฌ๊ทธ์ธ์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

(ํ•ด๋‹น ํ”Œ๋Ÿฌ๊ทธ์ธ์€ ํŒจํ‚ค์ง• ํ•  ๋•Œ executable jar ํŒŒ์ผ๋กœ ๊ตฌ์กฐ๋ฅผ ๋ณ€๊ฒฝํ•ด ์ฃผ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.)

 

spring-boot-maven-plugin๋Š” ๋ฉ”์ธ ํด๋ž˜์Šค๊ฐ€ ํฌํ•จ๋œ ๋ชจ๋“ˆ์—๋งŒ ์ถ”๊ฐ€ํ•ด์•ผ ํ•˜๋ฏ€๋กœ, ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์—ญํ• ์„ ํ•˜๋Š” common ๋ชจ๋“ˆ ์™ธ์— service, batch ๋ชจ๋“ˆ์— ๋ณ„๋„๋กœ ์ถ”๊ฐ€ํ•œ๋‹ค.

(common์—๋„ ํฌํ•จํ•˜๋ฉด main ํด๋ž˜์Šค๋ฅผ ์ฐพ์ง€ ๋ชปํ•ด ํŒจํ‚ค์ง•์— ์‹คํŒจํ•˜๊ฒŒ ๋œ๋‹ค.)

 

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

 

ํ”Œ๋Ÿฌ๊ทธ์ธ๊นŒ์ง€ ์ถ”๊ฐ€๊ฐ€ ์™„๋ฃŒ๋˜๋ฉด, ๊ธฐ๋ณธ์ ์ธ ๋ชจ๋“ˆ ๋ถ„๋ฆฌ๋Š” ์ด๊ฑธ๋กœ ๋์ด ๋‚œ๋‹ค.

ํ•˜์ง€๋งŒ ์ด๋Œ€๋กœ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์‹คํ–‰ํ•˜๋ ค๊ณ  ํ•˜๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜๋ฐ–์— ์—†๋‹ค.

 

์ด์ œ ์„œ๋น„์Šค ๋ชจ๋“ˆ์—์„œ JPA์™€ common ๋ชจ๋“ˆ์„ ์Šค์บ”ํ•˜๋Š” ์„ค์ • ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์ž.

5. ์„ค์ •

service์—์„œ๋Š” common ๋ชจ๋“ˆ์— ์žˆ๋Š” ํด๋ž˜์Šค๋“ค์„ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๋ฏ€๋กœ ๋•Œ๋ฌธ์—, common ๋ชจ๋“ˆ ํŒจํ‚ค์ง€๊นŒ์ง€ ํฌํ•จํ•ด์„œ ์ปดํฌ๋„ŒํŠธ ์Šค์บ”์„ ํ•ด์•ผ ํ•œ๋‹ค.

 

@SpringBootApplication(scanBasePackages = {"com.example"})
public class ServiceApplication {

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

 

service ๋ชจ๋“ˆ์˜ ํŒจํ‚ค์ง€ ๊ฒฝ๋กœ๋Š” com.example.service์ด๊ณ , common ๋ชจ๋“ˆ์˜ ํŒจํ‚ค์ง€ ๊ฒฝ๋กœ๋Š” com.example.common ์ด๋ฏ€๋กœ ์ƒ์œ„ ํŒจํ‚ค์ง€ ๊ฒฝ๋กœ์ธ com.example ๊นŒ์ง€๋งŒ ์ž…๋ ฅํ•˜๋ฉด ๊ด€๋ จ ๋ชจ๋“  ์„ค์ •์„ ๊ฐ€์ง€๊ณ  ์˜ฌ ์ˆ˜ ์žˆ๋‹ค. ๋งŒ์•ฝ, ํŒจํ‚ค์ง€ ๊ฒฝ๋กœ๊ฐ€ ๊ณตํ†ต๋œ ๋ถ€๋ถ„์ด ์—†๊ฑฐ๋‚˜ ๋‹ค๋ฅด๊ฒŒ ๋‚˜๋ˆ ์•ผ ํ•˜๋Š” ๋“ฑ ๋‹ค๋ฅธ ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค๋ฉด ํ•„์š”์— ๋”ฐ๋ผ ์ถ”๊ฐ€์ ์œผ๋กœ ๊ฒฝ๋กœ๋ฅผ ์ž‘์„ฑํ•˜๋ฉด ๋œ๋‹ค.

 

ํ•˜์ง€๋งŒ ์ปดํฌ๋„ŒํŠธ ์Šค์บ”์„ ํ–ˆ์Œ์—๋„, JPA๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์‹คํ–‰ํ•  ๋•Œ Repository ๋ถ€๋ถ„์—์„œ ํ•ด๋‹น Bean์„ ์ฐพ์ง€ ๋ชปํ•œ๋‹ค๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๋ณดํ†ต JPA๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด common ๋ชจ๋“ˆ์— Entity, Repository ๋“ฑ JPA์™€ ๊ด€๋ จ๋œ ํด๋ž˜์Šค๋ฅผ ์ž‘์„ฑํ•  ํ…๋ฐ, ์ด๋“ค์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ณ„๋„ ์„ค์ •์ด ํ•„์š”ํ•˜๋‹ค.

 

@Configuration
@EntityScan(basePackages = "com.example.common.entity")
@EnableJpaRepositories(basePackages = "com.example.common.repository")
@EnableJpaAuditing
public class JpaConfig {

}

 

Spring Data JPA์—์„œ ์ง€์›ํ•˜๋Š” @EntityScan, @EnableJpaRepositories ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ ์—”ํ‹ฐํ‹ฐ ํŒจํ‚ค์ง€์™€ Repository ํŒจํ‚ค์ง€ ๊ฒฝ๋กœ๋ฅผ ๋”ฐ๋กœ ์„ค์ • ํŒŒ์ผ์— ์ž‘์„ฑํ•ด์•ผ service ๋ชจ๋“ˆ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๋งŒ์•ฝ BaseEntity ๋“ฑ์˜ ๊ณตํ†ต ์—”ํ‹ฐํ‹ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๋ผ๋ฉด @EnableJpaAuditing๋„ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜์—ฌ JPA ๊ด€๋ จ ์„ค์ •์„ ํ•˜๋‚˜์˜ ํด๋ž˜์Šค์— ์ž‘์„ฑํ•˜๋ฉด ๊ด€๋ฆฌํ•˜๊ธฐ ์ข‹๋‹ค.

 

REFERENCE

https://stackoverflow.com/questions/70833195/spring-boot-multi-module-spring-boot-maven-plugin-compilation-failure

๋Œ“๊ธ€