Maven - 構建和測試專案



我們在專案建立章節中學習瞭如何使用 Maven 建立 Java 應用程式。現在我們將學習如何構建和測試該應用程式。

轉到 C:/MVN 目錄,您已在其中建立了 Java 應用程式。開啟 **consumerBanking** 資料夾。您將看到包含以下內容的 **POM.xml** 檔案。更新它以反映當前的 Java 版本。

<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>
   <properties>
      <maven.compiler.source>11</maven.compiler.source>
      <maven.compiler.target>11</maven.compiler.target>
   </properties>
   <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 將開始構建專案。

C:\MVN\consumerBanking>mvn clean package
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------< com.companyname.bank:consumerBanking >----------------
[INFO] Building consumerBanking 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[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. build 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.028 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:  4.663 s
[INFO] Finished at: 2021-12-13T17:34:27+05:30
[INFO] ------------------------------------------------------------------------

C:\MVN\consumerBanking>

您已經構建了專案並建立了最終的 jar 檔案,以下是關鍵學習概念:

  • 我們給 Maven 提供了兩個目標,首先是清理 target 目錄 (clean),然後將專案構建輸出打包為 jar (package)。

  • 打包後的 jar 檔案位於 consumerBanking\target 資料夾中,名為 consumerBanking-1.0-SNAPSHOT.jar。

  • 測試報告位於 consumerBanking\target\surefire-reports 資料夾中。

  • Maven 編譯原始碼檔案,然後測試原始碼檔案。

  • 然後 Maven 執行測試用例。

  • 最後,Maven 建立包。

現在開啟命令控制檯,轉到 C:\MVN\consumerBanking\target\classes 目錄並執行以下 java 命令。

>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** 命令。

>mvn clean compile

Maven 構建成功後,轉到 C:\MVN\consumerBanking\target\classes 目錄並執行以下 java 命令。

>java -cp com.companyname.bank.App

您將看到如下結果:

Hello World!
廣告