使用Maven構建和測試Java項目

我們在創建項目時要學習的是如何使用 Maven 來創建一個 Java 應用程序。現在將學習如何構建和測試應用程序。

進入到 C:\MVN 目錄我們準備創建來 java應用程序。打開 consumerBanking 文件夾。看到 pom.xml 文件並打開它,內容如下。


4.0.0
com.companyname.projectgroup
project
1.0


junit
junit
3.8.1


在這裏,你可以看到 Maven 已經添加了 Junit 測試框架。默認情況下 Maven 增加了一個源文件 App.java,以及在前面的章節中討論的一樣,默認目錄結構中也有一個測試文件:AppTest.java。

現在打開命令控制檯,進入到 C:\MVN\consumerBanking 目錄並執行以下命令 mvn 命令。

C:\MVN\consumerBanking>mvn clean package

Maven 將開始構建這個項目,如下所示:

[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: 2016-09-27T17:58:06+05:30
[INFO] Final Memory: 14M/247M
[INFO] ------------------------------------------------------------------------

現在我們已經建立項目,並創建生成最終 jar 文件,以下是主要一些概念介紹:

  • 我們給 maven 兩個目標,首先要清理目標目錄(clean),然後打包項目生成 JAR(包)輸出 ;

  • 打包的 JAR 可在 consumerBanking\target 文件夾找到文件:consumerBanking-1.0-SNAPSHOT.jar ;

  • 測試報告在 consumerBanking\target\surefire-reports 文件夾中;

  • Maven 編譯的源代碼文件,然後測試源代碼文件;

  • 然後 Maven 運行測試用例;

  • 最後 Maven 創建包;

現在打開命令控制檯,進入到 C:\MVN\consumerBanking\target\classes 目錄,然後執行以下 java 命令。 

C:\MVN\consumerBanking\target\classes>java com.companyname.bank.App

會看到結果,如下所示:

Hello World!

添加 Java 源文件

讓我們來看看如何在項目中添加額外的 Java 文件。打開 C:\MVN\consumerBanking\src\main\java\com\companyname\bank 文件夾,在其中創建一個 Util 類,其對應文件爲:Util.java 文件。 

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 構建成功後,請再進入 C:\MVN\consumerBanking\target\classes  目錄,然後執行以下 java 命令。

C:\MVN\consumerBanking\target\classes>java -cp com.companyname.bank.App

看到結果如下所示:

Hello World!