|  |  |
|
POM 커스터마이징
메이븐 2는 pom.xml을 통해 프로젝트에 대해 알게 된다. NumOps를 위해 Archetype을 사용하여 생성한 파일을 Listing 8에서 보여준다.
Listing 8. Archetype이 생성한 POM - 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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ibm.devworks</groupId>
<artifactId>NumOps</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>Maven Quick Start Archetype</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
|
Archetype이 모듈의 이름을 어떻게 정의했는지 주의해 살펴보기 바란다. 타입을 JAR 파일로 정의했으며 Junit의 종속성은 <scope> 태그를 사용하여 test 단계로 제한했다. 새 프로젝트에 맞게 pom.xml 파일을 바꾸려면 Listing 9에 있는 것처럼 약간 수정한다.
Listing 9. NumOps 프로젝트를 위해 생성된 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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ibm.devworks</groupId>
<artifactId>NumOps</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>Intro to Maven 2 Example 1</name>
<url>http://www.ibm.com/java</url>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
|
소스와 타깃 자바 코드 레벨을 재정의하려면 <build> 태그가 추가로 필요하다. 기본적으로 JDK 1.4가 사용되지만 위의 코드는 제너릭을 사용하므로 JDK 5.0 컴파일이 필요하다.
커스터마이징한 프로젝트 컴파일하기
이제 NumOps 프로젝트를 mvn compile 명령을 사용하여 컴파일할 수 있다. 이 명령을 실행하면 메이븐 2 엔진이 빌드 생명주기를 컴파일 단계까지 관련된 모조를 실행하며 진행시킨다. 세 개의 클래스 파일을 타깃 트리(Listing 10에 보이는)에 생성한 빌드 성공 보고서를 볼 수 있을 것이다. 처음으로 실행하면 관련된 종속성에 따라 인터넷을 통해 중앙 저장소에서 다운로드가 필요할 수 있기 때문에 다소 시간이 걸릴 수 있다.
Listing 10. NumOps 프로젝트에서 mvn compile 실행 결과 출력
[INFO] Scanning for projects...
[INFO] -------------------------------------------------------------------------
---
[INFO] Building Intro to Maven 2 Example 1
[INFO] task-segment: [compile]
[INFO] -------------------------------------------------------------------------
---
[INFO] [resources:resources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:compile]
Compiling 3 source files to C:\temp\maven\NumOps\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1 second
[INFO] Finished at: Sat Dec 02 22:52:16 EST 2006
[INFO] Final Memory: 3M/7M
[INFO] ------------------------------------------------------------------------
|
단위 테스트 추가하기
좋은 개발은 모든 코드 모듈에 대한 단위 테스트를 수행한다. 메이븐 2는 단위 테스트를 위해 AppTest.java를 만들어 준다. 이 파일을 NumOpsTest.java로 이름을 변경하고 단위 테스트를 위해 Listing 11에 보이는 것과 같이 변경한다. 단위 테스트 소스 코드(다운로드를 참조한다)를 받아 복사해도 된다.
Listing 11. NumOpsTest 단위 테스트를 프로젝트에 추가
package com.ibm.devworks;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit test for simple App.
*/
public class NumOpsTest
extends TestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public NumOpsTest( String testName )
{
super( testName );
}
...
public void testNumOps()
{
NumOps nops = new NumOps();
assertTrue( nops.size() == 1);
assertTrue(
nops.getOp(0).getDesc().equals("plus"));
assertTrue( nops.getOp(0).op(2,1) == 3);
}
}
|
이제 mvn test 명령을 사용하여 테스트 단계까지 관련된 모조를 모두 실행할 수 있다.
메이븐 2는 소스와 단위 테스트 소스 코드를 컴파일한다. 그 뒤에 테스트를 실행하고 성공, 실패, 에러 숫자를 Listing 12처럼 보여준다.
Listing 12. mvn test를 실행해 프로젝트를 컴파일하고 단위 테스트를 실행
[INFO] Scanning for projects...
[INFO] -------------------------------------------------------------------------
---
[INFO] Building Intro to Maven 2 Example 1
[INFO] task-segment: [test]
[INFO] -------------------------------------------------------------------------
---
[INFO] [resources:resources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:compile]
[INFO] Nothing to compile - all classes are up to date
[INFO] [resources:testResources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:testCompile]
Compiling 1 source file to C:\temp\maven\NumOps\target\test-classes
[INFO] [surefire:test]
[INFO] Surefire report directory: C:\temp\maven\NumOps\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.ibm.devworks.NumOpsTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.031 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2 seconds
[INFO] Finished at: Sat Dec 02 23:04:27 EST 2006
[INFO] Final Memory: 3M/6M
[INFO] ------------------------------------------------------------------------
|
 |

|
|  |
|