Maven 빌드 의존성 처리 과정
mvn compile 명령을 실행하여 소스 코드를 컴파일할 때, 필요한 외부 라이브러리가 있다면 Maven은 pom.xml 파일에서 해당 의존성의 좌표 정보를 확인한다. 예시:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
</dependencies>
좌표 정보를 확인한 후에는 로컬 저장소에서 해당 의존성을 검색한다. 로컬 저장소에 존재하지 않으면 중앙 저장소나 원격 저장소에서 다운로드하여 로컬 저장소에 저장한다.
예시: 로컬 저장소 의존성 사용하기
1 - 의존성 JAR 파일 로컬 저장소 설치
mvn install 명령으로 JAR 패키지를 로컬 저장소에 설치: com.example.project01.GreetingUtil
devuser@DESKTOP-ABC123 MINGW64 /d/Workspace/Projects/Project01
$ mvn install
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building project01 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
Downloading: http://central.maven.org/maven2/org/apache/maven/plugins/maven-install-plugin/2.4/maven-install-plugin-2.4.pom
Downloaded: http://central.maven.org/maven2/org/apache/maven/plugins/maven-install-plugin/2.4/maven-install-plugin-2.4.pom (0 B at 0 B/s)
Downloading: http://central.maven.org/maven2/org/apache/maven/plugins/maven-install-plugin/2.4/maven-install-plugin-2.4.jar
Downloaded: http://central.maven.org/maven2/org/apache/maven/plugins/maven-install-plugin/2.4/maven-install-plugin-2.4.jar (0 B at 0 B/s)
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ project01 ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\Workspace\Projects\Project01\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ project01 ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to D:\Workspace\Projects\Project01\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ project01 ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\Workspace\Projects\Project01\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ project01 ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to D:\Workspace\Projects\Project01\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ project01 ---
[INFO] Surefire report directory: D:\Workspace\Projects\Project01\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.example.project01.GreetingUtilTest
Execute test!
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.057 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ project01 ---
[INFO] Building jar: D:\Workspace\Projects\Project01\target\project01-1.0.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ project01 ---
Downloading: http://central.maven.org/maven2/org/codehaus/plexus/plexus-utils/3.0.5/plexus-utils-3.0.5.pom
Downloaded: http://central.maven.org/maven2/org/codehaus/plexus/plexus-utils/3.0.5/plexus-utils-3.0.5.pom (2.5 kB at 4.7 kB/s)
Downloading: http://central.maven.org/maven2/org/codehaus/plexus/plexus/3.1/plexus-3.1.pom
Downloaded: http://central.maven.org/maven2/org/codehaus/plexus/plexus/3.1/plexus-3.1.pom (19 kB at 24 kB/s)
Downloading: http://central.maven.org/maven2/org/codehaus/plexus/plexus-digest/1.0/plexus-digest-1.0.pom
Downloaded: http://central.maven.org/maven2/org/codehaus/plexus/plexus-digest/1.0/plexus-digest-1.0.pom (1.1 kB at 1.9 kB/s)
Downloading: http://central.maven.org/maven2/org/codehaus/plexus/plexus-components/1.1.7/plexus-components-1.1.7.pom
Downloaded: http://central.maven.org/maven2/org/codehaus/plexus/plexus-components/1.1.7/plexus-components-1.1.7.pom (5.0 kB at 8.9 kB/s)
Downloading: http://central.maven.org/maven2/org/codehaus/plexus/plexus/1.0.8/plexus-1.0.8.pom
Downloaded: http://central.maven.org/maven2/org/codehaus/plexus/plexus/1.0.8/plexus-1.0.8.pom (7.2 kB at 13 kB/s)
Downloading: http://central.maven.org/maven2/org/codehaus/plexus/plexus-container-default/1.0-alpha-8/plexus-container-default-1.0-alpha-8.pom
Downloaded: http://central.maven.org/maven2/org/codehaus/plexus/plexus-container-default/1.0-alpha-8/plexus-container-default-1.0-alpha-8.pom (7.3 kB at 13 kB/s)
Downloading: http://central.maven.org/maven2/org/codehaus/plexus/plexus-utils/3.0.5/plexus-utils-3.0.5.jar
Downloading: http://central.maven.org/maven2/org/codehaus/plexus/plexus-digest/1.0/plexus-digest-1.0.jar
Downloaded: http://central.maven.org/maven2/org/codehaus/plexus/plexus-digest/1.0/plexus-digest-1.0.jar (12 kB at 9.8 kB/s)
Downloaded: http://central.maven.org/maven2/org/codehaus/plexus/plexus-utils/3.0.5/plexus-utils-3.0.5.jar (230 kB at 179 kB/s)
[INFO] Installing D:\Workspace\Projects\Project01\target\project01-1.0.0-SNAPSHOT.jar to D:\MavenRepo\com\example\project01\project01\1.0.0-SNAPSHOT\project01-1.0.0-SNAPSHOT.jar
[INFO] Installing D:\Workspace\Projects\Project01\pom.xml to D:\MavenRepo\com\example\project01\project01\1.0.0-SNAPSHOT\project01-1.0.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 9.602 s
[INFO] Finished at: 2017-10-20T17:14:19+08:00
[INFO] Final Memory: 18M/210M
[INFO] ------------------------------------------------------------------------
devuser@DESKTOP-ABC123 MINGW64 /d/Workspace/Projects/Project01
$ ls -l
total 5
-rw-r--r-- 1 devuser 1049089 561 Oct 19 17:44 pom.xml
drwxr-xr-x 1 devuser 1049089 0 Oct 19 13:21 src/
drwxr-xr-x 1 devuser 1049089 0 Oct 20 17:14 target/
devuser@DESKTOP-ABC123 MINGW64 /d/Workspace/Projects/Project01
$ ls -l target/
total 4
drwxr-xr-x 1 devuser 1049089 0 Oct 20 17:14 classes/
drwxr-xr-x 1 devuser 1049089 0 Oct 20 17:14 maven-archiver/
drwxr-xr-x 1 devuser 1049089 0 Oct 20 17:14 maven-status/
drwxr-xr-x 1 devuser 1049089 0 Oct 20 17:14 surefire-reports/
drwxr-xr-x 1 devuser 1049089 0 Oct 20 17:14 test-classes/
-rw-r--r-- 1 devuser 1049089 2150 Oct 20 17:14 project01-1.0.0-SNAPSHOT.jar
devuser@DESKTOP-ABC123 MINGW64 /d/Workspace/Projects/Project01
$
2 - 로컬 저장소 의존성을 사용하는 Maven 프로젝트 생성 (코드 포함)
devuser@DESKTOP-ABC123 MINGW64 /d/Workspace/Projects
$ ls -l
total 4
drwxr-xr-x 1 devuser 1049089 0 Sep 1 16:50 Sample/
drwxr-xr-x 1 devuser 1049089 0 Oct 20 17:14 Project01/
drwxr-xr-x 1 devuser 1049089 0 Oct 20 17:22 Project02/
devuser@DESKTOP-ABC123 MINGW64 /d/Workspace/Projects
$ cd Project02/
devuser@DESKTOP-ABC123 MINGW64 /d/Workspace/Projects/Project02
$ ls -l
total 1
-rw-r--r-- 1 devuser 1049089 729 Oct 20 17:13 pom.xml
drwxr-xr-x 1 devuser 1049089 0 Oct 20 16:56 src/
devuser@DESKTOP-ABC123 MINGW64 /d/Workspace/Projects/Project02
$ cat pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>
<groupId>com.example.project02</groupId>
<artifactId>project02</artifactId>
<version>1.0.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
<dependency>
<groupId>com.example.project01</groupId>
<artifactId>project01</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
devuser@DESKTOP-ABC123 MINGW64 /d/Workspace/Projects/Project02
$ cat src/main/java/com/example/project02/Messenger.java
package com.example.project02;
import com.example.project01.GreetingUtil;
public class Messenger {
public String getGreeting() {
return new GreetingUtil().generateMessage();
}
}
devuser@DESKTOP-ABC123 MINGW64 /d/Workspace/Projects/Project02
$ cat src/test/java/com/example/project02/MessengerTest.java
package com.example.project02;
import org.junit.*;
import org.junit.Assert.*;
public class MessengerTest {
@Test
public void testGetGreeting() {
System.out.println("Execute test!");
Assert.assertEquals("Hello Maven!", new Messenger().getGreeting());
}
}
devuser@DESKTOP-ABC123 MINGW64 /d/Workspace/Projects/Project02
$
3- mvn compile, test, package 명령 실행
devuser@DESKTOP-ABC123 MINGW64 /d/Workspace/Projects/Project02
$ ll
total 1
-rw-r--r-- 1 devuser 1049089 729 Oct 20 17:13 pom.xml
drwxr-xr-x 1 devuser 1049089 0 Oct 20 16:56 src/
devuser@DESKTOP-ABC123 MINGW64 /d/Workspace/Projects/Project02
$
devuser@DESKTOP-ABC123 MINGW64 /d/Workspace/Projects/Project02
$ mvn compile
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building project02 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ project02 ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\Workspace\Projects\Project02\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ project02 ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to D:\Workspace\Projects\Project02\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.067 s
[INFO] Finished at: 2017-10-20T17:28:30+08:00
[INFO] Final Memory: 15M/304M
[INFO] ------------------------------------------------------------------------
devuser@DESKTOP-ABC123 MINGW64 /d/Workspace/Projects/Project02
$
devuser@DESKTOP-ABC123 MINGW64 /d/Workspace/Projects/Project02
$ mvn test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building project02 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ project02 ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\Workspace\Projects\Project02\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ project02 ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ project02 ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\Workspace\Projects\Project02\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ project02 ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to D:\Workspace\Projects\Project02\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ project02 ---
[INFO] Surefire report directory: D:\Workspace\Projects\Project02\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.example.project02.MessengerTest
Execute test!
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.069 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.468 s
[INFO] Finished at: 2017-10-20T17:28:50+08:00
[INFO] Final Memory: 15M/271M
[INFO] ------------------------------------------------------------------------
devuser@DESKTOP-ABC123 MINGW64 /d/Workspace/Projects/Project02
$
devuser@DESKTOP-ABC123 MINGW64 /d/Workspace/Projects/Project02
$ mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building project02 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ project02 ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\Workspace\Projects\Project02\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ project02 ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ project02 ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\Workspace\Projects\Project02\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ project02 ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to D:\Workspace\Projects\Project02\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ project02 ---
[INFO] Surefire report directory: D:\Workspace\Projects\Project02\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.example.project02.MessengerTest
Execute test!
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.069 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ project02 ---
[INFO] Building jar: D:\Workspace\Projects\Project02\target\project02-1.0.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.830 s
[INFO] Finished at: 2017-10-20T17:29:05+08:00
[INFO] Final Memory: 17M/258M
[INFO] ------------------------------------------------------------------------
devuser@DESKTOP-ABC123 MINGW64 /d/Workspace/Projects/Project02
$
devuser@DESKTOP-ABC123 MINGW64 /d/Workspace/Projects/Project02
$ ls -l target/
total 4
drwxr-xr-x 1 devuser 1049089 0 Oct 20 17:28 classes/
drwxr-xr-x 1 devuser 1049089 0 Oct 20 17:29 maven-archiver/
drwxr-xr-x 1 devuser 1049089 0 Oct 20 17:28 maven-status/
drwxr-xr-x 1 devuser 1049089 0 Oct 20 17:28 surefire-reports/
drwxr-xr-x 1 devuser 1049089 0 Oct 20 17:28 test-classes/
-rw-r--r-- 1 devuser 1049089 2202 Oct 20 17:29 project02-1.0.0-SNAPSHOT.jar
devuser@DESKTOP-ABC123 MINGW64 /d/Workspace/Projects/Project02
$