我们在创建项目时要学习的是如何使用 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!