使用Maven構建和測試Java專案


我們在建立專案時要學習的是如何使用 Maven 來建立一個 Java 應用程式。現在將學習如何構建和測試應用程式。

進入到 C:\MVN 目錄我們準備建立來 java應用程式。開啟 consumerBanking 檔案夾。看到 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>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>

在這裡,你可以看到 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!