Maven Wrapper(mvnw) ๋ช
๋ น์ด, pom.xml ๊ตฌ์กฐ, ๋ผ์ดํ์ฌ์ดํด, ์์กด์ฑ ์ค์ฝํ, ํ๋ฌ๊ทธ์ธ ๋ฐ ๋ฉํฐ ๋ชจ๋ ๋ ํผ๋ฐ์ค
## 1. Maven Wrapper (mvnw) & ํต์ฌ CLI ๋ช
๋ น์ด
```bash
# 1. ๋น๋ ๋ฐ ํจํค์ง
./mvnw clean compile # target/ ๋๋ ํ ๋ฆฌ ์ ๋ฆฌ ํ ์์ค ์ฝ๋ ์ปดํ์ผ
./mvnw clean package # ์ปดํ์ผ + ํ
์คํธ ์ํ ํ JAR/WAR ์์ฑ
./mvnw clean install # ํจํค์ง ํ ๋ก์ปฌ ์ ์ฅ์(~/.m2/repository)์ ๋ฐฐํฌ
# 2. ํ
์คํธ ์คํต ์ต์
(์ฐจ์ด์ ์ฃผ์!)
./mvnw clean package -DskipTests # ํ
์คํธ ์ปดํ์ผ์ ์ํํ๋ ์คํ๋ง ๊ฑด๋๋ (๊ถ์ฅ)
./mvnw clean package -Dmaven.test.skip=true # ํ
์คํธ ์ปดํ์ผ ๋ฐ ์คํ์ ๋ชจ๋ ๊ฑด๋๋
# 3. ๋๋ฒ๊ทธ ๋ฐ ๊ฐ์ ์
๋ฐ์ดํธ
./mvnw clean package -U # ์๊ฒฉ ์ ์ฅ์์ SNAPSHOT ๋ฐ ์์กด์ฑ ์บ์ ๊ฐ์ ๊ฐฑ์
./mvnw clean package -X # ์์ธ ๋๋ฒ๊ทธ ๋ก๊ทธ ์ถ๋ ฅ (์์ธ ๋ถ์ ์)
./mvnw clean package -e # ์๋ฌ ๋ฐ์ ์ ์ ์ฒด ์คํํธ๋ ์ด์ค ์ถ๋ ฅ
# 4. ๋ฉํฐ์ค๋ ๋ ๋ณ๋ ฌ ๋น๋ (์๋ ์ต์ ํ)
./mvnw clean package -T 4 # 4๊ฐ ์ค๋ ๋๋ก ๋ณ๋ ฌ ๋น๋
./mvnw clean package -T 1C # CPU ์ฝ์ด๋น 1๊ฐ ์ค๋ ๋๋ก ์๋ ํ ๋น
# 5. Spring Boot ๋ก์ปฌ ์คํ
./mvnw spring-boot:run # ์คํ๋ง ๋ถํธ ์ฑ ์ฆ์ ์คํ
./mvnw spring-boot:run -Dspring-boot.run.profiles=local # ํ๋กํ์ผ ์ง์ ์คํ
```
## 2. ๊ธฐ๋ณธ ๋น๋ ๋ผ์ดํ์ฌ์ดํด (Build Lifecycle)
```text
[Maven 3๋ ํ์ค ๋ผ์ดํ์ฌ์ดํด]
1. clean : ์ด์ ๋น๋ ์ฐ์ถ๋ฌผ(target/) ์ ๊ฑฐ
2. default : ์์ค ๋ฐฐํฌ ๋ฐ ํจํค์ง (ํต์ฌ)
3. site : ํ๋ก์ ํธ ๋ฌธ์ ๋ฐ ์ฌ์ดํธ ์์ฑ
[default ๋ผ์ดํ์ฌ์ดํด ์ฃผ์ ํ์ด์ฆ(Phase) ์์]
validate โ ํ๋ก์ ํธ ์ํ ๋ฐ ํ์ ์ ๋ณด ๊ฒ์ฆ
compile โ ์์ค ์ฝ๋ ์ปดํ์ผ (src/main/java โ target/classes)
test โ ๋จ์ ํ
์คํธ ์คํ (ํ
์คํธ ์คํจ ์ ๋น๋ ์ค๋จ)
package โ ์ปดํ์ผ๋ ์ฝ๋๋ฅผ JAR/WAR ๋ฑ ๋ฐฐํฌ ํฌ๋งท์ผ๋ก ์์ถ
verify โ ํตํฉ ํ
์คํธ ๊ฒฐ๊ณผ ๊ฒ์ฆ
install โ ๋น๋๋ ํจํค์ง๋ฅผ ๋ก์ปฌ ์ ์ฅ์(~/.m2)์ ๋ฑ๋ก
deploy โ ์๊ฒฉ ๋ ํฌ์งํ ๋ฆฌ(Nexus, Artifactory)์ ์ต์ข
๋ฐฐํฌ
```
## 3. pom.xml ๊ธฐ๋ณธ ๊ณจ๊ฒฉ & ํ์ค ๋๋ ํ ๋ฆฌ
```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
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- 1. ํ๋ก์ ํธ ์ขํ (GAV) -->
<groupId>com.example</groupId>
<artifactId>my-app</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging> <!-- jar, war, pom -->
<!-- 2. ๊ณตํต ํ๋กํผํฐ (Java ๋ฒ์ , ์ธ์ฝ๋ฉ) -->
<properties>
<java.version>17</java.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
```
```text
[Maven ํ์ค ๋๋ ํ ๋ฆฌ ๊ตฌ์กฐ]
src/main/java โ ํ๋ก๋์
Java ์์ค ์ฝ๋
src/main/resources โ ์ค์ ํ์ผ (application.yml ๋ฑ) ๋ฐ ์ ์ ๋ฆฌ์์ค
src/test/java โ ๋จ์/ํตํฉ ํ
์คํธ ์ฝ๋
src/test/resources โ ํ
์คํธ์ฉ ์ค์ ํ์ผ
target/ โ ์ปดํ์ผ ์ฐ์ถ๋ฌผ ๋ฐ ์ต์ข
JAR/WAR ํ์ผ ์์ฑ ๊ฒฝ๋ก
pom.xml โ ํ๋ก์ ํธ ๊ฐ์ฒด ๋ชจ๋ธ(POM) ์ค์ ํ์ผ
```
## 4. ์์กด์ฑ ์ค์ฝํ (Dependency Scopes)
```xml
<dependencies>
<!-- 1. compile (๊ธฐ๋ณธ๊ฐ): ๋ชจ๋ ํด๋์คํจ์ค(์ปดํ์ผ, ์คํ, ํ
์คํธ)์ ํฌํจ, ์ ์ด ์์กด์ฑ ์ ํ -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 2. provided: ์ปดํ์ผ/ํ
์คํธ์๋ง ํ์, ๋ฐํ์์๋ ์ปจํ
์ด๋/JDK๊ฐ ์ ๊ณต (JAR ํจํค์ง ์ ์ธ) -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<!-- 3. runtime: ์ปดํ์ผ์๋ ๋ถํ์, ๋ฐํ์ ๋ฐ ํ
์คํธ ์คํ ์์๋ง ํ์ -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<!-- 4. test: ํ
์คํธ ์ปดํ์ผ ๋ฐ ์คํ์๋ง ํ์, ์ต์ข
๋ฐฐํฌ ์ฐ์ถ๋ฌผ์์ ์ ์ธ -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 5. import: dependencyManagement ์์์ BOM(Bill of Materials) ๊ฐ์ ธ์ฌ ๋๋ง ์ฌ์ฉ -->
</dependencies>
```
## 5. ์์กด์ฑ ๊ด๋ฆฌ & ์ถฉ๋ ํด๊ฒฐ (Tree & Exclusions)
```bash
# 1. ์์กด์ฑ ํธ๋ฆฌ ์ถ๋ ฅ (๋ฒ์ ์ถฉ๋ ๋ฐ ์ ์ด ์์กด์ฑ ํ์ธ)
./mvnw dependency:tree
# 2. ํน์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๊ฒ์ ํํฐ
./mvnw dependency:tree -Dincludes=org.slf4j:*
# 3. ์ฌ์ฉ๋์ง ์๊ฑฐ๋ ๋๋ฝ๋ ์์กด์ฑ ์ ์ ๋ถ์
./mvnw dependency:analyze
```
```xml
<!-- ์์กด์ฑ ์ ์ธ (Exclusion): ์ค๋ณต๋๊ฑฐ๋ ์ถฉ๋ํ๋ ์ ์ด ์์กด์ฑ ์ฐจ๋จ -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId> <!-- ๋ด์ฅ ํฐ์บฃ ์ ์ธ (Undertow ๋ฑ ๊ต์ฒด ์) -->
</exclusion>
</exclusions>
</dependency>
<!-- dependencyManagement: ํ์ ๋ชจ๋/์์กด์ฑ์ ๋ฒ์ ์ค์ ์ง์ค ๊ด๋ฆฌ (BOM ํจํด) -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2023.0.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
```
## 6. ์ฃผ์ ํ๋ฌ๊ทธ์ธ ์ค์ (Plugins)
```xml
<build>
<plugins>
<!-- 1. Java ์ปดํ์ผ๋ฌ & Annotation Processor(Lombok/MapStruct) -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>17</source>
<target>17</target>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<!-- 2. ๋จ์ ํ
์คํธ (๋จ์ผ ํ
์คํธ: ./mvnw test -Dtest=UserServiceTest) -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
</plugin>
<!-- 3. Spring Boot ์คํ ๊ฐ๋ฅํ Fat JAR ํจํค์ง -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
```
## 7. ๋ฉํฐ ๋ชจ๋ ํ๋ก์ ํธ (Multi-Module)
```xml
<!-- [Root pom.xml] -->
<groupId>com.example</groupId>
<artifactId>root-project</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging> <!-- ๋ฃจํธ๋ ๋ฐ๋์ pom ํจํค์ง -->
<modules>
<module>core-module</module>
<module>api-module</module>
</modules>
<!-- [Child api-module/pom.xml] -->
<parent>
<groupId>com.example</groupId>
<artifactId>root-project</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>api-module</artifactId>
<dependencies>
<!-- ๊ฐ์ ํ๋ก์ ํธ ๋ด ๋ค๋ฅธ ํ์ ๋ชจ๋ ์ฐธ์กฐ -->
<dependency>
<groupId>com.example</groupId>
<artifactId>core-module</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
```
```bash
# ๋ฉํฐ ๋ชจ๋ ๋น๋ ๊ฟํ
./mvnw clean package -pl api-module -am # api-module๊ณผ ํ์ํ ์์ ์์กด ๋ชจ๋๋ง ๊ณจ๋ผ์ ๋น๋
./mvnw clean package -pl core-module # core-module ํ๋๋ง ๋จ๋
๋น๋
```
## 8. ๋น๋ ํ๋กํ์ผ & ํ๊ฒฝ ๋ถ๋ฆฌ (Profiles)
```xml
<profiles>
<!-- ๋ก์ปฌ ๊ฐ๋ฐ ํ๊ฒฝ ํ๋กํ์ผ -->
<profile>
<id>local</id>
<activation>
<activeByDefault>true</activeByDefault> <!-- ๊ธฐ๋ณธ ํ์ฑํ -->
</activation>
<properties>
<env>local</env>
</properties>
</profile>
<!-- ์ด์ ํ๊ฒฝ ํ๋กํ์ผ -->
<profile>
<id>prod</id>
<properties>
<env>prod</env>
</properties>
</profile>
</profiles>
```
```bash
# ํน์ ํ๋กํ์ผ์ ํ์ฑํํ์ฌ ๋น๋
./mvnw clean package -P prod
./mvnw clean package -P local,metrics # ์ผํ๋ก ๋ค์ค ํ๋กํ์ผ ์ ์ฉ
```
## 9. ๋ฆฌ์์ค ํํฐ๋ง (Resource Filtering)
```xml
<!-- application.yml ๋ด @env@ ๋๋ ${env} ๋ณ์ ์นํ ํ์ฑํ -->
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering> <!-- ํ๋กํผํฐ ๊ฐ ์๋ ์นํ -->
<includes>
<include>**/*.properties</include>
<include>**/*.yml</include>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
```
```yaml
# src/main/resources/application.yml ์์
spring:
profiles:
active: @env@ # Maven ๋น๋ ์ ํ๋กํ์ผ ๋ณ์๋ก ์นํ๋จ
```
## 10. ์ค๋ฌด ํธ๋ฌ๋ธ์ํ
& ๊ฟํ (Tips & Troubleshooting)
```bash
# 1. Maven Wrapper ์์ฑ ๋๋ ๋ฒ์ ์
๊ทธ๋ ์ด๋
mvn wrapper:wrapper -Dmaven=3.9.6
# 2. ๋ก์ปฌ ์บ์(~/.m2/repository) ๊ผฌ์ ํด๊ฒฐ
# ๋น๋ ์คํจ๋ corrupted jar ๋ฐ์ ์ ํด๋น ๋ผ์ด๋ธ๋ฌ๋ฆฌ ํด๋ ์ญ์ ํ -U ์ต์
์ผ๋ก ์ฌ๋ค์ด๋ก๋
rm -rf ~/.m2/repository/com/example/corrupted-lib/
./mvnw clean package -U
# 3. ๋จ์ผ ํ
์คํธ ํด๋์ค ๋๋ ํน์ ๋ฉ์๋๋ง ์คํ
./mvnw test -Dtest=OrderServiceTest
./mvnw test -Dtest=OrderServiceTest#testCreateOrder
# 4. ๋ฐฐํฌ ๋ฐ ์๊ฒฉ ์ ์ฅ์ ๋ฑ๋ก (pom.xml ๋ด distributionManagement ์ค์ ํ์)
./mvnw clean deploy
# 5. ์คํ๋ผ์ธ ๋ชจ๋ ๋น๋ (์ธ๋ถ ๋คํธ์ํฌ ํธ์ถ ์ฐจ๋จ)
./mvnw clean package -o
```
์๊ฒฌ ๋ฐ ์ง๋ฌธ
0์์ง ๋ฑ๋ก๋ ์๊ฒฌ์ด ์์ต๋๋ค. ์ฒซ ๋ฒ์งธ ๋๊ธ์ ๋จ๊ฒจ๋ณด์ธ์!
๋๊ธ ์์
๋๊ธ ์ญ์