-

我们在“项目创建”一章中学到的是如何使用Maven创建Java应用程序。现在我们将看到如何创建和测试应用程序。

转到您创建Java应用程序的C:/ MVN目录。打开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:MVNconsumerBanking>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:MVNconsumerBanking	arget
[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:MVNconsumerBankingsrcmain
esources
[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:MVNconsumerBanking	argetclasses
[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:MVNconsumerBankingsrc	est
esources
[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:MVNconsumerBanking	arget	est-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ consumerBanking ---
[INFO] Surefire report directory: C:MVNconsumerBanking	argetsurefire-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:MVNconsumerBanking	argetconsumerBanking-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 14.406 s
[INFO] Finished at: 2015-09-27T17:58:06+05:30
[INFO] Final Memory: 14M/247M
[INFO] ------------------------------------------------------------------------

您已经创建了项目并创建了最终的jar文件,以下是关键的学习概念

现在打开命令控制台,进入C: MVN consumerBanking target classes目录并执行以下java命令。

C:MVNconsumerBanking	argetclasses>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:MVNconsumerBanking>mvn clean compile

Maven创建成功后,请执行以下命令:C: MVN consumerBanking target classes目录。

C:MVNconsumerBanking	argetclasses>java -cp com.companyname.bank.App

你会看到结果

Hello World!