我們?cè)趧?chuàng)建工程章節(jié)中學(xué)到的是如何使用 Maven 創(chuàng)建 Java 應(yīng)用?,F(xiàn)在我們將看到如何構(gòu)建和測(cè)試這個(gè)應(yīng)用。
跳轉(zhuǎn)到 C:/MVN 目錄下,既你的 java 應(yīng)用目錄下。打開(kāi) consumerBanking 文件夾。你將看到 POM.xml 文件中有下面的內(nèi)容。
<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 已經(jīng)添加了 JUnit 作為測(cè)試框架。默認(rèn) Maven 添加了一個(gè)源碼文件 App.java 和一個(gè)測(cè)試文件 AppTest.java 到上個(gè)章節(jié)中我們提到的默認(rèn)目錄結(jié)構(gòu)中。
打開(kāi)命令控制臺(tái),跳轉(zhuǎn)到 C:\MVN\consumerBanking 目錄下,并執(zhí)行以下 mvn 命令。
C:\MVN\consumerBanking>mvn clean package
Maven 將開(kāi)始構(gòu)建工程。
[INFO] Scanning for projects...
[INFO] -------------------------------------------------------------------
[INFO] Building consumerBanking
[INFO] task-segment: [clean, package]
[INFO] -------------------------------------------------------------------
[INFO] [clean:clean {execution: default-clean}]
[INFO] Deleting directory C:\MVN\consumerBanking\target
[INFO] [resources:resources {execution: default-resources}]
[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] [compiler:compile {execution: default-compile}]
[INFO] Compiling 1 source file to C:\MVN\consumerBanking\target\classes
[INFO] [resources:testResources {execution: default-testResources}]
[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] [compiler:testCompile {execution: default-testCompile}]
[INFO] Compiling 1 source file to C:\MVN\consumerBanking\target\test-classes
[INFO] [surefire:test {execution: default-test}]
[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.027 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] [jar:jar {execution: default-jar}]
[INFO] Building jar: C:\MVN\consumerBanking\target\
consumerBanking-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2 seconds
[INFO] Finished at: Tue Jul 10 16:52:18 IST 2012
[INFO] Final Memory: 16M/89M
[INFO] ------------------------------------------------------------------------
你已經(jīng)構(gòu)建了你的工程并創(chuàng)建了最終的 jar 文件,下面是要學(xué)習(xí)的關(guān)鍵概念:
現(xiàn)在打開(kāi)命令控制臺(tái),跳轉(zhuǎn)到 C:\MVN\consumerBanking\target\classes 目錄,并執(zhí)行下面的 java 命令。
C:\MVN\consumerBanking\target\classes>java com.companyname.bank.App
你可以看到結(jié)果:
Hello World!
我們看看如何添加其他的 Java 文件到工程中。打開(kāi) C:\MVN\consumerBanking\src\main\java\com\companyname\bank 文件夾,在其中創(chuàng)建 Util 類 Util.java。
package com.companyname.bank;
public class Util
{
public static void printMessage(String message){
System.out.println(message);
}
}
更新 App 類來(lái)使用 Util 類。
package com.companyname.bank;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
Util.printMessage("Hello World!");
}
}
現(xiàn)在打開(kāi)命令控制臺(tái),跳轉(zhuǎn)到 C:\MVN\consumerBanking 目錄下,并執(zhí)行下面的 mvn 命令。
C:\MVN\consumerBanking>mvn clean compile
在 Maven 構(gòu)建成功之后,跳轉(zhuǎn)到 C:\MVN\consumerBanking\target\classes 目錄下,并執(zhí)行下面的 java 命令。
C:\MVN\consumerBanking\target\classes>java -cp com.companyname.bank.App
你可以看到結(jié)果:
Hello World!
更多建議: