Maven的安裝配置與使用
Maven的用途之一是服務於構建,它是一個異常強大的構建工具,能夠幫我們自動化構建過程,從清理、編譯、測試到生成報告,再到打包和部署。我們不需要也不應該一遍又一遍地輸入命令,一次又一次地點選滑鼠,我們要做的是使用Maven配置好專案,然後輸入簡單的命令(如mvn clean install),Maven會幫我們處理那些煩瑣的任務。
Maven還為全世界的Java開發者提供了一個免費的中央倉庫,在其中幾乎可以找到任何的流行開源類庫。通過一些Maven的衍生工具(如Nexus),我們還能對其進行快速地搜尋。只要定位了座標,Maven就能夠幫我們自動下載,省去了手工勞動。
類似於ANT(Another Neat Tool)使用build.xml來定義構建的指令碼。Maven使用一個POM.xml來進行定義。POM的意思是P
Maven的下載地址:Maven
可以直接從Maven的官方網站進行下載。下載完成之後,直接解壓開即可,無需安裝。
解壓完之後我們就需要配置Maven的環境變數:
MAVEN_HOME 對應到解壓的目錄
PATH %MAVEN_HOME%bin
MAVEN_OPTS -Xms128m -Xmx512m(可選)
這時候我們進控制檯進行測試:
說明配置成功。
- Bin:該目錄包含了mvn執行的指令碼,這些指令碼用來配置Java命令,準備好classpath
- Boot: 該目錄只包含一個檔案,以maven 3.0為例,該檔案為plexus-classworlds-2.2.3.jar。plexus-classworlds是一個類載入器框架,相對於預設的java類載入器,它提供了更豐富的語法以方便配置,Maven使用該框架載入自己的類庫。更多關於classworlds的資訊請參考。對於一般的Maven使用者來說,不必關心該檔案。
- Conf: 該目錄包含了一個非常重要的檔案settings.xml。直接修改該檔案,就能在機器上全域性地定製Maven的行為。一般情況下,我們更偏向於複製該檔案至~/.m2/目錄下(這裡~表示使用者目錄),然後修改該檔案,在使用者範圍定製Maven的行為。本書的後面將會多次提到該settings.xml,並逐步分析其中的各個元素。
- Lib: 該目錄包含了所有Maven執行時需要的Java類庫,Maven本身是分模組開發的,因此使用者能看到諸如mavn-core-3.0.jar、maven-model-3.0.jar之類的檔案,此外這裡還包含一些Maven用到的第三方依賴如common-cli-1.2.jar、google-collection-1.0.jar等等。(對於Maven 2來說,該目錄只包含一個如maven-2.2.1-uber.jar的檔案原本各為獨立JAR檔案的Maven模組和第三方類庫都被拆解後重新合併到了這個JAR檔案中)。可以說,這個lib目錄就是真正的Maven。關於該檔案,還有一點值得一提的是,使用者可以在這個目錄中找到Maven內建的超級POM,這一點在8.5小節詳細解釋。其他:LICENSE.txt記錄了Maven使用的軟體許可證ApacheLicense Version 2.0;NOTICE.txt記錄了Maven包含的第三方軟體;而README.txt則包含了Maven的簡要介紹,包括安裝需求及如何安裝的簡要指令等等。
由於Conf下面的settings.xml是全域性的設定檔案。我們可以在~/.m2/這個目錄下複製一個,同時如果不想要將Maven的repository預設到C盤 可以在settings.xml檔案中修改目錄位置:
<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ${user.home}/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
-->
<localRepository>E:/Maven/m2/repository</localRepository>
這時候我們就可以使用下面的指令:
mvn help:system
從命令列輸出看到Maven會下載maven-help-plugin,包括pom檔案和jar檔案。這些檔案都被下載到了Maven本地倉庫中。在我們制定的檔案目錄下下載了一大頓的Maven應用的檔案。
如果需要新增代理可以使用下邊的設定:
<proxies>
<proxy>
<id>my-proxy</id>
<active>true</active>
<protocol>http</protocol>
<host>218.14.227.197</host>
<port>3128</port>
<!--
<username>***</username>
<password>***</password>
<nonProxyHosts>repository.mycom.com|*.google.com</nonProxyHosts>
-->
</proxy>
</proxies>
我們先不使用IDE,手動配置一下Maven的實現:
1、建立一個資料夾E:\Maven Project,然後在這個目錄下面建立一個POM.xml
<?xml version="1.0" encoding="UTF-8"?>
<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.smile.mvnbook</groupId>
<artifactId>hello-world</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Maven Hello World Project</name>
</project>
程式碼的第一行是XML頭,指定了該xml文件的版本和編碼方式。緊接著是project元素,project是所有pom.xml的根元素,它還聲明瞭一些POM相關的名稱空間及xsd元素,雖然這些屬性不是必須的,但使用這些屬效能夠讓第三方工具(如IDE中的XML編輯器)幫助我們快速編輯POM。
根元素下的第一個子元素modelVersion指定了當前POM模型的版本,對於Maven2及Maven 3來說,它只能是4.0.0。
這段程式碼中最重要的是groupId,artifactId和version三行。這三個元素定義了一個專案基本的座標,在Maven的世界,任何的jar、pom或者war都是以基於這些基本的座標進行區分的。
groupId定義了專案屬於哪個組,這個組往往和專案所在的組織或公司存在關聯,譬如你在googlecode上建立了一個名為myapp的專案,那麼groupId就應該是com.googlecode.myapp,如果你的公司是mycom,有一個專案為myapp,那麼groupId就應該是com.mycom.myapp。本書中所有的程式碼都基於groupId com.smile.mvnbook。
artifactId定義了當前Maven專案在組中唯一的ID,我們為這個Hello World專案定義artifactId為hello-world,本書其他章節程式碼會被分配其他的artifactId。而在前面的groupId為com.googlecode.myapp的例子中,你可能會為不同的子專案(模組)分配artifactId,如:myapp-util、myapp-domain、myapp-web等等。
顧名思義,version指定了Hello World專案當前的版本——1.0-SNAPSHOT。SNAPSHOT意為快照,說明該專案還處於開發中,是不穩定的版本。隨著專案的發展,version會不斷更新,如升級為1.0、1.1-SNAPSHOT、1.1、2.0等等。本書的6.5小節會詳細介紹SNAPSHOT,第13章介紹如何使用Maven管理專案版本的升級釋出。
最後一個name元素聲明瞭一個對於使用者更為友好的專案名稱,雖然這不是必須的,但我還是推薦為每個POM宣告name,以方便資訊交流。
沒有任何實際的Java程式碼,我們就能夠定義一個Maven專案的POM,這體現了Maven的一大優點,它能讓專案物件模型最大程度地與實際程式碼相獨立,我們可以稱之為解耦,或者正交性,這在很大程度上避免了Java程式碼和POM程式碼的相互影響。比如當專案需要升級版本時,只需要修改POM,而不需要更改Java程式碼;而在POM穩定之後,日常的Java程式碼開發工作基本不涉及POM的修改。
以上部分轉載自:來源預設情況下,Maven假設專案主程式碼位於src/main/java目錄,我們遵循Maven的約定,建立該目錄,然後在該目錄下建立檔案com/smile/mvnbook/helloworld/HelloWorld.java:
package com.smile.mvnbook;
public class HelloWorld
{
public String sayHello()
{
return "Hello Maven";
}
public static void main(String[] args)
{
System.out.print( new HelloWorld().sayHello() );
}
}
關於該Java程式碼有兩點需要注意。首先,在95%以上的情況下,我們應該把專案主程式碼放到src/main/java/目錄下(遵循Maven的約定),而無須額外的配置,Maven會自動搜尋該目錄找到專案主程式碼。其次,該Java類的包名是com.smile.mvnbook.helloworld,這與我們之前在POM中定義的groupId和artifactId相吻合。一般來說,專案中Java類的包都應該基於專案的groupId和artifactId,這樣更加清晰,更加符合邏輯,也方便搜尋構件或者Java類。
在專案根目錄下執行命令 mvn clean compile:
E:\Maven Project>mvn clean compile
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for
com.smile.mvnbook:hello-world:jar:1.0-SNAPSHOT
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-comp
iler-plugin is missing. @ line 21, column 16
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten t
he stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support buildin
g such malformed projects.
[WARNING]
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Hello World Project 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ hello-world ---
[INFO] Deleting E:\Maven Project\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hello-worl
d ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e
. build is platform dependent!
[INFO] skip non existing resourceDirectory E:\Maven Project\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ hello-world ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. buil
d is platform dependent!
[INFO] Compiling 1 source file to E:\Maven Project\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.411 s
[INFO] Finished at: 2014-12-17T16:10:55+08:00
[INFO] Final Memory: 12M/123M
[INFO] ------------------------------------------------------------------------
clean告訴Maven清理輸出目錄target/,compile告訴Maven編譯專案主程式碼,從輸出中我們看到Maven首先執行了clean:clean任務,刪除target/目錄,預設情況下Maven構建的所有輸出都在target/目錄中;接著執行resources:resources任務(未定義專案資源,暫且略過);最後執行compiler:compile任務,將專案主程式碼編譯至target/classes目錄(編譯好的類為com/juvenxu/mvnbook/helloworld/HelloWorld.Class)。
上文提到的clean:clean、resources:resources,以及compiler:compile對應了一些Maven外掛及外掛目標,比如clean:clean是clean外掛的clean目標,compiler:compile是compiler外掛的compile目標。
要使用JUnit,我們首先需要為Hello World專案新增一個JUnit依賴,修改POM.xml: <?xml version="1.0" encoding="UTF-8"?>
<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.juvenxu.mvnbook</groupId>
<artifactId>hello-world</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Maven Hello World Project</name>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
程式碼中添加了dependencies元素,該元素下可以包含多個dependency元素以宣告專案的依賴,這裡我們添加了一個依賴——groupId是junit,artifactId是junit,version是4.7。前面我們提到groupId、artifactId和version是任何一個Maven專案最基本的座標,JUnit也不例外,有了這段宣告,Maven就能夠自動下載junit-4.7.jar。也許你會問,Maven從哪裡下載這個jar呢?在Maven之前,我們可以去JUnit的官網下載分發包。而現在有了Maven,它會自動訪問中央倉庫(http://repo1.maven.org/maven2/),下載需要的檔案。讀者也可以自己訪問該倉庫,開啟路徑junit/junit/4.7/,就能看到junit-4.7.pom和junit-4.7.jar。本書第6章會詳細介紹Maven倉庫及中央倉庫。
上述POM程式碼中還有一個值為test的元素scope,scope為依賴範圍,若依賴範圍為test則表示該依賴只對測試有效,換句話說,測試程式碼中的import JUnit程式碼是沒有問題的,但是如果我們在主程式碼中用import JUnit程式碼,就會造成編譯錯誤。如果不宣告依賴範圍,那麼預設值就是compile,表示該依賴對主程式碼和測試程式碼都有效。
在src/test/java目錄下建立檔案:
package com.smile.mvnbook;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class HelloWorldTest
{
@Test
public void testSayHello()
{
HelloWorld helloWorld = new HelloWorld();
String result = helloWorld.sayHello();
assertEquals( "Hello Maven", result );
}
}
測試用例編寫完畢之後就可以呼叫Maven執行測試,執行 mvn clean test :
E:\Maven Project>mvn clean test
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for
com.smile.mvnbook:hello-world:jar:1.0-SNAPSHOT
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-com
iler-plugin is missing. @ line 21, column 16
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten
he stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support buildi
g such malformed projects.
[WARNING]
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Hello World Project 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ hello-world ---
[INFO] Deleting E:\Maven Project\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hello-wor
d ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.
. build is platform dependent!
[INFO] skip non existing resourceDirectory E:\Maven Project\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ hello-world --
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. bui
d is platform dependent!
[INFO] Compiling 1 source file to E:\Maven Project\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ h
llo-world ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.
. build is platform dependent!
[INFO] skip non existing resourceDirectory E:\Maven Project\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ hello-
orld ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. bui
d is platform dependent!
[INFO] Compiling 1 source file to E:\Maven Project\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ hello-world ---
[INFO] Surefire report directory: E:\Maven Project\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.smile.mvnbook.HelloWorldTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.047 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.929 s
[INFO] Finished at: 2014-12-17T16:16:13+08:00
[INFO] Final Memory: 13M/123M
[INFO] ------------------------------------------------------------------------
將專案進行編譯、測試之後,下一個重要步驟就是打包(package)。Hello World的POM中沒有指定打包型別,使用預設打包型別jar,我們可以簡單地執行命令mvn clean package 進行打包,可以看到如下輸出:
E:\Maven Project>mvn clean package
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for
com.smile.mvnbook:hello-world:jar:1.0-SNAPSHOT
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-comp
iler-plugin is missing. @ line 21, column 16
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten t
he stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support buildin
g such malformed projects.
[WARNING]
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Hello World Project 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ hello-world ---
[INFO] Deleting E:\Maven Project\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hello-worl
d ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e
. build is platform dependent!
[INFO] skip non existing resourceDirectory E:\Maven Project\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ hello-world ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. buil
d is platform dependent!
[INFO] Compiling 1 source file to E:\Maven Project\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ he
llo-world ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e
. build is platform dependent!
[INFO] skip non existing resourceDirectory E:\Maven Project\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ hello-w
orld ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. buil
d is platform dependent!
[INFO] Compiling 1 source file to E:\Maven Project\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ hello-world ---
[INFO] Surefire report directory: E:\Maven Project\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.smile.mvnbook.HelloWorldTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.049 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ hello-world ---
[INFO] Building jar: E:\Maven Project\target\hello-world-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-shade-plugin:1.2.1:shade (default) @ hello-world ---
[INFO] Replacing original artifact with shaded artifact.
[INFO] Replacing E:\Maven Project\target\hello-world-1.0-SNAPSHOT.jar with E:\Ma
ven Project\target\hello-world-1.0-SNAPSHOT-shaded.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.992 s
[INFO] Finished at: 2014-12-17T16:17:14+08:00
[INFO] Final Memory: 16M/123M
[INFO] ------------------------------------------------------------------------
類似地,Maven會在打包之前執行編譯、測試等操作。這裡我們看到jar:jar任務負責打包,實際上就是jar外掛的jar目標將專案主程式碼打包成一個名為hello-world-1.0-SNAPSHOT.jar的檔案,該檔案也位於target/輸出目錄中,它是根據artifact-version.jar規則進行命名的,如有需要,我們還可以使用finalName來自定義該檔案的名稱。
至此,我們得到了專案的輸出,如果有需要的話,就可以複製這個jar檔案到其他專案的Classpath中從而使用HelloWorld類。但是,如何才能讓其他的Maven專案直接引用這個jar呢?我們還需要一個安裝的步驟,執行mvn clean install:
E:\Maven Project>mvn clean install
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for
com.smile.mvnbook:hello-world:jar:1.0-SNAPSHOT
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-com
iler-plugin is missing. @ line 21, column 16
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten
he stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support buildi
g such malformed projects.
[WARNING]
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Hello World Project 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ hello-world ---
[INFO] Deleting E:\Maven Project\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hello-wor
d ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.
. build is platform dependent!
[INFO] skip non existing resourceDirectory E:\Maven Project\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ hello-world --
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. bui
d is platform dependent!
[INFO] Compiling 1 source file to E:\Maven Project\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ h
llo-world ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.
. build is platform dependent!
[INFO] skip non existing resourceDirectory E:\Maven Project\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ hello-
orld ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. bui
d is platform dependent!
[INFO] Compiling 1 source file to E:\Maven Project\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ hello-world ---
[INFO] Surefire report directory: E:\Maven Project\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.smile.mvnbook.HelloWorldTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.048 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ hello-world ---
[INFO] Building jar: E:\Maven Project\target\hello-world-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-shade-plugin:1.2.1:shade (default) @ hello-world ---
[INFO] Replacing original artifact with shaded artifact.
[INFO] Replacing E:\Maven Project\target\hello-world-1.0-SNAPSHOT.jar with E:\M
ven Project\target\hello-world-1.0-SNAPSHOT-shaded.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ hello-world ---
[INFO] Installing E:\Maven Project\target\hello-world-1.0-SNAPSHOT.jar to E:\Ma
en\m2\repository\com\smile\mvnbook\hello-world\1.0-SNAPSHOT\hello-world-1.0-SNA
SHOT.jar
[INFO] Installing E:\Maven Project\pom.xml to E:\Maven\m2\repository\com\smile\
vnbook\hello-world\1.0-SNAPSHOT\hello-world-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.301 s
[INFO] Finished at: 2014-12-17T16:18:25+08:00
[INFO] Final Memory: 15M/123M
[INFO] ------------------------------------------------------------------------
到目前為止,我們還沒有執行Hello World專案,不要忘了HelloWorld類可是有一個main方法的。預設打包生成的jar是不能夠直接執行的,因為帶有main方法的類資訊不會新增到manifest中(我們可以開啟jar檔案中的META-INF/MANIFEST.MF檔案,將無法看到Main-Class一行)。為了生成可執行的jar檔案,我們需要藉助maven-shade-plugin,配置該外掛如下:
<?xml version="1.0" encoding="UTF-8"?>
<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.smile.mvnbook</groupId>
<artifactId>hello-world</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Maven Hello World Project</name>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.smile.mvnbook.HelloWorld</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
plugin元素在POM中的相對位置應該在<project><build><plugins>下面。我們配置了mainClass為com.juvenxu.mvnbook.helloworld.HelloWorld,專案在打包時會將該資訊放到MANIFEST中。現在執行mvn clean install ,待構建完成之後開啟target/目錄,我們可以看到hello-world-1.0-SNAPSHOT.jar和original-hello-world-1.0-SNAPSHOT.jar,前者是帶有Main-Class資訊的可執行jar,後者是原始的jar,開啟hello-world-1.0-SNAPSHOT.jar的META-INF/MANIFEST.MF,可以看到它包含這樣一行資訊:
Main-Class: com.juvenxu.mvnbook.helloworld.HelloWorld
現在,我們在專案根目錄中執行該jar檔案:
E:\Maven Project>java -jar target/hello-world-1.0-SNAPSHOT.jar
Hello Maven
Hello World專案中有一些Maven的約定:在專案的根目錄中放置pom.xml,在src/main/java目錄中放置專案的主程式碼,在src/test/java中放置專案的測試程式碼。
還是以Hello World為例,我們使用maven archetype來建立該專案的骨架,離開當前的Maven專案目錄。
如果是Maven 3,簡單的執行:
mvn archetype:generate
在IDE工具中建立Maven Project就比較簡單,這裡不做贅述。轉載自:Maven的安裝、配置及使用入門