课程表

Maven课程

工具箱
速查手册

Maven 构建和测试工程

当前位置:免费教程 » 软件/图像 » Maven

我们在创建工程章节中学到的是如何使用 Maven 创建 Java 应用。现在我们将看到如何构建和测试这个应用。

跳转到 C:/MVN 目录下,既你的 java 应用目录下。打开 consumerBanking 文件夹。你将看到 POM.xml 文件中有下面的内容。

  1. <project xmlns="http://maven.apache.org/POM/4.0.0"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
  4. http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>com.companyname.projectgroup</groupId>
  7. <artifactId>project</artifactId>
  8. <version>1.0</version>
  9. <dependencies>
  10. <dependency>
  11. <groupId>junit</groupId>
  12. <artifactId>junit</artifactId>
  13. <version>3.8.1</version>
  14. </dependency>
  15. </dependencies>
  16. </project>

可以看到,Maven 已经添加了 JUnit 作为测试框架。默认 Maven 添加了一个源码文件 App.java 和一个测试文件 AppTest.java 到上个章节中我们提到的默认目录结构中。

打开命令控制台,跳转到 C:\MVN\consumerBanking 目录下,并执行以下 mvn 命令。

  1. C:\MVN\consumerBanking>mvn clean package

Maven 将开始构建工程。

  1. [INFO] Scanning for projects...
  2. [INFO] -------------------------------------------------------------------
  3. [INFO] Building consumerBanking
  4. [INFO] task-segment: [clean, package]
  5. [INFO] -------------------------------------------------------------------
  6. [INFO] [clean:clean {execution: default-clean}]
  7. [INFO] Deleting directory C:\MVN\consumerBanking\target
  8. [INFO] [resources:resources {execution: default-resources}]
  9. [WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,
  10. i.e. build is platform dependent!
  11. [INFO] skip non existing resourceDirectory C:\MVN\consumerBanking\src\mainresources
  12. [INFO] [compiler:compile {execution: default-compile}]
  13. [INFO] Compiling 1 source file to C:\MVN\consumerBanking\target\classes
  14. [INFO] [resources:testResources {execution: default-testResources}]
  15. [WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,
  16. i.e. build is platform dependent!
  17. [INFO] skip non existing resourceDirectory C:\MVN\consumerBanking\src\testresources
  18. [INFO] [compiler:testCompile {execution: default-testCompile}]
  19. [INFO] Compiling 1 source file to C:\MVN\consumerBanking\target\test-classes
  20. [INFO] [surefire:test {execution: default-test}]
  21. [INFO] Surefire report directory: C:\MVN\consumerBanking\targetsurefire-reports
  22. -------------------------------------------------------
  23. T E S T S
  24. -------------------------------------------------------
  25. Running com.companyname.bank.AppTest
  26. Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.027 sec
  27. Results :
  28. Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
  29. [INFO] [jar:jar {execution: default-jar}]
  30. [INFO] Building jar: C:\MVN\consumerBanking\targetconsumerBanking-1.0-SNAPSHOT.jar
  31. [INFO] ------------------------------------------------------------------------
  32. [INFO] BUILD SUCCESSFUL
  33. [INFO] ------------------------------------------------------------------------
  34. [INFO] Total time: 2 seconds
  35. [INFO] Finished at: Tue Jul 10 16:52:18 IST 2012
  36. [INFO] Final Memory: 16M/89M
  37. [INFO] ------------------------------------------------------------------------

你已经构建了你的工程并创建了最终的 jar 文件,下面是要学习的关键概念:

  • 我们给了 maven 两个目标,首先清理目标目录(clean),然后打包工程构建的输出为 jar(package)文件。
  • 打包好的 jar 文件可以在 consumerBanking\target 中获得,名称为 consumerBanking-1.0-SNAPSHOT.jar。
  • 测试报告存放在 consumerBanking\target\surefire-reports 文件夹中。
  • Maven 编译源码文件,以及测试源码文件。
  • 接着 Maven 运行测试用例。
  • 最后 Maven 创建工程包。

现在打开命令控制台,跳转到 C:\MVN\consumerBanking\target\classes 目录,并执行下面的 java 命令。

  1. C:\MVN\consumerBanking\target\classes>java com.companyname.bank.App

你可以看到结果:

  1. Hello World!

添加 Java 源文件

我们看看如何添加其他的 Java 文件到工程中。打开 C:\MVN\consumerBanking\src\main\java\com\companyname\bank 文件夹,在其中创建 Util 类 Util.java。

  1. package com.companyname.bank;
  2. public class Util
  3. {
  4. public static void printMessage(String message){
  5. System.out.println(message);
  6. }
  7. }

更新 App 类来使用 Util 类。

  1. package com.companyname.bank;
  2. /**
  3. * Hello world!
  4. *
  5. */
  6. public class App
  7. {
  8. public static void main( String[] args )
  9. {
  10. Util.printMessage("Hello World!");
  11. }
  12. }

现在打开命令控制台,跳转到 C:\MVN\consumerBanking 目录下,并执行下面的 mvn 命令。

  1. C:\MVN\consumerBanking>mvn clean compile

在 Maven 构建成功之后,跳转到 C:\MVN\consumerBanking\target\classes 目录下,并执行下面的 java 命令。

  1. C:\MVN\consumerBanking\target\classes>java -cp com.companyname.bank.App

你可以看到结果:

  1. Hello World!
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号