Maven项目之构建与测试

下文笔者讲述maven项目的生成和测试的方法分享,如下所示
本示例基于上一章创建的maven项目进行演示

maven项目的构建和测试实现思路

构建maven项目
   mvn clean package 
clean:
   清理项目
package:
   构建项目包
例:

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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.linux28.www</groupId>
  <artifactId>testMaven</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>testMaven</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

构建maven项目


D:\tmp\testMaven>mvn clean package
[WARNING]
[WARNING] Some problems were encountered while building the effective settings
[WARNING] Unrecognised tag: 'distributionManagement' (position: START_TAG seen ...<!-- \u5b9a\u4e49snapshots\u5e93\u548creleases\u5e93\u7684nexus\u5730\u5740 -->\r\n<distributionManagement>... @79:25)  @ D:\Tool\apache-maven-3.8.3\conf\settings.xml, line 79, column 25
[WARNING]
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------------< com.linux28.www:testMaven >----------------------
[INFO] Building testMaven 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ testMaven ---
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ testMaven ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\tmp\testMaven\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ testMaven ---
[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:\tmp\testMaven\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ testMaven ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\tmp\testMaven\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ testMaven ---
[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:\tmp\testMaven\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ testMaven ---
[INFO] Surefire report directory: D:\tmp\testMaven\target\surefire-reports
Downloading from maven-public: https://alibaba:8087/repository/maven-public/org/apache/maven/surefire/surefire-junit3/2.12.4/surefire-junit3-2.12.4.pom
Downloaded from maven-public: https://alibaba:8087/repository/maven-public/org/apache/maven/surefire/surefire-junit3/2.12.4/surefire-junit3-2.12.4.pom (1.7 kB at 1.6 kB/s)
Downloading from maven-public: https://alibaba:8087/repository/maven-public/org/apache/maven/surefire/surefire-providers/2.12.4/surefire-providers-2.12.4.pom
Downloaded from maven-public: https://alibaba:8087/repository/maven-public/org/apache/maven/surefire/surefire-providers/2.12.4/surefire-providers-2.12.4.pom (2.3 kB at 10 kB/s)
Downloading from maven-public: https://alibaba:8087/repository/maven-public/org/apache/maven/surefire/surefire-junit3/2.12.4/surefire-junit3-2.12.4.jar
Downloaded from maven-public: https://alibaba:8087/repository/maven-public/org/apache/maven/surefire/surefire-junit3/2.12.4/surefire-junit3-2.12.4.jar (26 kB at 56 kB/s)

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.linux28.www.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.005 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ testMaven ---
[INFO] Building jar: D:\tmp\testMaven\target\testMaven-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  4.118 s
[INFO] Finished at: 2023-02-16T15:41:13+08:00
[INFO] ---------------------------------------
运行完毕后,可发现项目目录下多了一个target目录
maven编译后的效果

target目录简介

 
classes:源代码编译后存放在该目录中。
test-classes:测试源代码编译后并存放在该目录中。
surefire-reports:Maven 运行测试用例生成的测试报告存放在该目录中。
testMaven-1.0-SNAPSHOT.jar:Maven 对项目进行打包生成的 jar 文件。

运行生成后的jar文件

D:\tmp\testMaven\target\classes>java com.linux28.www.App
Hello World!