JAVA

maven

aucd29 2013. 9. 26. 21:05
1. http://maven.apache.org/
선행작업해야할 것이 JAVA_HOME 설정 그리고 Path 에 %JAVA_HOME%/bin 설정 (javac 을 위해서) 과 maven path 설정(mvn 을 위해서) 을 해줘야 한다.
Download link : http://ftp.daum.net/apache//maven/binaries/apache-maven-3.0.3-bin.zip

http://search.maven.org // libraries url

///////////////////////////////////////////////////////////

- make project
mvn archetype:generate

Choose a number: 109: // quick start project
Define value for property 'groupId': : // same dynamic library

groupId / actifactId / version // 순서 이리되어 있단


maven 이 libaray 구분을 위해서 3개를 사용하는데

1). groudId    그룹
2). artifactId    이름
3). version    버전

////////////////////////////////////////////////////////////

mvn compile
mvn clean
mvn clean package
mvn javadoc:javadoc
mvn site

// run
java -cp . net.minigate.kurome.App

/////////////////////////////////////////////////////////////////////////////

http://maven.apache.org/pom.html
http://maven.apache.org/plugins/maven-assembly-plugin/
http://maven.apache.org/plugins/maven-assembly-plugin/usage.html
http://repo2.maven.org/maven2/
http://mvnrepository.com/
maven document pdf link : http://maven.apache.org/apache-maven.pdf

////////////////////////////////////////////////////////////////////////////////

getting started in 5-min : http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html
getting started in 30-min : http://maven.apache.org/guides/getting-started/index.html

maven 을 사용하는 이유???
이젠 젠장할 eclipse 가 너무 잘만들어 졌잖아? 는 농이고 Dependency 관리가 너무 훌륭~

////////////////////////////////////////////////////////////////////////////////////////////////////

>> manual for korean
http://www.javajigi.net/display/IDE/Maven

maven 의 로컬 리파지토리는 USER_HOME/.m2/repository 이다. xp 의 경우 C:\Documents and Settings\사용자아이디\.m2\repository 임..

로컬 리파지토리 변경 : <localRepository>D:/Repositories/MavenRepository</localRepository>

라이브러리 스코프

compile : scope를 설정할지 않았을 때의 디폴트 scope이다. 컴파일 시에도 사용되며, 배포시에도 같이 배포되어야 하는 라이브러리이다.
provided : JDK가 컨테이너에 의하여 제공되는 라이브러리이다. 예를 들어 servlet.jar의 경우 Servlet 컨테이너에 의하여 제공되기 때문에 이 scope를 사용한다.
runtime : 이 scope는 말 그대로 컴파일 시에는 사용되지 않지만 애플리케이션을 실행할 때 사용되는 라이브러리일 경우 설정한다.
test : 테스트를 위해서만 사용하는 라이브러리이다.
system : 이 scope는 provided와 비슷하다. 단지 우리가 직접 jar 파일을 제공해야 한다. 따라서 이 scope의 jar 파일은 repository에서 관리되지 않을 수도 있다.


/////////////////////////////////////////////////////////////////////////////////////////////////////

하나를 알려달라고 했는데 열을 알려주시고 유유히 가시는 권책임님 ㄷㄷㄷㄷㄷ 무섭단 ㄷㄷㄷ


static 으로 빌드하기 위해선 다음처럼 한다. 아래 페이지에서 Assembly Directories 란을 보면
http://maven.apache.org/plugins/maven-assembly-plugin/usage.html

<project>
[...]
<build>
    [...]
    <plugins>
     <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.2-beta-5</version>
        <configuration>
         <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
         </descriptorRefs>
        </configuration>
        <executions>
         <execution>
            <id>make-assembly</id> <!-- this is used for inheritance merges -->
            <phase>package</phase> <!-- append to the packaging phase. -->
            <goals>
             <goal>single-directory</goal> <!-- goals == mojos -->
            </goals>
         </execution>
        </executions>
     </plugin>
     [...]
</project>

요런 내용이 있는데 pom.xml 에 붙여 넣자. 이리되면 dataManager-ubuntu.jar-jar-with-dependencies.jar 식으로 생성된다.

실행방법은 아래와 같다.

[code]java -jar targetfile-jar-with-dependencies.jar[/code]

//////////////////////////////////////////////////////////////////////////////////////////////

필터링이라는 게 있는데

<resource>
    <directory>src/main/resources</directory>
    <filtering>true</filtering>
</resource>

이런식으로 리소스 위치와 필터링에 대한 옵션을 활성화 시키고


<properties>
    <a>abcde</a>
</properties>
를 이용해 리소스에 값을 프로젝트 파일에서 변경해서 적용할 수 있다고 함

타겟이 되는 파일은 ${a} 식으로 데이터를 입력하면 abcde 가 입력됨..


/////////////////////////////////////////////////////////////////

프로파일이라고 빌드 환경에 변화를 줄 수 있는 놈이 있는데

<profiles>
    <profile>
        <id>ubuntu</id>
        <build>
            <finalName>${project.artifactId}-ubuntu.jar</finalName>
        </build>
        <properties>
            <a>ubuntu</a>
        </properties>
    </profile>
</profiles>

이런식으로 id를 부여하고 빌드될 파일명 등을 입력해 사용할 수 있음~

생성 방법은 아래처럼 하면 됨

[code] mvn -P ubuntu clean package [/code]


/////////////////////////////////////////////////////////////////


완성된 내용


<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>net.minigate</groupId>
    <artifactId>dataManager</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>${project.artifactId}</name>
    <url>http://maven.apache.org</url>
    <dependencies>
    
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>net.sarangnamu</groupId>
            <artifactId>basicApi</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>

    </dependencies>

    <developers>
        <developer>
            <id>kurome</id>
            <name>kurome</name>
            <email>cdchoi@minigate.net</email>
            <organization>Minigate</organization>
            <organizationUrl>http://www.minigate.net</organizationUrl>
            <roles>
                <role>Mobile</role>
                <role>developer</role>
            </roles>
            <timezone>+9</timezone>
        </developer>
    </developers>


    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.0.2</version>
                <configuration>
                    <source>1.5</source>
                    <target>1.5</target>
                </configuration>
            </plugin>

            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.2-beta-5</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>net.minigate.datamanager.App</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id> <!-- this is used for inheritance merges -->
                        <phase>package</phase> <!-- append to the packaging phase. -->
                        <goals>
                            <goal>single</goal> <!-- goals == mojos -->
                        </goals>
                    </execution>
                </executions>
            </plugin>

        </plugins>
        <resources>
            <resource>
                <directory>./src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>

    <properties>
        <a>abcde</a>
    </properties>

    <profiles>
        <profile>
            <id>ubuntu</id>
            <build>
                <finalName>${project.artifactId}-ubuntu.jar</finalName>
            </build>
            <properties>
                <a>ubuntu</a>
            </properties>
        </profile>
    </profiles>

</project>



mvn -P ubuntu clean package



maven definitive guide
http://www.sonatype.com/book
http://www.sonatype.com/books/mvnref-book/reference/public-book.html