1. 설치
http://maven.apache.org/의 [Download] 메뉴에서 다운로드 받는다. Maven 3.3은 JDK 1.7 이상을 필요로 한다. PC에 설치하는 경우 Binary zip archive 버전을 받으면 된다. 이 문서는 3.3.9를 기준으로 한다.
2. Repository 위치 변경
apache-maven-3.3.9\conf 디렉토리에 들어가면 settings.xml 파일이 있다. 만약 Repository 위치를 변경하고자 한다면 settings.xml 파일에서 <localRepository>를 수정하면 된다. 기본적으로는 주석처리되어 있으며 기본 값은 ${user.home}/.m2/repository이다. 만약 Repository 위치를 d:\apache-maven-3.3.9\repository로 변경하려면 다음과 같이 설정한다.
<localRepository>D:\apache-maven-3.3.9\repository</localRepository>
3. Maven 실행 확인
Maven 설치 후에는 PC의 환경 변수에 Maven 설치 디렉토리 하위의 bin 디렉토리를 PATH로 잡는다. 예를 들어 d:\apache-maven-3.3.9\bin을 PATH에 추가한다. PATH 추가 후에는 cmd 창에서 mvn을 실행하여 정상적으로 PATH에 추가되었는지 확인한다.
C:\Users\viktor>mvn -version Error: JAVA_HOME not found in your environment. Please set the JAVA_HOME variable in your environment to match the location of your Java installation.
JAVA_HOME이 설정되어 있지 않아 정상적으로 실행할 수 없다. 환경 변수에 JAVA_HOME 추가 후 다시 실행해 보자.
C:\Users\viktor>mvn -version Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-11T01:41:4 7+09:00) Maven home: D:\apache-maven-3.3.9\bin\.. Java version: 1.7.0_79, vendor: Oracle Corporation Java home: C:\Program Files\Java\jdk1.7.0_79\jre Default locale: ko_KR, platform encoding: MS949 OS name: "windows 7", version: "6.1", arch: "amd64", family: "windows"
4. 프로젝트 생성
그러면 프로젝트를 생성해 보자. (프로젝트 저장을 위한 sample 디렉토리는 별도로 만들어 놓았다)
C:\Users\viktor\sample>mvn archetype:generate -DgroupId=choi.viktor -DartifactId=mtest1 -Dpackage=choi.viktor.mtest1 -Dversion=1.0-SNAPSHOT (...생략...) 773: remote -> org.apache.servicemix.tooling:servicemix-jsr181-wsdl-first-service-unit (-) 774: remote -> org.apache.servicemix.tooling:servicemix-lwcontainer-service-unit (-) 775: remote -> org.apache.servicemix.tooling:servicemix-mail-service-unit (-) 776: remote -> org.apache.servicemix.tooling:servicemix-ode-service-unit (-) (...생략...) [INFO] Using property: groupId = choi.viktor [INFO] Using property: artifactId = mtest1 [INFO] Using property: version = 1.0-SNAPSHOT [INFO] Using property: package = choi.viktor.mtest1 Confirm properties configuration: groupId: choi.viktor artifactId: mtest1 version: 1.0-SNAPSHOT package: choi.viktor.mtest1 Y: : [INFO] ---------------------------------------------------------------------------- [INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.1 [INFO] ---------------------------------------------------------------------------- [INFO] Parameter: groupId, Value: choi.viktor [INFO] Parameter: packageName, Value: choi.viktor.mtest1 [INFO] Parameter: package, Value: choi.viktor.mtest1 [INFO] Parameter: artifactId, Value: mtest1 [INFO] Parameter: basedir, Value: D:\apache-maven-3.3.9\sample [INFO] Parameter: version, Value: 1.0-SNAPSHOT [INFO] project created from Old (1.x) Archetype in dir: D:\apache-maven-3.3.9\sample\mtest1 [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 01:36 min [INFO] Finished at: 2016-01-05T08:54:47+09:00 [INFO] Final Memory: 17M/224M [INFO] ------------------------------------------------------------------------
생성된 디렉토리 및 파일은 다음과 같다.
- src (디렉토리) \ main, test (하위 디렉토리) \ … \ App.java, AppTest.java (파일)
- pom.xml (파일)
Project Object Model, 즉 POM 설정 파일인 pom.xml은 maven 프로젝트에 대한 설정을 담고 있다. 위에서 만든 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> <groupId>choi.viktor</groupId> <artifactId>mtest1</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>mtest1</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
5. 수정
그러면 메이븐 테스트를 위하여 기본 제공되는 App.java를 수정해 보자.
package choi.viktor.mtest1; import org.apache.commons.lang3.StringUtils; /** * Hello world! * */ public class App { public static void main( String[] args ) { System.out.println(StringUtils.capitalize(" Hello World!")); } }
pom.xml도 수정해 보자. <dependency>를 추가하는 것이 핵심이다.
<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>choi.viktor</groupId> <artifactId>mtest1</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>mtest1</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.4</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
6. 빌드
그럼 이제 빌드해 보자.
D:\apache-maven-3.3.9\sample\mtest1>mvn install [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building mtest1 1.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mtest1 --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory D:\apache-maven-3.3.9\sample\mtest1\src\main\resources [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ mtest1 --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to D:\apache-maven-3.3.9\sample\mtest1\target\classes [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ mtest1 --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory D:\apache-maven-3.3.9\sample\mtest1\src\test\resources [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ mtest1 --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to D:\apache-maven-3.3.9\sample\mtest1\target\test-classes [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ mtest1 --- (...생략...) ------------------------------------------------------- T E S T S ------------------------------------------------------- Running choi.viktor.mtest1.AppTest Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.002 sec Results : Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 (...생략...) [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 18.712 s [INFO] Finished at: 2016-01-05T09:46:59+09:00 [INFO] Final Memory: 15M/121M [INFO] ------------------------------------------------------------------------
빌드를 위하여 mvn install이 정상적으로 완료되면 프로젝트 폴더 내 target 폴더가 생성되고 그 하위에 여러 폴더및 mtest1-1.0-SNAPSHOT.jar 파일이 생성되었음을 알 수 있다. 생성된 jar 파일을 실행해 보자.
D:\apache-maven-3.3.9\sample\mtest1\target>java -cp mtest1-1.0-SNAPSHOT.jar choi.viktor.mtest1.App Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils at choi.viktor.mtest1.App.main(App.java:13) Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang3.StringUtils at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) ... 1 more
하지만 StringUtils 클래스가 포함된 jar 파일을 찾을 수 없는 상태이다. CLASSPATH를 다시 한 번 점검하고 실행해 보자.
D:\apache-maven-3.3.9\sample\mtest1\target>java -cp mtest1-1.0-SNAPSHOT.jar;D:\apache-maven-3.3.9\repository\org\apache\commons\commons-lang3\3.4\commons-lang3-3.4.jar choi.viktor.mtest1.App Hello World!
archetype 플러그인이 아닌 exec 플러그인을 사용하여 실행할 수도 있다.
D:\apache-maven-3.3.9\sample\mtest1>mvn exec:java -Dexec.mainClass=choi.viktor.mtest1.App [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building mtest1 1.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- exec-maven-plugin:1.4.0:java (default-cli) @ mtest1 --- Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-container-default/1.0-alpha-30/plexus-container-default-1.0-alpha-30.pom Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-container-default/1.0-alpha-30/plexus-container-default-1.0-alpha-30.pom (4 KB at 2.8 KB/sec) Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-containers/1.0-alpha-30/plexus-containers-1.0-alpha-30.pom Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-containers/1.0-alpha-30/plexus-containers-1.0-alpha-30.pom (2 KB at 4.7 KB/sec) Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-classworlds/1.2-alpha-9/plexus-classworlds-1.2-alpha-9.pom Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-classworlds/1.2-alpha-9/plexus-classworlds-1.2-alpha-9.pom (4 KB at 8.3 KB/sec) Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.0.20/plexus-utils-3.0.20.pom Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.0.20/plexus-utils-3.0.20.pom (4 KB at 10.8 KB/sec) Downloading: https://repo.maven.apache.org/maven2/org/apache/commons/commons-exec/1.3/commons-exec-1.3.pom Downloaded: https://repo.maven.apache.org/maven2/org/apache/commons/commons-exec/1.3/commons-exec-1.3.pom (11 KB at 28.6 KB/sec) Downloading: https://repo.maven.apache.org/maven2/org/apache/commons/commons-parent/35/commons-parent-35.pom Downloaded: https://repo.maven.apache.org/maven2/org/apache/commons/commons-parent/35/commons-parent-35.pom (57 KB at 80.3 KB/sec) Downloading: https://repo.maven.apache.org/maven2/org/apache/apache/15/apache-15.pom Downloaded: https://repo.maven.apache.org/maven2/org/apache/apache/15/apache-15.pom (15 KB at 39.4 KB/sec) Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.0.20/plexus-utils-3.0.20.jar Downloading: https://repo.maven.apache.org/maven2/org/apache/commons/commons-exec/1.3/commons-exec-1.3.jar Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.0.20/plexus-utils-3.0.20.jar (238 KB at 278.3 KB/sec) Downloaded: https://repo.maven.apache.org/maven2/org/apache/commons/commons-exec/1.3/commons-exec-1.3.jar (54 KB at 51.2 KB/sec) Hello World! [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 6.896 s [INFO] Finished at: 2016-01-05T15:22:09+09:00 [INFO] Final Memory: 14M/156M [INFO] ------------------------------------------------------------------------
7. 버전 변경
pom.xml 파일에서 아래와 같이 <version>을 수정하면 output 파일명 내 버전을 변경할 수 있다.
<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>choi.viktor</groupId> <artifactId>mtest1</artifactId> <version>1.1-SNAPSHOT</version> <packaging>jar</packaging> <name>mtest1</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.4</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
1.1로 변경한 상태에서 빌드하면 target 디렉토리 내에 mtest1-1.1-SNAPSHOT.jar 파일이 생성됨을 알 수 있다.
8. 이클립스 프로젝트 생성
앞서 만든 프로젝트를 이클립스에서 Import 하기 위한 작업이다.
D:\apache-maven-3.3.9\sample\mtest1>mvn eclipse:eclipse [INFO] Scanning for projects... Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-eclipse-plugin/maven-metadata.xml Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-eclipse-plugin/maven-metadata.xml (741 B at 0.6 KB/sec) Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-eclipse-plugin/2.10/maven-eclipse-plugin-2.10.pom Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-eclipse-plugin/2.10/maven-eclipse-plugin-2.10.pom (19 KB at 42.8 KB/sec) (...생략...) [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building mtest1 1.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ (...생략...) [INFO] --- maven-eclipse-plugin:2.10:eclipse (default-cli) @ mtest1 --- (...생략...) [INFO] Using Eclipse Workspace: null [INFO] Adding default classpath container: org.eclipse.jdt.launching.JRE_CONTAINER [INFO] Not writing settings - defaults suffice [INFO] Wrote Eclipse project for "mtest1" to D:\apache-maven-3.3.9\sample\mtest1. [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 16.594 s [INFO] Finished at: 2016-01-05T15:37:28+09:00 [INFO] Final Memory: 14M/156M [INFO] ------------------------------------------------------------------------
그러면 추가로 다음 파일들이 생성된다.
- .classpath
- .project
이제 이클립스에서 File -> Import -> Existing Projects into Workspace하여 프로젝트를 설치(Import)한다.
9. 이클립스에서 모든 설정은...
이클립스 프로젝트와 Maven 연동을 위하여 m2e를 설치한다. Help -> Install New Software에서 Work with에 http://download.eclipse.org/technology/m2e/releases를 입력한 후 Add를 한다.
Maven Integration for WTP도 설치한다. 역시 http://download.eclipse.org/m2e-wtp/releases를 입력한 후 Add를 한다.
이후 이클립스를 재시작한다.
이제 이클립스 상에서 새로운 프로젝트를 생성해 보자. File -> New -> Other -> Maven에서 Maven Project를 선택한다. 역시 main 폴더 하위에 App.java, test 폴더 하위에 AppTest.java 파일이 생성된다.
이클립스 상에서 pom.xml 파일을 다루기 위하여 해당 파일을 클릭하면 Maven POM Editor가 실행된다.