Maven學習筆記(一):Maven介紹、安裝及簡單示例

分類:IT技術 時間:2017-07-04

註意:本系列文檔均以mac os平臺為試驗平臺。

一、Maven介紹

Maven是一個基於Java平臺的項目構建、依賴管理和項目信息管理的跨平臺的項目管理工具。Maven能夠最大化地消除構建的重復,抽象了完整的構建生命周期,並為絕大部分的構建任務提供了已實現的插件,同時,Maven還能夠幫助我們標準化構建過程。

 

二、Maven的安裝

在安裝Maven之前,需要安裝JDK(版本至少要求1.7及以上,本實驗平臺安裝的是Oracle JDK 1.8)

接下來詳述Maven安裝過程。

2.1 下載Maven

在Apache Maven的官方網址可以下載Maven的安裝包(http://maven.apache.org/download.cgi),如下圖所示,下載apache-maven-3.5.0-bin.tar.gz安裝包。

2.2 配置Maven環境變量

下載了安裝包之後,將該安裝包進行解壓至理想的目錄下(用戶可自行決定解壓目錄)

tar xzvf apache-maven-3.5.0-bin.tar.gz

解壓之後的maven目錄結構為:

LICENSE		
README.txt	
NOTICE
boot/		
lib/	
bin/		
conf/

將maven環境變量添加至~/.bashrc文件中

vi ~/.bashrc

在.bashrc文件中添加maven的環境變量如下

# Maven Environment
export MAVEN_HOME=/User/xhhuang/maven
export PATH=$PATH:$MAVEN_HOME/bin

其中,/User/xhhuang/maven為安裝包的解壓目錄。

添加完畢後,執行source ~/.bashrc命令,然後在終端輸入mvn -version目錄,看到如下所示結果說明maven環境變量配置成功。

xhHuangs-MacBook-Pro:maven xhhuang$ mvn -version
Apache Maven 3.5.0 (ff8f5e7444045639af65f6095c62210b5713f426; 2017-04-04T03:39:06+08:00)
Maven home: /Users/xhhuang/709/maven
Java version: 1.8.0_101, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "mac os x", version: "10.12.5", arch: "x86_64", family: "mac"
xhHuangs-MacBook-Pro:maven xhhuang$ 

 

三、Maven Eclipse開發環境搭建

首先需要下載Eclipse IDE,下載地址為:(http://www.eclipse.org/downloads/eclipse-packages/),可依據不同平臺進行下載。

3.1 安裝m2eclipse插件

選擇Eclipse導航欄Help中的Install new softwares,輸入m2eclipse網址中給出的下載鏈接(詳細地址請訪問http://www.eclipse.org/m2e/),地址如下圖所示:

在eclipse中輸入網址,然後全選Mave Integration for Eclipse,點擊下一步進行自動下載安裝,如下圖所示:

3.3 配置外部maven程序

Eclipse默認使用內置的mave程序,我們需要將其配置為我們在第2節中安裝的外部maven程序。

選擇Eclipse中的Preferences,然後選擇maven的installation選項,添加第2節中安裝的外部maven程序地址即可,如下圖所示。

至此,maven的基本開發環境已經全部搭建完畢。

 

四、Maven的基本使用

4.1 創建Maven項目

可以通過Archetype生成maven項目骨架,使用mvn archetype:generate命令

mkdir HelloWorld

cd HelloWorld/

xhHuangs-MacBook-Pro:HelloWorld xhhuang$ mvn archetype:generate
[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] >>> maven-archetype-plugin:3.0.1:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO] 
[INFO] <<< maven-archetype-plugin:3.0.1:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO] 
[INFO] 
[INFO] --- maven-archetype-plugin:3.0.1:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Interactive mode
[INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0)
Choose archetype:
...
中間的一些下載過程、說明過程在此處都省略掉
...
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 997: 
Choose org.apache.maven.archetypes:maven-archetype-quickstart version: 
1: 1.0-alpha-1
2: 1.0-alpha-2
3: 1.0-alpha-3
4: 1.0-alpha-4
5: 1.0
6: 1.1
Choose a number: 6: 
Define value for property 'groupId': com.xhhuang
Define value for property 'artifactId': helloworld
Define value for property 'version' 1.0-SNAPSHOT: : 
Define value for property 'package' com.xhhuang: : com.xhhuang.helloworld
Confirm properties configuration:
groupId: com.xhhuang
artifactId: helloworld
version: 1.0-SNAPSHOT
package: com.xhhuang.helloworld
 Y: : y
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.1
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: basedir, Value: /Users/xhhuang/Documents/mavenProject/HelloWorld
[INFO] Parameter: package, Value: com.xhhuang.helloworld
[INFO] Parameter: groupId, Value: com.xhhuang
[INFO] Parameter: artifactId, Value: helloworld
[INFO] Parameter: packageName, Value: com.xhhuang.helloworld
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: /Users/xhhuang/Documents/mavenProject/HelloWorld/helloworld
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 44.938 s
[INFO] Finished at: 2017-07-04T00:29:14+08:00
[INFO] Final Memory: 18M/213M
[INFO] ------------------------------------------------------------------------
xhHuangs-MacBook-Pro:HelloWorld xhhuang$ 

在交互的過程中,回提示讓你輸入groupId、artifactId、version以及package。

  • groupID:定義了項目屬於哪一個組,這個組往往和項目所在的組織或公司存在關聯。譬如,在Googlecode上建立一個名為myapp的項目,那麽gourpId就應該為com.googlecode.myapp/
  • artifactId:定義了當前maven項目在組中的唯一ID,此處我們的artifactId為helloworld
  • version:指定了maven項目的當前版本,SNAPSHOT為快照,表示該項目目前還處於開發階段,是不穩定的版本
  • packageName:定義了包的名字

maven通過groupId、artifactId和version定義一個項目的基本坐標。

可以看到生成的maven項目的目錄樹結構如下所示:

xhHuangs-MacBook-Pro:mavenProject xhhuang$ ls -R helloworld/
pom.xml	src

helloworld//src:
main	test

helloworld//src/main:
java

helloworld//src/main/java:
com

helloworld//src/main/java/com:
xhhuang

helloworld//src/main/java/com/xhhuang:
helloworld

helloworld//src/main/java/com/xhhuang/helloworld:
App.java

helloworld//src/test:
java

helloworld//src/test/java:
com

helloworld//src/test/java/com:
xhhuang

helloworld//src/test/java/com/xhhuang:
helloworld

helloworld//src/test/java/com/xhhuang/helloworld:
AppTest.java
xhHuangs-MacBook-Pro:mavenProject xhhuang$ 

App.java中的默認代碼如下圖所示:

修改pom.xml文件,在<project></project>標簽內添加如下部分:

<build>
    <plugins>
      <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.xhhuang.helloworld.App</mainClass>
                              </transformer>
                          </transformers>
                      </configuration>
              </execution>
          </executions>
       </plugin>
    </plugins>
  </build>

這一部分保證我們生成的jar包能夠通過java -jar命令運行(能找到main函數入口)。

執行mvn clean package命令生成jar包,jar包在命令執行之後產生的target目錄中。

xhHuangs-MacBook-Pro:helloworld xhhuang$ ls
pom.xml	src
xhHuangs-MacBook-Pro:helloworld xhhuang$ mvn clean package
[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] Building helloworld 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ helloworld ---
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ helloworld ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/xhhuang/Documents/mavenProject/helloworld/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ helloworld ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/xhhuang/Documents/mavenProject/helloworld/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ helloworld ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/xhhuang/Documents/mavenProject/helloworld/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ helloworld ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/xhhuang/Documents/mavenProject/helloworld/target/test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ helloworld ---
[INFO] Surefire report directory: /Users/xhhuang/Documents/mavenProject/helloworld/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.xhhuang.helloworld.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.007 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ helloworld ---
[INFO] Building jar: /Users/xhhuang/Documents/mavenProject/helloworld/target/helloworld-1.0-SNAPSHOT.jar
[INFO] 
[INFO] --- maven-shade-plugin:1.2.1:shade (default) @ helloworld ---
[INFO] Replacing original artifact with shaded artifact.
[INFO] Replacing /Users/xhhuang/Documents/mavenProject/helloworld/target/helloworld-1.0-SNAPSHOT.jar with /Users/xhhuang/Documents/mavenProject/helloworld/target/helloworld-1.0-SNAPSHOT-shaded.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.649 s
[INFO] Finished at: 2017-07-04T00:50:51+08:00
[INFO] Final Memory: 18M/197M
[INFO] ------------------------------------------------------------------------
xhHuangs-MacBook-Pro:helloworld xhhuang$ ls
pom.xml	src	target
xhHuangs-MacBook-Pro:helloworld xhhuang$ cd target/
xhHuangs-MacBook-Pro:target xhhuang$ ls
classes					original-helloworld-1.0-SNAPSHOT.jar
helloworld-1.0-SNAPSHOT.jar		surefire-reports
maven-archiver				test-classes
maven-status
xhHuangs-MacBook-Pro:target xhhuang$ java -jar helloworld-1.0-SNAPSHOT.jar 
Hello World!

可以看到,通過java -jar命令運行產生之後的jar文件,輸出了程序的結果。

 

附錄:maven常用命令

mvn clean

mvn clean compile

mvn clean test

mvn clean package

mvn clean install


Tags: Maven 安裝 maven 構建 變量 解壓

文章來源:


ads
ads

相關文章
ads

相關文章

ad