Maven——項目構建完整配置

 我的工具包在哪裏,我的插件在哪裏?哎,我的項目怎麼缺了這個又缺了哪個,咦,這兩個動動版本不配合?不起效了!怎麼辦!怎麼辦!在Maven沒有誕生前,這一系列的問題,總能困住一羣又一羣的開發人員,而今天的我們,因爲有了maven,再也不必擔心依賴於插件的問題啦。所以,我們有必要好好來了解一下Maven。

 

一、Maven簡介

(一)什麼是Maven

      Maven是apache下的開源項目,項目管理工具,管理java項目。

1、項目對象模型(Project Object Model)

      POM對象模型,每個maven工程中都有一個pom.xml文件,定義工程所依賴的jar包、本工程的座標、打包運行方式。。

2、依賴管理系統(基礎核心 )

       maven通過座標對項目工程所依賴的jar包統一規範管理。

3、maven定義一套項目生命週期

清理、初始化、編譯、測試、報告、打包、部署、站點生成

4、一組標準集合

       強調:maven工程有自己標準的工程目錄結構、定義座標有標準。

5、maven 管理項目生命週期過程都是基於插件完成的

(二)Maven倉庫

1、中央倉庫

       就是遠程倉庫,倉庫中jar由專業團隊(maven團隊)統一維護。

       中央倉庫的地址:http://repo1.maven.org/maven2/

2、本地倉庫

        相當於緩存,工程第一次會從遠程倉庫(互聯網)去下載jar 包,將jar包存在本地倉庫(在程序員的電腦上)。第二次不需要從遠程倉庫去下載。先從本地倉庫找,如果找不到纔會去遠程倉庫找。

3、私服

       在公司內部架設一臺私服,其它公司架設一臺倉庫,對外公開。

二、Maven常用操作

Maven依賴查詢
http://mvnrepository.com/

導入jar到本地倉庫:

http://blog.csdn.net/we_shell/article/details/49819221

Maven常用命令
1. 
創建Maven的普通java項目:
   mvn archetype:create
   -DgroupId=packageName
   -DartifactId=projectName 
2. 
創建MavenWeb項目:  
    mvn archetype:create
    -DgroupId=packageName   
    -DartifactId=webappName
    -DarchetypeArtifactId=maven-archetype-webapp   
