Maven - Build & Test Project
<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.companyname.projectgroup</groupId> <artifactId>project</artifactId> <version>1.0</version> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> </dependency> </dependencies> </project>
C:\MVN\consumerBanking>mvn clean package
[INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building consumerBanking 1.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ consumerBanking --- [INFO] Deleting C:\MVN\consumerBanking\target [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ consumerBanking --- [WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,i.e. build is platform dependent! [INFO] skip non existing resourceDirectory C:\MVN\consumerBanking\src\main\resources [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ consumerBanking --- [INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent! [INFO] Compiling 1 source file to C:\MVN\consumerBanking\target\classes [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ consumerBanking --- [WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory C:\MVN\consumerBanking\src\test\resources [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ consumerBanking --- [INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. b uild is platform dependent! [INFO] Compiling 1 source file to C:\MVN\consumerBanking\target\test-classes [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ consumerBanking --- [INFO] Surefire report directory: C:\MVN\consumerBanking\target\surefire-reports ------------------------------------------------------- T E S T S ------------------------------------------------------- Running com.companyname.bank.AppTest Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.063 sec Results : Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ consumerBanking --- [INFO] Building jar: C:\MVN\consumerBanking\target\consumerBanking-1.0-SNAPSHOT.jar [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 14.406 s [INFO] Finished at: 2015-09-27T17:58:06+05:30 [INFO] Final Memory: 14M/247M [INFO] ------------------------------------------------------------------------
프로젝트가 빌드되고 최종 jar파일이 생성된다. 아래는 배우고 있는 주요 개념들이다.
We give maven two goals, first to clean the target directory (clean) and then package the project build output as jar(package). (Maven에게 두가지 목표를 주었다. 처음은 target 디렉토리를 정리하는 것이고 그다음 프로젝트가 jar로써 출력출력하는 build package이다.)
Packaged jar is available in consumerBanking\target folder as consumerBanking-1.0-SNAPSHOT.jar. (jar package는 conumerBanking-1.0-SNAPSHOT.jar로써 consumerBanking/target폴더에서 가능하다.)
Test reports are available in consumerBanking\target\surefire-reports folder. (테스트 레보트는 consumerBanking/target/surefire-reports폴더에서 가능하다.)
Maven compiled source code file(s) and then test source code file(s). (Maven은 소스코드 파일을 컴파일하고 소스코드 파일을 테스트한다.)
Then Maven run the test cases. (그리고 Maven은 테스트 케이스를 실행한다.)
Finally Maven created the package. (최종적으로 Maven은 패키지를 만든다.)
이제 콘솔을 열고 C:\MVN\consumerBanking\target\classes디렉토리로 가서 아래 java명령을 실행한다.
C:\MVN\consumerBanking\target\classes>java com.companyname.bank.App
아래와 같은 결과가 보일 것이다.
Hello World!
Adding Java Source Files
어떻게 프로젝트에 추가적인 java파일을 넣을수 있는지 보자. C:\MVN\consumerBanking\src\main\java\com\companyname\bank폴더를 열고 Util.java로 폴더안에 Util 클래스를 만들자.
package com.companyname.bank; public class Util { public static void printMessage(String message){ System.out.println(message); } }
App 클래스를 Util클래스를 사용하는 것으로 갱신하자.
package com.companyname.bank; /** * Hello world! * */ public class App { public static void main( String[] args ) { Util.printMessage("Hello World!"); } }
이제 콘솔을 열고 C:\MVN\consumerBanking디렉토리로 가서 아래 mvn명령을 실행한다.
C:\MVN\consumerBanking>mvn clean compile
Maven이 성공적으로 build한 후 C:\MVN\consumerBanking\target\classes 디렉토리로 가서 아래 java명령을 실행한다.
C:\MVN\consumerBanking\target\classes>java -cp com.companyname.bank.App
아래 결과를 보게 될 것이다.
Hello World!