Maven项目的构建与测试

9个月前 (04-27)

在上一节中,我们介绍了如何使用 archetype 创建 Maven 项目,接下来我们介绍如何构建和测试这个项目。

构建项目

查看 helloMaven 项目的 pom.xml 文件,配置如下。

<project xmlns="http://maven.apache网站站点" rel="nofollow" />

mvn clean package


令执行结果如下:

[INFO] Scanning for projects...

[INFO]

[INFO] --------------------< net.biancheng.www:helloMaven >--------------------

[INFO] Building helloMaven 1.0-SNAPSHOT

[INFO] --------------------------------[ jar ]---------------------------------

[INFO]

[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ helloMaven ---

[INFO] Deleting D:\maven\helloMaven\target

[INFO]

[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ helloMaven ---

[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!

[INFO] skip non existing resourceDirectory D:\maven\helloMaven\src\main\resources

[INFO]

[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ helloMaven ---

[INFO] Changes detected - recompiling the module!

[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!

[INFO] Compiling 1 source file to D:\maven\helloMaven\target\classes

[INFO]

[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ helloMaven ---

[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!

[INFO] skip non existing resourceDirectory D:\maven\helloMaven\src\test\resources

[INFO]

[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ helloMaven ---

[INFO] Changes detected - recompiling the module!

[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!

[INFO] Compiling 1 source file to D:\maven\helloMaven\target\test-classes

[INFO]

[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ helloMaven ---

[INFO] Surefire report directory: D:\maven\helloMaven\target\surefire-reports

Downloading from central: https://repo.maven.apache网站站点" rel="nofollow" /> maven jar

图1:target 目录


说明:

  • Maven 令中包含了两个令:clean 和 package,其中 clean 负责清理 target 目录,package 负责将项目构建并打包输出为 jar 文件。

  • classes:源代码编译后存放在该目录中。

  • test-classes:测试源代码编译后并存放在该目录中。

  • surefire-reports:Maven 运行测试用例生成的测试报告存放在该目录中。

  • helloMaven-1.0-SNAPSHOT.jar:Maven 对项目进行打包生成的 jar 文件。


打开令控制台,跳转到 D:\maven\helloMaven\target\classes 目录,执行 Java 令

java net.biancheng.www.App


执行结果如下。

Hello World!

测试项目

下面我们介绍如何在 Maven 项目中添加其他的 Java 文件,并进行测试。

打开 D:\maven\helloMaven\src\main\java\net\biancheng\www 文件夹,在其中创建一个名为  Util.java 的文件。

package net.biancheng.www;

/**

* Maven 项目中添加的自定义java类

*

* @author 编程帮 www.biancheng网站站点" rel="nofollow" />

package net.biancheng.www;

/**

* @author 编程帮 www.biancheng网站站点" rel="nofollow" />

mvn clean compile


令执行结果如下。

[INFO] Scanning for projects...

[INFO]

[INFO] --------------------< net.biancheng.www:helloMaven >--------------------

[INFO] Building helloMaven 1.0-SNAPSHOT

[INFO] --------------------------------[ jar ]---------------------------------

[INFO]

[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ helloMaven ---

[INFO] Deleting D:\maven\helloMaven\target

[INFO]

[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ helloMaven ---

[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!

[INFO] skip non existing resourceDirectory D:\maven\helloMaven\src\main\resources

[INFO]

[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ helloMaven ---

[INFO] Changes detected - recompiling the module!

[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!

[INFO] Compiling 2 source files to D:\maven\helloMaven\target\classes

[INFO] ------------------------------------------------------------------------

[INFO] BUILD SUCCESS

[INFO] ------------------------------------------------------------------------

[INFO] Total time: 0.853 s

[INFO] Finished at: 2021-03-03T09:26:29+08:00

[INFO] ------------------------------------------------------------------------


编译成功后,跳转到 D:\maven\helloMaven\target\classes 目录下,执行以下 Java 令。

java net.biancheng.www.App


执行结果如下。

编程帮欢迎您,网址:www.biancheng网站站点" rel="nofollow" />