3. 
編譯源代碼: mvn compile  (將項目中java文件編譯爲class,放在target目錄)
4. 
編譯測試代碼:mvn test-compile   
5. 
運行測試:mvn test  
6. 
產生sitemvn site  
7. 
打包:mvn package      (先執行compile,然後將class文件打包成jar或war包到本項目target目錄)
8. 
在本地Repository中安裝jarmvn install (先執行compile與package,然後將jar包安裝到本地倉庫) 
9. 
清除產生的項目:mvn clean  
10. 
生成eclipse項目:mvn eclipse:eclipse 
11. 
生成idea項目:mvn idea:idea 
12. 
組合使用goal命令,如只打包不測試:mvn -Dtest package  
13. 
編譯測試的內容:mvn test-compile 
14. 
只打jar: mvn jar:jar 
15. 
只測試而不編譯,也不測試編譯:mvn test -skipping compile -skipping test-compile
      ( -skipping 
的靈活運用,當然也可以用於其他組合命令
16 
只打包不測試:mvn package -Dmaven.test.skip=true -P prepare

17. 清除eclipse的一些系統設置:mvn eclipse:clean 

18.安裝到遠程倉庫:mvn deploy

三、Maven的Setting.xml

頂級元素概覽

下面列舉了settings.xml中的頂級元素

[html] view plain copy
  1. <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"  
  2.      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     
  3.      xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0   
  4.      https://maven.apache.org/xsd/settings-1.0.0.xsd">  
  5.   
  6.   <localRepository/>  
  7.   
  8.   <interactiveMode/>  
  9.   
  10.   <usePluginRegistry/>  
  11.   
  12.   <offline/>  
  13.   
  14.   <pluginGroups/>  
  15.   
  16.   <servers/>  
  17.   
  18.   <mirrors/>  
  19.   
  20.   <proxies/>  
  21.   
  22.   <profiles/>  
  23.   
  24.   <activeProfiles/></settings>  

LocalRepository

作用:該值表示構建系統本地倉庫的路徑
其默認值:~/.m2/repository。

<localRepository>${user.home}/.m2/repository</localRepository>

InteractiveMode

作用:表示maven是否需要和用戶交互以獲得輸入。
如果maven需要和用戶交互以獲得輸入,則設置成true,反之則應爲false。默認爲true。

<interactiveMode>true</interactiveMode>

UsePluginRegistry

作用:maven是否需要使用plugin-registry.xml文件來管理插件版本。
如果需要讓maven使用文件~/.m2/plugin-registry.xml來管理插件版本,則設爲true。默認爲false。

<usePluginRegistry>false</usePluginRegistry>

Offline

作用:表示maven是否需要在離線模式下運行。
如果構建系統需要在離線模式下運行,則爲true,默認爲false。
當由於網絡設置原因或者安全因素,構建服務器不能連接遠程倉庫的時候,該配置就十分有用。

<offline>false</offline>

PluginGroups

作用:當插件的組織id(groupId)沒有顯式提供時,供搜尋插件組織Id(groupId)的列表。
該元素包含一個pluginGroup元素列表,每個子元素包含了一個組織Id(groupId)。
當我們使用某個插件,並且沒有在命令行爲其提供組織Id(groupId)的時候,Maven就會使用該列表。默認情況下該列表包含了org.apache.maven.plugins和org.codehaus.mojo。

[html] view plain copy
  1. <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"  
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0                     https://maven.apache.org/xsd/settings-1.0.0.xsd">  
  3.   
  4.   ...  
  5.   
  6.   <pluginGroups>  
  7.   
  8.     <!--plugin的組織Id(groupId) -->  
  9.    <pluginGroup>org.codehaus.mojo</pluginGroup>  
  10.   
  11.   </pluginGroups>  
  12.   
  13.   ...</settings>  

Servers

作用:一般,倉庫的下載和部署是在pom.xml文件中的repositories和distributionManagement元素中定義的。然而,一般類似用戶名、密碼(有些倉庫訪問是需要安全認證的)等信息不應該在pom.xml文件中配置,這些信息可以配置在settings.xml中。

[html] view plain copy
  1. <settings  
  2. xmlns="http://maven.apache.org/SETTINGS/1.0.0"  
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0                     https://maven.apache.org/xsd/settings-1.0.0.xsd">  
  4.   
  5.   ...  
  6.   
  7.   <!--配置服務端的一些設置。一些設置如安全證書不應該和pom.xml一起分發。這種類型的信息應該存在於構建服務器上的settings.xml文件中。 -->  
  8.   
  9.   <servers>  
  10.   
  11.     <!--服務器元素包含配置服務器時需要的信息 -->  
  12.   
  13.     <server>  
  14.   
  15.       <!--這是server的id(注意不是用戶登陸的id),該id與distributionManagement中repository元素的id相匹配。 -->  
  16.   
  17.       <id>server001</id>  
  18.   
  19.       <!--鑑權用戶名。鑑權用戶名和鑑權密碼錶示服務器認證所需要的登錄名和密碼。 -->  
  20.   
  21.       <username>my_login</username>  
  22.   
  23.       <!--鑑權密碼。鑑權用戶名和鑑權密碼錶示服務器認證所需要的登錄名和密碼。密碼加密功能已被添加到2.1.0 +。詳情請訪問密碼加密頁面 -->  
  24.   
  25.      <password>my_password</password>  
  26.   
  27.       <!--鑑權時使用的私鑰位置。和前兩個元素類似,私鑰位置和私鑰密碼指定了一個私鑰的路徑(默認是${user.home}/.ssh/id_dsa)以及如果需要的話,一個密語。將來passphrase和password元素可能會被提取到外部,但目前它們必須在settings.xml文件以純文本的形式聲明。 -->  
  28.   
  29.      <privateKey>${usr.home}/.ssh/id_dsa</privateKey>  
  30.   
  31.       <!--鑑權時使用的私鑰密碼。 -->  
  32.   
  33.      <passphrase>some_passphrase</passphrase>  
  34.   
  35.       <!--文件被創建時的權限。如果在部署的時候會創建一個倉庫文件或者目錄,這時候就可以使用權限(permission)。這兩個元素合法的值是一個三位數字,其對應了unix文件系統的權限,如664,或者775。 -->  
  36.   
  37.      <filePermissions>664</filePermissions>  
  38.   
  39.       <!--目錄被創建時的權限。 -->  
  40.   
  41.      <directoryPermissions>775</directoryPermissions>  
  42.   
  43.     </server>  
  44.   
  45.   </servers>  
  46.   
  47.   ...</settings>  

Mirrors

作用:爲倉庫列表配置的下載鏡像列表

[html] view plain copy
  1. <settings  
  2. xmlns="http://maven.apache.org/SETTINGS/1.0.0"  
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0                      https://maven.apache.org/xsd/settings-1.0.0.xsd">  
  4.   
  5.   ...  
  6.   
  7.   <mirrors>  
  8.   
  9.     <!-- 給定倉庫的下載鏡像。 -->  
  10.   
  11.     <mirror>  
  12.   
  13.       <!-- 該鏡像的唯一標識符。id用來區分不同的mirror元素。 -->  
  14.   
  15.       <id>planetmirror.com</id>  
  16.   
  17.       <!-- 鏡像名稱 -->  
  18.   
  19.       <name>PlanetMirrorAustralia</name>  
  20.   
  21.       <!-- 該鏡像的URL。構建系統會優先考慮使用該URL,而非使用默認的服務器URL。 -->  
  22.   
  23.      <url>http://downloads.planetmirror.com/pub/maven2</url>  
  24.   
  25.       <!-- 被鏡像的服務器的id。例如,如果我們要設置了一個Maven中央倉庫(http://repo.maven.apache.org/maven2/)的鏡像,就需要將該元素設置成central。這必須和中央倉庫的id central完全一致。 -->  
  26.   
  27.       <mirrorOf>central</mirrorOf>  
  28.   
  29.     </mirror>  
  30.   
  31.   </mirrors>  
  32.   
  33.   ...</settings>  

Proxies

作用:用來配置不同的代理。

[html] view plain copy
  1. <settings  
  2. xmlns="http://maven.apache.org/SETTINGS/1.0.0"  
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0                     https://maven.apache.org/xsd/settings-1.0.0.xsd">  
  4.   
  5.   ...  
  6.   
  7.   <proxies>  
  8.   
  9.     <!--代理元素包含配置代理時需要的信息 -->  
  10.   
  11.     <proxy>  
  12.   
  13.       <!--代理的唯一定義符,用來區分不同的代理元素。 -->  
  14.   
  15.       <id>myproxy</id>  
  16.   
  17.       <!--該代理是否是激活的那個。true則激活代理。當我們聲明瞭一組代理,而某個時候只需要激活一個代理的時候,該元素就可以派上用處。 -->  
  18.   
  19.       <active>true</active>  
  20.   
  21.       <!--代理的協議。協議://主機名:端口,分隔成離散的元素以方便配置。 -->  
  22.   
  23.       <protocol>http</protocol>  
  24.   
  25.       <!--代理的主機名。協議://主機名:端口,分隔成離散的元素以方便配置。 -->  
  26.   
  27.       <host>proxy.somewhere.com</host>  
  28.   
  29.       <!--代理的端口。協議://主機名:端口,分隔成離散的元素以方便配置。 -->  
  30.   
  31.       <port>8080</port>  
  32.   
  33.       <!--代理的用戶名,用戶名和密碼錶示代理服務器認證的登錄名和密碼。 -->  
  34.   
  35.      <username>proxyuser</username>  
  36.   
  37.       <!--代理的密碼,用戶名和密碼錶示代理服務器認證的登錄名和密碼。 -->  
  38.   
  39.      <password>somepassword</password>  
  40.   
  41.       <!--不該被代理的主機名列表。該列表的分隔符由代理服務器指定;例子中使用了豎線分隔符,使用逗號分隔也很常見。 -->  
  42.   
  43.      <nonProxyHosts>*.google.com|ibiblio.org</nonProxyHosts>  
  44.   
  45.     </proxy>  
  46.   
  47.   </proxies>  
  48.   
  49.   ...</settings>  

Profiles

作用:根據環境參數來調整構建配置的列表。
settings.xml中的profile元素是pom.xml中profile元素的裁剪版本。
它包含了id、activation、repositories、pluginRepositories和 properties元素。這裏的profile元素只包含這五個子元素是因爲這裏只關心構建系統這個整體(這正是settings.xml文件的角色定位),而非單獨的項目對象模型設置。如果一個settings.xml中的profile被激活,它的值會覆蓋任何其它定義在pom.xml中帶有相同id的profile。

[html] view plain copy
  1. <settings  
  2. xmlns="http://maven.apache.org/SETTINGS/1.0.0"  
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0                      https://maven.apache.org/xsd/settings-1.0.0.xsd">  
  4.   
  5.  ...  
  6.   
  7.  <profiles>  
  8.   
  9.    <profile>  
  10.   
  11.      <!-- profile的唯一標識 -->  
  12.   
  13.      <id>test</id>  
  14.   
  15.      <!-- 自動觸發profile的條件邏輯 -->  
  16.   
  17.      <activation />  
  18.   
  19.      <!-- 擴展屬性列表 -->  
  20.   
  21.      <properties />  
  22.   
  23.      <!-- 遠程倉庫列表 -->  
  24.   
  25.      <repositories />  
  26.   
  27.      <!-- 插件倉庫列表 -->  
  28.   
  29.      <pluginRepositories />  
  30.   
  31.    </profile>  
  32.   
  33.  </profiles>  
  34.   
  35.  ...</settings>  

 

Activation

作用:自動觸發profile的條件邏輯。
如pom.xml中的profile一樣,profile的作用在於它能夠在某些特定的環境中自動使用某些特定的值;這些環境通過activation元素指定。
activation元素並不是激活profile的唯一方式。settings.xml文件中的activeProfile元素可以包含profile的id。profile也可以通過在命令行,使用-P標記和逗號分隔的列表來顯式的激活(如,-P test)。

[html] view plain copy
  1. <activation>  
  2.   
  3.   <!--profile默認是否激活的標識 -->  
  4.   
  5.  <activeByDefault>false</activeByDefault>  
  6.   
  7.   <!--當匹配的jdk被檢測到,profile被激活。例如,1.4激活JDK1.4,1.4.0_2,而!1.4激活所有版本不是以1.4開頭的JDK。 -->  
  8.   
  9.   <jdk>1.5</jdk>  
  10.   
  11.   <!--當匹配的操作系統屬性被檢測到,profile被激活。os元素可以定義一些操作系統相關的屬性。 -->  
  12.   
  13.   <os>  
  14.   
  15.     <!--激活profile的操作系統的名字 -->  
  16.   
  17.     <name>Windows XP</name>  
  18.   
  19.     <!--激活profile的操作系統所屬家族(如 'windows') -->  
  20.   
  21.     <family>Windows</family>  
  22.   
  23.     <!--激活profile的操作系統體系結構 -->  
  24.   
  25.     <arch>x86</arch>  
  26.   
  27.     <!--激活profile的操作系統版本 -->  
  28.   
  29.     <version>5.1.2600</version>  
  30.   
  31.   </os>  
  32.   
  33.   <!--如果Maven檢測到某一個屬性(其值可以在POM中通過${name}引用),其擁有對應的name = 值,Profile就會被激活。如果值字段是空的,那麼存在屬性名稱字段就會激活profile,否則按區分大小寫方式匹配屬性值字段 -->  
  34.   
  35.   <property>  
  36.   
  37.     <!--激活profile的屬性的名稱 -->  
  38.   
  39.     <name>mavenVersion</name>  
  40.   
  41.     <!--激活profile的屬性的值 -->  
  42.   
  43.     <value>2.0.3</value>  
  44.   
  45.   </property>  
  46.   
  47.   <!--提供一個文件名,通過檢測該文件的存在或不存在來激活profile。missing檢查文件是否存在,如果不存在則激活profile。另一方面,exists則會檢查文件是否存在,如果存在則激活profile。 -->  
  48.   
  49.   <file>  
  50.   
  51.     <!--如果指定的文件存在,則激活profile。 -->  
  52.   
  53.    <exists>${basedir}/file2.properties</exists>  
  54.   
  55.     <!--如果指定的文件不存在,則激活profile。 -->  
  56.   
  57.    <missing>${basedir}/file1.properties</missing>  
  58.   
  59.   </file></activation>  

注:在maven工程的pom.xml所在目錄下執行mvn help:active-profiles命令可以查看中央倉儲的profile是否在工程中生效。

properties

作用:對應profile的擴展屬性列表。
maven屬性和ant中的屬性一樣,可以用來存放一些值。這些值可以在pom.xml中的任何地方使用標記${X}來使用,這裏X是指屬性的名稱。屬性有五種不同的形式,並且都能在settings.xml文件中訪問。

<!--  1. env.X: 在一個變量前加上"env."的前綴,會返回一個shell環境變量。例如,"env.PATH"指代了$path環境變量(在Windows上是%PATH%)。   2. project.x:指代了POM中對應的元素值。例如: <project><version>1.0</version></project>通過${project.version}獲得version的值。   3. settings.x: 指代了settings.xml中對應元素的值。例如:<settings><offline>false</offline></settings>通過 ${settings.offline}獲得offline的值。   4. Java System Properties: 所有可通過java.lang.System.getProperties()訪問的屬性都能在POM中使用該形式訪問,例如 ${java.home}。   5. x: 在<properties/>元素中,或者外部文件中設置,以${someVar}的形式使用。 --><properties>

 <user.install>${user.home}/our-project</user.install></properties>

注:如果該profile被激活,則可以在pom.xml中使用${user.install}。

Repositories

作用:遠程倉庫列表,它是maven用來填充構建系統本地倉庫所使用的一組遠程倉庫。

[html] view plain copy
  1. <repositories>  
  2.   
  3.   <!--包含需要連接到遠程倉庫的信息 -->  
  4.   
  5.   <repository>  
  6.   
  7.     <!--遠程倉庫唯一標識 -->  
  8.   
  9.     <id>codehausSnapshots</id>  
  10.   
  11.     <!--遠程倉庫名稱 -->  
  12.   
  13.     <name>Codehaus Snapshots</name>  
  14.   
  15.     <!--如何處理遠程倉庫裏發佈版本的下載 -->  
  16.   
  17.     <releases>  
  18.   
  19.       <!--true或者false表示該倉庫是否爲下載某種類型構件(發佈版,快照版)開啓。 -->  
  20.   
  21.       <enabled>false</enabled>  
  22.   
  23.       <!--該元素指定更新發生的頻率。Maven會比較本地POM和遠程POM的時間戳。這裏的選項是:always(一直),daily(默認,每日),interval:X(這裏X是以分鐘爲單位的時間間隔),或者never(從不)。 -->  
  24.   
  25.      <updatePolicy>always</updatePolicy>  
  26.   
  27.       <!--當Maven驗證構件校驗文件失敗時該怎麼做-ignore(忽略),fail(失敗),或者warn(警告)。 -->  
  28.   
  29.      <checksumPolicy>warn</checksumPolicy>  
  30.   
  31.     </releases>  
  32.   
  33.     <!--如何處理遠程倉庫裏快照版本的下載。有了releases和snapshots這兩組配置,POM就可以在每個單獨的倉庫中,爲每種類型的構件採取不同的策略。例如,可能有人會決定只爲開發目的開啓對快照版本下載的支持。參見repositories/repository/releases元素 -->  
  34.   
  35.     <snapshots>  
  36.   
  37.       <enabled />  
  38.   
  39.       <updatePolicy />  
  40.   
  41.       <checksumPolicy />  
  42.   
  43.     </snapshots>  
  44.   
  45.     <!--遠程倉庫URL,按protocol://hostname/path形式 -->  
  46.   
  47.    <url>http://snapshots.maven.codehaus.org/maven2</url>  
  48.   
  49.     <!--用於定位和排序構件的倉庫佈局類型-可以是default(默認)或者legacy(遺留)。Maven 2爲其倉庫提供了一個默認的佈局;然而,Maven 1.x有一種不同的佈局。我們可以使用該元素指定佈局是default(默認)還是legacy(遺留)。 -->  
  50.   
  51.     <layout>default</layout>  
  52.   
  53.   </repository></repositories>  

pluginRepositories

作用:發現插件的遠程倉庫列表。
和repository類似,只是repository是管理jar包依賴的倉庫,pluginRepositories則是管理插件的倉庫。
maven插件是一種特殊類型的構件。由於這個原因,插件倉庫獨立於其它倉庫。pluginRepositories元素的結構和repositories元素的結構類似。每個pluginRepository元素指定一個Maven可以用來尋找新插件的遠程地址。

[html] view plain copy
  1. <pluginRepositories>  
  2.   
  3.   <!-- 包含需要連接到遠程插件倉庫的信息.參見profiles/profile/repositories/repository元素的說明 -->  
  4.   
  5.   <pluginRepository>  
  6.   
  7.     <releases>  
  8.   
  9.       <enabled />  
  10.   
  11.       <updatePolicy />  
  12.   
  13.       <checksumPolicy />  
  14.   
  15.     </releases>  
  16.   
  17.     <snapshots>  
  18.   
  19.       <enabled />  
  20.   
  21.       <updatePolicy />  
  22.   
  23.       <checksumPolicy />  
  24.   
  25.     </snapshots>  
  26.   
  27.     <id />  
  28.   
  29.     <name />  
  30.   
  31.     <url />  
  32.   
  33.     <layout />  
  34.   
  35.  </pluginRepository></pluginRepositories>  

ActiveProfiles

作用:手動激活profiles的列表,按照profile被應用的順序定義activeProfile。
該元素包含了一組activeProfile元素,每個activeProfile都含有一個profile id。任何在activeProfile中定義的profile id,不論環境設置如何,其對應的 profile都會被激活。如果沒有匹配的profile,則什麼都不會發生。
例如,env-test是一個activeProfile,則在pom.xml(或者profile.xml)中對應id的profile會被激活。如果運行過程中找不到這樣一個profile,Maven則會像往常一樣運行。

[html] view plain copy
  1. <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0                     https://maven.apache.org/xsd/settings-1.0.0.xsd">  
  2.   
  3.   ...  
  4.   
  5.   <activeProfiles>  
  6.   
  7.     <!-- 要激活的profile id -->  
  8.   
  9.    <activeProfile>env-test</activeProfile>  
  10.   
  11.   </activeProfiles>  
  12.   
  13.   ...</settings>  

四、Maven的Pom.xml

原文地址:http://blog.csdn.net/zhuxinhua/article/details/5788546

什麼是pom?
    pom作爲項目對象模型。通過xml表示maven項目,使用pom.xml來實現。主要描述了項目:包括配置文件;開發者需要遵循的規則,缺陷管理系統,組織和licenses,項目的url,項目的依賴性,以及其他所有的項目相關因素。

快速察看:

[html] view plain copy
 
 
  1. <project>  
  2.   <modelVersion>4.0.0</modelVersion>  
  3. <!--maven2.0必須是這樣寫,現在是maven2唯一支持的版本-->  
  4.   <!-- 基礎設置 -->  
  5.   <groupId>...</groupId>  
  6.   <artifactId>...</artifactId>  
  7.   <version>...</version>  
  8.   <packaging>...</packaging>  
  9.   <name>...</name>  
  10.   <url>...</url>  
  11.   <dependencies>...</dependencies>  
  12.   <parent>...</parent>  
  13.   <dependencyManagement>...</dependencyManagement>  
  14.   <modules>...</modules>  
  15.   <properties>...</properties>  
  16.   
  17.   <!--構建設置 -->  
  18.   <build>...</build>  
  19.   <reporting>...</reporting>  
  20.   
  21.   <!-- 更多項目信息 -->  
  22.   <name>...</name>  
  23.   <description>...</description>  
  24.   <url>...</url>  
  25.   <inceptionYear>...</inceptionYear>  
  26.   <licenses>...</licenses>  
  27.   <organization>...</organization>  
  28.   <developers>...</developers>  
  29.   <contributors>...</contributors>  
  30.   
  31.   <!-- 環境設置-->  
  32.   <issueManagement>...</issueManagement>  
  33.   <ciManagement>...</ciManagement>  
  34.   <mailingLists>...</mailingLists>   
  35.   <scm>...</scm>  
  36.   <prerequisites>...</prerequisites>  
  37.   <repositories>...</repositories>  
  38.   <pluginRepositories>...</pluginRepositories>  
  39.   <distributionManagement>...</distributionManagement>  
  40.   <profiles>...</profiles>  
  41. </project>  

基本內容:

POM包括了所有的項目信息

groupId:項目或者組織的唯一標誌,並且配置時生成路徑也是由此生成,如org.myproject.mojo生成的相對路徑爲:/org/myproject/mojo

artifactId:項目的通用名稱

version:項目的版本

packaging:打包機制,如pom,jar,maven-plugin,ejb,war,ear,rar,par

name:用戶描述項目的名稱,無關緊要的東西,可選

url:應該是隻是寫明開發團隊的網站,無關緊要,可選

classifer:分類

其中groupId,artifactId,version,packaging這四項組成了項目的唯一座標。一般情況下,前面三項就可以組成項目的唯一座標了。

 

POM關係:主要爲依賴,繼承,合成

依賴關係:

 

[html] view plain copy
 
 
  1. <dependencies>  
  2.     <dependency>  
  3.       <groupId>junit</groupId>  
  4.       <artifactId>junit</artifactId>  
  5.       <version>4.0</version>  
  6.       <type>jar</type>  
  7.       <scope>test</scope>  
  8.       <optional>true</optional>  
  9.     </dependency>  
  10.    
  11.     <dependency>  
  12.         <groupId>com.alibaba.china.shared</groupId>  
  13.         <artifactId>alibaba.apollo.webx</artifactId>  
  14.         <version>2.5.0</version>  
  15.         <exclusions>  
  16.           <exclusion>  
  17.             <artifactId>org.slf4j.slf4j-api</artifactId>  
  18.             <groupId>com.alibaba.external</groupId>  
  19.           </exclusion>  
  20.           ....  
  21.         </exclusions>  
  22. ......  
  23. </dependencies>  

其中groupId, artifactId, version這三個組合標示依賴的具體工程,而且這個依賴工程必需是maven中心包管理範圍內的,如果碰上非開源包,maven支持不了這個包,那麼則有三種方法處理:

1.本地安裝這個插件install plugin

例如:mvn install:intall-file -Dfile=non-maven-proj.jar -DgroupId=som.group -DartifactId=non-maven-proj -Dversion=1

2.創建自己的repositories並且部署這個包,使用類似上面的deploy:deploy-file命令,

3.設置scope爲system,並且指定系統路徑。

 

dependency裏屬性介紹:

type:默認爲jar類型,常用的類型有:jar,ejb-client,test-jar...,可設置plugins中的extensions值爲true後再增加新的類型,

scope:是用來指定當前包的依賴範圍,maven的依賴範圍

optional:設置指依賴是否可選,默認爲false,即子項目默認都繼承,爲true,則子項目必需顯示的引入,與dependencyManagement裏定義的依賴類似 。

exclusions:如果X需要A,A包含B依賴,那麼X可以聲明不要B依賴,只要在exclusions中聲明exclusion.

exclusion:是將B從依賴樹中刪除,如上配置,alibaba.apollo.webx不想使用com.alibaba.external  ,但是alibaba.apollo.webx是集成了com.alibaba.external,r所以就需要排除掉.

 

如果一個工程是parent或者aggregation(即mutil-module的)的,那麼必須在packing賦值爲pom,child工程從parent繼承的包括:dependencies,developers,contributors,plugin lists,reports lists,plugin execution with matching ids,plugin configuration

parent的使用方法如下:

 

[html] view plain copy
 
 
  1. <parent>   
  2.     <groupId>org.codehaus.mojo</groupId>   
  3.     <artifactId>my-parent</artifactId>   
  4.     <version>2.0</version>   
  5.     <relativePath>../my-parent</relativePath>   
  6. </parent>  

relativePath是可選的,maven會首先搜索這個地址,在搜索本地遠程repositories之前.

dependencyManagement:是用於幫助管理chidren的dependencies的。例如如果parent使用dependencyManagement定義了一個dependencyon junit:junit4.0,那麼它的children就可以只引用 groupId和artifactId,而version就可以通過parent來設置,這樣的好處就是可以集中管理依賴的詳情

 

modules:對於多模塊的project,outer-module沒有必需考慮inner-module的dependencies,當列出modules的時候,modules的順序是不重要的,因爲maven會自動根據依賴關係來拓撲排序,

modules例子如下 :

<module>my-project</module>

<module>other-project</module>

 

properties:是爲pom定義一些常量,在pom中的其它地方可以直接引用。

定義方式如下:

 

[html] view plain copy
 
 
  1. <properties>  
  2.       <file.encoding>UTF-8</file_encoding>  
  3.       <Java.source.version>1.5</java_source_version>  
  4.       <java.target.version>1.5</java_target_version>  
  5. </properties>  

使用方式 如下 :

${file.encoding}

 

還可以使用project.xx引用pom裏定義的其它屬性:如$(project.version} 

 

build設置:

defaultGoal:默認的目標,必須跟命令行上的參數相同,如:jar:jar,或者與時期parse相同,例如install

directory:指定build target目標的目錄,默認爲$(basedir}/target,即項目根目錄下的target

finalName:指定去掉後綴的工程名字,例如:默認爲${artifactId}-${version}

filters:用於定義指定filter屬性的位置,例如filter元素賦值filters/filter1.properties,那麼這個文件裏面就可以定義name=value對,這個name=value對的值就可以在工程pom中通過${name}引用,默認的filter目錄是${basedir}/src/main/fiters/

resources:描述工程中資源的位置 

 

[html] view plain copy
 
 
  1. <resource>   
  2.         <targetPath>META-INF/plexus</targetPath>   
  3.         <filtering>false</filtering>   
  4.         <directory>${basedir}/src/main/plexus</directory>   
  5.         <includes>   
  6.           <include>configuration.xml</include>   
  7.         </includes>   
  8.         <excludes>   
  9.           <exclude>**/*.properties</exclude>   
  10.         </excludes>   
  11. </resource>  

targetPath:指定build資源到哪個目錄,默認是base directory

filtering:指定是否將filter文件(即上面說的filters裏定義的*.property文件)的變量值在這個resource文件有效,例如上面就指定那些變量值在configuration文件無效。

directory:指定屬性文件的目錄,build的過程需要找到它,並且將其放到targetPath下,默認的directory是${basedir}/src/main/resources

includes:指定包含文件的patterns,符合樣式並且在directory目錄下的文件將會包含進project的資源文件。

excludes:指定不包含在內的patterns,如果inclues與excludes有衝突,那麼excludes勝利,那些符合衝突的樣式的文件是不會包含進來的。

testResources:這個模塊包含測試資源元素,其內容定義與resources類似,不同的一點是默認的測試資源路徑是${basedir}/src/test/resources,測試資源是不部署的。

 

plugins配置:

 

[html] view plain copy
 
 
  1. <plugin>   
  2.         <groupId>org.apache.maven.plugins</groupId>   
  3.         <artifactId>maven-jar-plugin</artifactId>   
  4.         <version>2.0</version>   
  5.         <extensions>false</extensions>   
  6.         <inherited>true</inherited>   
  7.         <configuration>   
  8.           <classifier>test</classifier>   
  9.         </configuration>   
  10.         <dependencies>...</dependencies>   
  11.         <executions>...</executions>   
  12. </plugin>  

extensions:true or false, 決定是否要load這個plugin的extensions,默認爲true.

inherited:是否讓子pom繼承,ture or false 默認爲true.

configuration:通常用於私有不開源的plugin,不能夠詳細瞭解plugin的內部工作原理,但使plugin滿足的properties

dependencies:與pom基礎的dependencies的結構和功能都相同,只是plugin的dependencies用於plugin,而pom的denpendencies用於項目本身。在plugin的dependencies主要用於改變plugin原來的dependencies,例如排除一些用不到的dependency或者修改dependency的版本等,詳細請看pom的denpendencies.

executions:plugin也有很多個目標,每個目標具有不同的配置,executions就是設定plugin的目標,

 

[html] view plain copy
 
 
  1. <execution>   
  2.             <id>echodir</id>   
  3.             <goals>   
  4.               <goal>run</goal>   
  5.             </goals>   
  6.             <phase>verify</phase>   
  7.             <inherited>false</inherited>   
  8.             <configuration>   
  9.               <tasks>   
  10.                 <echo>Build Dir: ${project.build.directory}</echo>   
  11.               </tasks>   
  12.             </configuration>   
  13. </execution>   

id:標識符

goals:裏面列出一系列的goals元素,例如上面的run goal

phase:聲明goals執行的時期,例如:verify

inherited:是否傳遞execution到子pom裏。

configuration:設置execution下列表的goals的設置,而不是plugin所有的goals的設置

 

pluginManagement配置:

pluginManagement的作用類似於denpendencyManagement,只是denpendencyManagement是用於管理項目jar包依賴,pluginManagement是用於管理plugin。與pom build裏的plugins區別是,這裏的plugin是列出來,然後讓子pom來決定是否引用。

例如:

 

[html] view plain copy
 
 
  1. <pluginManagement>   
  2.       <plugins>   
  3.         <plugin>   
  4.           <groupId>org.apache.maven.plugins</groupId>   
  5.           <artifactId>maven-jar-plugin</artifactId>   
  6.           <version>2.2</version>   
  7.           <executions>   
  8.             <execution>   
  9.               <id>pre-process-classes</id>   
  10.               <phase>compile</phase>   
  11.               <goals>   
  12.                 <goal>jar</goal>   
  13.               </goals>   
  14.               <configuration>   
  15.                 <classifier>pre-process</classifier>   
  16.               </configuration>   
  17.             </execution>   
  18.           </executions>   
  19.         </plugin>   
  20.       </plugins>   
  21. </pluginManagement>   



子pom引用方法: 
在pom的build裏的plugins引用: 

[html] view plain copy
 
 
  1. <plugins>   
  2.   <plugin>   
  3.     <groupId>org.apache.maven.plugins</groupId>   
  4.     <artifactId>maven-jar-plugin</artifactId>   
  5.   </plugin>   
  6. </plugins>  

build裏的directories:

 

[html] view plain copy
 
 
  1. <sourceDirectory>${basedir}/src/main/java</sourceDirectory>   
  2. <scriptSourceDirectory>${basedir}/src/main/scripts</scriptSourceDirectory>   
  3. <testSourceDirectory>${basedir}/src/test/java</testSourceDirectory>   
  4. <outputDirectory>${basedir}/target/classes</outputDirectory>   
  5. <testOutputDirectory>${basedir}/target/test-classes</testOutputDirectory>  

這幾個元素只在parent build element裏面定義,他們設置多種路徑結構,他們並不在profile裏,所以不能通過profile來修改

 

build 裏面的Extensions: 
它們是一系列build過程中要使用的產品,他們會包含在running bulid‘s classpath裏面。他們可以開啓extensions,也可以通過提供條件來激活plugins。簡單來講,extensions是在build過程被激活的產品 

[html] view plain copy
 
 
  1. <extensions>   
  2.   <extension>   
  3.     <groupId>org.apache.maven.wagon</groupId>   
  4.     <artifactId>wagon-ftp</artifactId>   
  5.     <version>1.0-alpha-3</version>   
  6.   </extension>   
  7. </extensions>   

reporting設置:

reporting包含site生成階段的一些元素,某些maven plugin可以生成reports並且在reporting下配置。例如javadoc,maven site等,在reporting下配置的report plugin的方法與build幾乎一樣,最不同的是build的plugin goals在executions下設置,而reporting的configures goals在reporttest。

excludeDefaults:是否排除site generator默認產生的reports

outputDirectory,默認的dir變成:${basedir}/target/site

report sets:設置execution goals,相當於build裏面的executions,不同的是不能夠bind a report to another phase,只能夠是site

 

[html] view plain copy
 
 
  1. <reporting>   
  2.     <plugins>   
  3.       <plugin>   
  4.         ...   
  5.         <reportSets>   
  6.           <reportSet>   
  7.             <id>sunlink</id>   
  8.             <reports>   
  9.               <report>javadoc</report>   
  10.             </reports>   
  11.             <inherited>true</inherited>   
  12.             <configuration>   
  13.               <links>   
  14.                 <link>http://java.sun.com/j2se/1.5.0/docs/api/</link>   
  15.               </links>   
  16.             </configuration>   
  17.           </reportSet>   
  18.         </reportSets>   
  19.       </plugin>   
  20.     </plugins>   
  21. </reporting>   

reporting裏面的reportSets和build裏面的executions的作用都是控制pom的不同粒度去控制build的過程,我們不單要配置plugins,還要配置那些plugins單獨的goals。

更多項目信息:

name:項目除了artifactId外,可以定義多個名稱
description: 項目描述
url: 項目url
inceptionYear:創始年份

Licenses

[html] view plain copy
 
 
  1. <licenses>  
  2.   <license>  
  3.     <name>Apache 2</name>  
  4.     <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>  
  5.     <distribution>repo</distribution>  
  6.     <comments>A business-friendly OSS license</comments>  
  7.   </license>  
  8. </licenses>  

列出本工程直接的licenses,而不要列出dependencies的licenses

配置組織信息:

[html] view plain copy
 
 
  1. <organization>  
  2.   <name>Codehaus Mojo</name>  
  3.   <url>http://mojo.codehaus.org</url>  
  4. </organization>  

很多工程都受到某些組織運行,這裏設置基本信息

 

配置開發者信息:

例如:一個開發者可以有多個roles,properties是 

[html] view plain copy
 
 
  1. <developers>  
  2.     <developer>  
  3.       <id>eric</id>  
  4.       <name>Eric</name>  
  5.       <email>[email protected]</email>  
  6.       <url>http://eric.propellors.NET</url>  
  7.       <organization>Codehaus</organization>  
  8.       <organizationUrl>http://mojo.codehaus.org</organizationUrl>  
  9.       <roles>  
  10.         <role>architect</role>  
  11.         <role>developer</role>  
  12.       </roles>  
  13.       <timezone>-6</timezone>  
  14.       <properties>  
  15.         <picUrl>http://tinyurl.com/prv4t</picUrl>  
  16.       </properties>  
  17.     </developer>  
  18.   </developers>  
  19.    

環境設置:

issueManagement:bug跟蹤管理系統,定義defect tracking system缺陷跟蹤系統,比如有(bugzilla,testtrack,clearquest等).

例如:

 

[html] view plain copy
 
 
  1. <issueManagement>   
  2.   <system>Bugzilla</system>   
  3.   <url>http://127.0.0.1/bugzilla/</url>   
  4. </issueManagement>   

倉庫:

Repositories:pom裏面的倉庫與setting.xml裏的倉庫功能是一樣的。主要的區別在於,pom裏的倉庫是個性化的。比如一家大公司裏的setting文件是公用 的,所有項目都用一個setting文件,但各個子項目卻會引用不同的第三方庫,所以就需要在pom裏設置自己需要的倉庫地址。

repositories:要成爲maven2的repository artifact,必須具有pom文件在$BASE_REPO/groupId/artifactId/version/artifactId-version.pom 
BASE_REPO可以是本地,也可以是遠程的。repository元素就是聲明那些去查找的repositories 
默認的central Maven repository在http://repo1.maven.org/maven2/

 

[html] view plain copy
 
 
  1. <repositories>   
  2.     <repository>   
  3.       <releases>   
  4.         <enabled>false</enabled>   
  5.         <updatePolicy>always</updatePolicy>   
  6.         <checksumPolicy>warn</checksumPolicy>   
  7.       </releases>   
  8.       <snapshots>   
  9.         <enabled>true</enabled>   
  10.         <updatePolicy>never</updatePolicy>   
  11.         <checksumPolicy>fail</checksumPolicy>   
  12.       </snapshots>   
  13.       <id>codehausSnapshots</id>   
  14.       <name>Codehaus Snapshots</name>   
  15.       <url>http://snapshots.maven.codehaus.org/maven2</url>   
  16.       <layout>default</layout>   
  17.     </repository>   
  18. </repositories>   

release和snapshots:是artifact的兩種policies,pom可以選擇那種政策有效。 
enable:本別指定兩種類型是否可用,true or false 
updatePolicy:說明更新發生的頻率always 或者 never 或者 daily(默認的)或者 interval:X(X是分鐘數) 

checksumPolicy:當Maven的部署文件到倉庫中,它也部署了相應的校驗和文件。您可以選擇忽略,失敗,或缺少或不正確的校驗和警告。

layout:maven1.x與maven2有不同的layout,所以可以聲明爲default或者是legacy(遺留方式maven1.x)。

 

插件倉庫:

pluginRepositories:與Repositories具有類似的結構,只是Repositories是dependencies的home,而這個是plugins 的home。

 

分發管理:

distributionManagement :管理distribution和supporting files。 

downloadUrl:是其他項目爲了抓取本項目的pom’s artifact而指定的url,就是說告訴pom upload的地址也就是別人可以下載的地址。 
status:這裏的狀態不要受到我們的設置,maven會自動設置project的狀態,有效的值:none:沒有聲明狀態,pom默認的;converted:本project是管理員從原先的maven版本convert到maven2的;partner:以前叫做synched,意思是與partner repository已經進行了同步;deployed:至今爲止最經常的狀態,意思是製品是從maven2 instance部署的,人工在命令行deploy的就會得到這個;verified:本製品已經經過驗證,也就是已經定下來了最終版。 
repository:聲明deploy過程中current project會如何變成repository,說明部署到repository的信息。 

[html] view plain copy
 
 
  1. <repository>   
  2.   <uniqueVersion>false</uniqueVersion>   
  3.   <id>corp1</id>   
  4.   <name>Corporate Repository</name>   
  5.   <url>scp://repo1/maven2</url>   
  6.   <layout>default</layout>   
  7. </repository>   
  8. <snapshotRepository>   
  9.   <uniqueVersion>true</uniqueVersion>   
  10.   <id>propSnap</id>   
  11.   <name>Propellors Snapshots</name>   
  12.   <url>sftp://propellers.Net/maven</url>   
  13.   <layout>legacy</layout>   
  14. </snapshotRepository>   

id, name::唯一性的id,和可讀性的name 
uniqueVersion:指定是否產生一個唯一性的version number還是使用address裏的其中version部分。true or false 
url:說明location和transport protocol 
layout:default或者legacy

 

profiles:pom4.0的一個新特性就是具有根據environment來修改設置的能力

它包含可選的activation(profile的觸發器)和一系列的changes。例如test過程可能會指向不同的數據庫(相對最終的deployment)或者不同的dependencies或者不同的repositories,並且是根據不同的JDK來改變的。那麼結構如下: 

[html] view plain copy
 
 
  1. <profiles>   
  2.   <profile>   
  3.     <id>test</id>   
  4.     <activation>...</activation>   
  5.     <build>...</build>   
  6.     <modules>...</modules>   
  7.     <repositories>...</repositories>   
  8.     <pluginRepositories>...</pluginRepositories>   
  9.     <dependencies>...</dependencies>   
  10.     <reporting>...</reporting>   
  11.     <dependencyManagement>...</dependencyManagement>   
  12.     <distributionManagement>...</distributionManagement>   
  13.   </profile>   
  14. </profiles>   

Activation: 
觸發這個profile的條件配置如下例:(只需要其中一個成立就可以激活profile,如果第一個條件滿足了,那麼後面就不會在進行匹配。 

[html] view plain copy
 
 
  1. <profile>   
  2.   <id>test</id>   
  3.   <activation>   
  4.     <activeByDefault>false</activeByDefault>   
  5.     <jdk>1.5</jdk>   
  6.     <os>   
  7.       <name>Windows XP</name>   
  8.       <family>Windows</family>   
  9.       <arch>x86</arch>   
  10.       <version>5.1.2600</version>   
  11.     </os>   
  12.     <property>   
  13.       <name>mavenVersion</name>   
  14.       <value>2.0.3</value>   
  15.     </property>   
  16.     <file>   
  17.       <exists>${basedir}/file2.properties</exists>   
  18.       <missing>${basedir}/file1.properties</missing>   
  19.     </file>   
  20.   </activation>   

激活profile的方法有多個:setting文件的activeProfile元素明確指定激活的profile的ID,在命令行上明確激活Profile用-P flag 參數 

查看某個build會激活的profile列表可以用:mvn help:active-profiles 

五、Maven私服

 

(一)nexus安裝

1. 安裝nexus

下載nexus至本地目錄

在命令提示符下,進入本地資源目錄,敲入命令

nexus install

如有下列提示則證明安裝成功。

如果想卸載,命令爲:

nexus uninstall


2. 啓動nexus

在服務中找到nexus ,右鍵啓動

3. 進入nexus 

打開瀏覽器輸入地址:http://localhost:8081/nexus

點擊右上角的Log In  ,在彈出窗口中輸入username爲admin ,password爲admin123 ,點擊Log In按鈕

(二)構建索引

剛剛安裝好的nexus是無法直接搜索到jar包的,必須下載索引纔可以正常使用。

我們到中央倉庫地址 http://repo.maven.apache.org/maven2/.index/下載以下兩個索引壓縮文件



這兩個文件一般在此頁面的最後的位置,另外還需要在網上下載一個名爲indexer-cli-5.1.1.jar 的jar包 

將以下三個文件放入一個文件夾

cmd執行:

java -jar indexer-cli-5.1.1.jar -u nexus-maven-repository-index.gz -d indexer

執行後生成目錄indexer,目錄下的文件是索引文件,如下圖

將D:\nexus-2.12.0-01-bundle\sonatype-work\nexus\indexer\central-ctx 目錄下的文件刪除,將上邊的索引文件拷貝到此目錄下。

拷貝後重新啓動nexus,然後進入nexus搜索jar包發現可以進行搜索了。

(三)倉庫類型

1、hosted:宿主倉庫,自己項目的jar要放到hosted類型的倉庫中。

2、proxy:代理倉庫,代理中央倉庫

3、virtual:虛擬倉庫,存儲了maven1的構件,一般不用。

4、group:倉庫組,將一些倉庫組成一個組,自己項目連接倉庫組去下載jar包。

       平常開發中都去連接倉庫組,倉庫組中包括:hosted、proxy等。

(四)鏡像配置

nexus已經安裝好了,我們要開始使用它了。現在我們需要在maven的配置文件settings.xml中配置鏡像,讓maven找私服,而不是直接到中央倉庫下載。

打開maven的配置文件settings.xml,添加如下配置:

<mirrors>    

    <mirror>

      <id>nexus</id>

      <mirrorOf>*</mirrorOf>

      <url>http://localhost:8081/nexus/content/groups/public/</url>

    </mirror>

</mirrors>


mirrors爲鏡像組,可以配置多個mirror(鏡像),我們這裏配置的是nexus中的中央代理倉庫。

配置好後,我們需要創建一個項目來測試一下,看看是不是可以通過nexus來下載jar包。

測試步驟:

創建maven工程(jar),在pom.xml中添加依賴,觀察nexus中是否下載了相應的jar包,目錄爲 nexus-2.12.0-01-bundle\sonatype-work\nexus\storage\central

(五)發佈自己的工程jar到nexus

1. 創建maven工程itcastutil (jar) 

座標信息:  Group Id :cn.itcast.util  

           Artifact Id: itcastutil

創建包cn.itcast.util

創建類DateUtil 

package cn.itcast.util;

 

import java.util.Calendar;

import java.util.Date;

/**

 日期工具類

 @author Administrator

 *

 */

publicclass DateUtil {

    

    /**

     * 獲取當前年份

     * @return

     */

    publicstaticint getYear(){

        

        Calendar calendar=Calendar.getInstance();

        calendar.setTime(new Date());

        return  calendar.get(Calendar.YEAR);

    }

}

在pom.xml中引入如下配置信息 

<distributionManagement>

    <repository>

        <id>releases</id>

        <url>

             http://localhost:8081/nexus/content/repositories/releases/

       </url>

    </repository>

    <snapshotRepository>

        <id>snapshots</id>

        <url>

            http://localhost:8081/nexus/content/repositories/snapshots/

        </url>

    </snapshotRepository>

  </distributionManagement>


2. 在Maven中配置

將以下配置信息放入maven的settings.xml中

  <servers>

    <server>

      <id>releases</id>

      <username>admin</username>

      <password>admin123</password>

    </server>   

    <server>

      <id>snapshots</id>

      <username>admin</username>

      <password>admin123</password>

    </server>

  </servers>

注意:settings.xml 配置 <id>和 pom.xml<id> 對應!

然後執行命令  deploy  ,有如下提示表示部署成功

我們在私服中進行查詢,發現可以查到剛纔我們上傳的jar

(六)發佈第三方的工程jar到nexus

有很多jar包由於版權等問題,並不會出現在中央倉庫中,比如oracle的驅動,那如何將這類jar包放在nexus中呢?我們看下列操作步驟:

(1)選擇左側菜單的Repositories,  在Repositories窗口中選擇3rd party

(2)在3rd party窗口中選擇Artifact Upload 

(3)在Artifact Upload選項卡中填寫座標,並上傳jar包。

 

上傳jar包選擇oracle的驅動。

填寫座標

有下列提示則爲上傳成功

上傳成功後可以在3rd party庫中找到此jar包

(七)從nexus中下載jar包

  1、對於所有項目,設置setting.xml

[html] view plain copy
  1. <profile>       
  2.     <!--profile的id-->    
  3.    <id>dev</id>       
  4.     <repositories>       
  5.       <repository>      
  6.         <!--倉庫id,repositories可以配置多個倉庫,保證id不重複-->    
  7.         <id>nexus</id>       
  8.         <!--倉庫地址,即nexus倉庫組的地址-->    
  9.         <url>http://localhost:8081/nexus/content/groups/public/</url>       
  10.         <!--是否下載releases構件-->    
  11.         <releases>       
  12.           <enabled>true</enabled>       
  13.         </releases>       
  14.         <!--是否下載snapshots構件-->    
  15.         <snapshots>       
  16.           <enabled>true</enabled>       
  17.         </snapshots>       
  18.       </repository>       
  19.     </repositories>      
  20.      <pluginRepositories>      
  21.         <!-- 插件倉庫,maven的運行依賴插件,也需要從私服下載插件 -->    
  22.         <pluginRepository>      
  23.             <!-- 插件倉庫的id不允許重複,如果重複後邊配置會覆蓋前邊 -->    
  24.             <id>public</id>      
  25.             <name>Public Repositories</name>      
  26.             <url>http://localhost:8081/nexus/content/groups/public/</url>      
  27.         </pluginRepository>      
  28.     </pluginRepositories>      
  29.   </profile>      

  2、對於單個項目,設置pom.xml

 

[html] view plain copy
  1. <repositories>    
  2.     <repository>    
  3.       <releases>    
  4.         <enabled>true</enabled>    
  5.       </releases>    
  6.       <snapshots>    
  7.         <enabled>true</enabled>    
  8.       </snapshots>    
  9.       <id>nexus</id>    
  10.       <url>http://localhost:8081/nexus/content/groups/public/</url>    
  11.     </repository>    
  12.     <repository>    
  13.       <snapshots>    
  14.         <enabled>false</enabled>    
  15.       </snapshots>    
  16.       <id>central</id>    
  17.       <name>Central Repository</name>    
  18.       <url>https://repo.maven.apache.org/maven2</url>    
  19.     </repository>    
  20.   </repositories>    
  21.   <pluginRepositories>    
  22.     <pluginRepository>    
  23.       <id>public</id>    
  24.       <name>Public Repositories</name>    
  25.       <url>http://localhost:8081/nexus/content/groups/public/</url>    
  26.     </pluginRepository>    
  27.     <pluginRepository>    
  28.       <releases>    
  29.         <updatePolicy>never</updatePolicy>    
  30.       </releases>    
  31.       <snapshots>    
  32.         <enabled>false</enabled>    
  33.       </snapshots>    
  34.       <id>central</id>    
  35.       <name>Central Repository</name>    
  36.       <url>https://repo.maven.apache.org/maven2</url>    
  37.     </pluginRepository>    
  38.   </pluginRepositories>    

六、Maven常見問題

http://blog.csdn.net/heweirun_2014/article/details/46558329

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章