2013년 8월 16일 금요일

Maven 설치 및 샘플 Java 빌드하기

1. http://maven.apache.org에서 Maven을 다운로드하고 설치합니다.


-> apache-maven-3.1.0-bin.zip를 다운로드하고 C:\에 압축을 해제하면 C:\apache-maven-3.1.0 폴더가 만들어 집니다.

2. 환경 변수 M2_HOME=C:\apache-maven-3.1.0를 설정합니다.


->

3. 환경 변수 path에 C:\apache-maven-3.1.0\bin을 추가합니다.


->

4. 커맨드 창에서 mvn --version을 실행해서 설치 및 설정을 확인합니다.


->

5. 이제 인터넷이 연결되어 있는 상태에서 샘플 프로젝트 구조를 만들어 봅니다.


(1) "myapp" 폴더를 작성하고 해당 폴더로 이동합니다.


-> C:\Users\kdyoung>mkdir myapp

-> C:\Users\kdyoung>cd myapp

(2) 다음으로 mvn archetype:generate를 호출하여 프로젝트 구조를 작성합니다.


-> C:\Users\kdyoung\myapp>mvn archetype:generate

821: remote -> pro.savant.circumflex:webapp-archetype (-)
822: remote -> ru.circumflex:circumflex-archetype (-)
823: remote -> ru.nikitav.android.archetypes:release (-)
824: remote -> ru.nikitav.android.archetypes:release-robolectric (-)
825: remote -> se.vgregion.javg.maven.archetypes:javg-minimal-archetype (-)
826: remote -> sk.seges.sesam:sesam-annotation-archetype (-)
827: remote -> tk.skuro:clojure-maven-archetype (A simple Maven archetype for Clojure)
828: remote -> uk.ac.rdg.resc:edal-ncwms-based-webapp (-)

-> Choose a number or apply filter (format: [groupId:]artifactId, case sensitive co
ntains): 294: 엔터키 

디폴트 294 Archetype를 기초로 선택하여 프로젝트 구조를 작성합니다.

Choose org.apache.maven.archetypes:maven-archetype-quickstart version:
1: 1.0-alpha-1
2: 1.0-alpha-2
3: 1.0-alpha-3
4: 1.0-alpha-4
5: 1.0
6: 1.1
-> Choose a number: 6: 엔터키

디폴트 6을 선택하여 최신 버전을 사용합니다.

Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/archetypes/maven-archetype-quickstart/1.1/maven-archetype-quickstart-1.1.jar
Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/archetypes/maven-archetype-quickstart/1.1/maven-archetype-quickstart-1.1.jar (7 KB at 7.5 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/archetypes/maven-archetype-quickstart/1.1/maven-archetype-quickstart-1.1.pom
Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/archetypes/maven-archetype-quickstart/1.1/maven-archetype-quickstart-1.1.pom (2 KB at 5.7 KB/sec)

-> Define value for property 'groupId': : org.samples.java

Group Id로 사용할 임의의 문자열을 입력합니다.

-> Define value for property 'artifactId': : MyMavenTest

Artifact Id로 사용할 임의의 문자열을 입력합니다.

-> Define value for property 'version':  1.0-SNAPSHOT: : 엔터키

디폴트 값을 Version 문자열로 사용합니다.

-> Define value for property 'package':  org.samples.java: : 엔터키

Package로 사용할 문자열로 Artifact Id 문자열을 사용합니다.

Confirm properties configuration:
groupId: org.samples.java
artifactId: MyMavenTest
version: 1.0-SNAPSHOT
package: org.samples.java
-> Y: : 엔터키

마지막으로 확인을 합니다.

[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype:
 maven-archetype-quickstart:1.1
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: org.samples.java
[INFO] Parameter: packageName, Value: org.samples.java
[INFO] Parameter: package, Value: org.samples.java
[INFO] Parameter: artifactId, Value: MyMavenTest
[INFO] Parameter: basedir, Value: C:\Users\kdyoung\myapp
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: C:\Users\kdyoung\myapp\MyMavenTest
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 16:58.392s
[INFO] Finished at: Fri Aug 16 15:55:56 KST 2013
[INFO] Final Memory: 8M/25M
[INFO] ------------------------------------------------------------------------



6. 프로젝트 구조를 확인합니다.


-> C:\Users\kdyoung\myapp>dir

2013-08-16  오후 03:55    <DIR>          MyMavenTest

-> C:\Users\kdyoung\myapp>cd MyMavenTest

-> C:\Users\kdyoung\myapp\MyMavenTest>dir

2013-08-16  오후 03:55               785 pom.xml
2013-08-16  오후 03:55    <DIR>          src

7. 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>org.samples.java</groupId>
  <artifactId>MyMavenTest</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>MyMavenTest</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>
---------------------------------------------------------------------------------------------------------

8. src 폴더 내용을 확인합니다.


-> C:\Users\kdyoung\myapp\MyMavenTest>cd src

-> C:\Users\kdyoung\myapp\MyMavenTest\src>dir

2013-08-16  오후 03:55    <DIR>          main
2013-08-16  오후 03:55    <DIR>          test

살펴보면 두개의 파일이 작성되어 있습니다.
(1) C:\Users\kdyoung\myapp\MyMavenTest\src\main\java\org\samples\java\App.java
---------------------------------------------------------------------------------------------------------
package org.samples.java;

public class App 
{
    public static void main( String[] args )
    {        System.out.println( "Hello World!" );    }
}
---------------------------------------------------------------------------------------------------------
(2) C:\Users\kdyoung\myapp\MyMavenTest\src\test\java\org\samples\java\AppTest.java
---------------------------------------------------------------------------------------------------------
package org.samples.java;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

public class AppTest extends TestCase
{
    public AppTest( String testName )
    {        super( testName );    }

    public static Test suite()
    {        return new TestSuite( AppTest.class );    }

    public void testApp()
    {        assertTrue( true );    }
}
---------------------------------------------------------------------------------------------------------

9. pom.xml이 있는 폴더에서 컴파일을 합니다.


-> C:\Users\kdyoung\myapp\MyMavenTest>mvn compile

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building MyMavenTest 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-resources-plugin/2.6/maven-resources-plugin-2.6.pom
중략
Downloaded: http://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-compiler-api/1.9.1/plexus-compiler-api-1.9.1.jar (21 KB at 22.9 KB/sec)
[INFO] Compiling 1 source file to C:\Users\kdyoung\myapp\MyMavenTest\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 11.028s
[INFO] Finished at: Fri Aug 16 16:22:27 KST 2013
[INFO] Final Memory: 6M/16M
[INFO] ------------------------------------------------------------------------

10. pom.xml이 있는 폴더에서 패키징을 합니다.


-> C:\Users\kdyoung\myapp\MyMavenTest>mvn package

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building MyMavenTest 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-surefire-plugin/2.12.4/maven-surefire-plugin-2.12.4.pom
중략
Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-junit3/2.12.4/surefire-junit3-2.12.4.jar (26 KB at 63.6 KB/sec)

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running org.samples.java.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.003 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ MyMavenTest ---
Downloading: http://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-arch
iver/2.1/plexus-archiver-2.1.jar
중략
Downloaded: http://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-archi
ver/2.1/plexus-archiver-2.1.jar (181 KB at 235.8 KB/sec)
[INFO] Building jar: C:\Users\kdyoung\myapp\MyMavenTest\target\MyMavenTest-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 14.747s
[INFO] Finished at: Fri Aug 16 16:26:35 KST 2013
[INFO] Final Memory: 7M/19M
[INFO] ------------------------------------------------------------------------


11. 생성된 패키지를 실행하여 결과를 확인합니다.


-> C:\Users\kdyoung\myapp\MyMavenTest>java -cp target\MyMavenTest-1.0-SNAPSHOT.jar org.samples.java.App

Hello World!

12. (옵션) 빌드된 패키지를 로컬 리파지토리에 설치합니다.


-> C:\Users\kdyoung\myapp\MyMavenTest>mvn install

[INFO] --- maven-install-plugin:2.4:install (default-install) @ MyMavenTest ---
[INFO] Installing C:\Users\kdyoung\myapp\MyMavenTest\target\MyMavenTest-1.0-SNAPSHOT.jar to C:\Users\kdyoung\.m2\repository\org\samples\java\MyMavenTest\1.0-SNAPSHOT\MyMavenTest-1.0-SNAPSHOT.jar
[INFO] Installing C:\Users\kdyoung\myapp\MyMavenTest\pom.xml to C:\Users\kdyoung\.m2\repository\org\samples\java\MyMavenTest\1.0-SNAPSHOT\MyMavenTest-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.943s
[INFO] Finished at: Fri Aug 16 17:25:05 KST 2013
[INFO] Final Memory: 5M/15M
[INFO] ------------------------------------------------------------------------


13. Maven 빌드를 수행하고 나면 target 폴더가 생성됩니다.


-> C:\Users\kdyoung\myapp\MyMavenTest>dir

2013-08-16  오후 03:55               785 pom.xml
2013-08-16  오후 03:55    <DIR>          src
2013-08-16  오후 05:25    <DIR>          target


14. (옵션) 생성된 target 폴더를 다음과 같이 삭제할 수 있습니다.


-> C:\Users\kdyoung\myapp\MyMavenTest>mvn clean
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building MyMavenTest 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ MyMavenTest ---
[INFO] Deleting C:\Users\kdyoung\myapp\MyMavenTest\target
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.364s
[INFO] Finished at: Fri Aug 16 17:57:09 KST 2013
[INFO] Final Memory: 2M/15M
[INFO] ------------------------------------------------------------------------

-> C:\Users\kdyoung\myapp\MyMavenTest>dir

2013-08-16  오후 03:55               785 pom.xml
2013-08-16  오후 03:55    <DIR>          src

99. 결론적으로 Maven은 편리한 프로젝트 및 빌드 구조를 만들어 줍니다.





참고: http://www.youtube.com/playlist?list=PLMUNVW3VsMWWbU6HExbztVskPH6lhpt-J
...

댓글 없음:

댓글 쓰기