如果在Eclipse中使用過Maven插件,想必會有這個經驗:配置settings.xml文件的路徑。
(資料圖片)
settings.xml文件是干什么的,為什么要配置它呢?從settings.xml的文件名就可以看出,它是用來設置maven參數的配置文件。并且,settings.xml是maven的全局配置文件。而pom.xml文件是所在項目的局部配置。Settings.xml中包含類似本地倉儲位置、修改遠程倉儲服務器(如阿里云的遠程倉庫)、認證信息等配置(比如對于某個遠程的倉庫需要用戶名和密碼的話也需要在這個setting.xml中進行配置)。
在setting.xml中配置阿里云的遠程倉庫:
alimaven * aliyun maven http://maven.aliyun.com/nexus/content/repositories/central/ repo1 central Human Readable Name for this Mirror. http://repo1.maven.org/maven2/ repo2 central Human Readable Name for this Mirror. http://repo2.maven.org/maven2/
settings.xml文件一般存在于兩個位置:全局配置: ${M2_HOME}/conf/settings.xml用戶配置: 用戶配置優先于全局配置。user.home/.m2/settings.xmlnote:用戶配置優先于全局配置。{user.home} 和和所有其他系統屬性只能在3.0+版本上使用。請注意windows和Linux使用變量的區別。
eclipse對于Maven工程中需要注意的是:局部配置優先于全局配置。配置優先級從高到低:pom.xml> user settings > global settings如果這些文件同時存在,在應用配置時,會合并它們的內容,如果有重復的配置,優先級高的配置會覆蓋優先級低的。
下面列舉了settings.xml
中的頂級元素
作用:該值表示構建系統本地倉庫的路徑。其默認值:~/.m2/repository。
${user.home}/.m2/repository
作用:表示maven是否需要和用戶交互以獲得輸入。如果maven需要和用戶交互以獲得輸入,則設置成true,反之則應為false。默認為true。
true
作用:maven是否需要使用plugin-registry.xml文件來管理插件版本。如果需要讓maven使用文件~/.m2/plugin-registry.xml來管理插件版本,則設為true。默認為false。
false
作用:表示maven是否需要在離線模式下運行。如果構建系統需要在離線模式下運行,則為true,默認為false。當由于網絡設置原因或者安全因素,構建服務器不能連接遠程倉庫的時候,該配置就十分有用。
false
作用:當插件的組織id(groupId)沒有顯式提供時,供搜尋插件組織Id(groupId)的列表。該元素包含一個pluginGroup元素列表,每個子元素包含了一個組織Id(groupId)。當我們使用某個插件,并且沒有在命令行為其提供組織Id(groupId)的時候,Maven就會使用該列表。默認情況下該列表包含了org.apache.maven.plugins
和org.codehaus.mojo
。
... org.codehaus.mojo ...
作用:一般,倉庫的下載和部署是在pom.xml文件中的repositories
和distributionManagement
元素中定義的。然而,一般類似用戶名、密碼(有些倉庫訪問是需要安全認證的)等信息不應該在pom.xml文件中配置,這些信息可以配置在settings.xml
中。
... server001 my_login my_password ${usr.home}/.ssh/id_dsa some_passphrase 664 775 ...
作用:為倉庫列表配置的下載鏡像列表。
... planetmirror.com PlanetMirror Australia http://downloads.planetmirror.com/pub/maven2 central ...
作用:用來配置不同的代理。
... myproxy true http proxy.somewhere.com 8080 proxyuser somepassword *.google.com|ibiblio.org ...
作用:根據環境參數來調整構建配置的列表。settings.xml
中的profile
元素是pom.xml
中profile
元素的裁剪版本。它包含了id
、activation
、repositories
、pluginRepositories
和 properties
元素。這里的profile元素只包含這五個子元素是因為這里只關心構建系統這個整體(這正是settings.xml文件的角色定位),而非單獨的項目對象模型設置。如果一個settings.xml
中的profile
被激活,它的值會覆蓋任何其它定義在pom.xml
中帶有相同id的profile
。
... test ...
作用:自動觸發profile
的條件邏輯。如pom.xml
中的profile
一樣,profile
的作用在于它能夠在某些特定的環境中自動使用某些特定的值;這些環境通過activation
元素指定。activation
元素并不是激活profile
的唯一方式。settings.xml
文件中的activeProfile
元素可以包含profile
的id
。profile
也可以通過在命令行,使用-P標記和逗號分隔的列表來顯式的激活(如,-P test)。
false 1.5 Windows XP Windows x86 5.1.2600 mavenVersion 2.0.3 ${basedir}/file2.properties ${basedir}/file1.properties
注:在maven工程的pom.xml所在目錄下執行mvn help:active-profiles
命令可以查看中央倉儲的profile是否在工程中生效。
作用:對應profile
的擴展屬性列表。maven屬性和ant中的屬性一樣,可以用來存放一些值。這些值可以在pom.xml
中的任何地方使用標記${X}
來使用,這里X是指屬性的名稱。屬性有五種不同的形式,并且都能在settings.xml文件中訪問。
${user.home}/our-project
注:如果在setting.xml中的profile被激活,則可以在pom.xml
中使用${user.install}。
作用:遠程倉庫列表,它是maven用來填充構建系統本地倉庫所使用的一組遠程倉庫。
codehausSnapshots Codehaus Snapshots false always warn http://snapshots.maven.codehaus.org/maven2 default
作用:發現插件的遠程倉庫列表。和repository
類似,只是repository
是管理jar包依賴的倉庫,pluginRepositories
則是管理插件的倉庫。maven插件是一種特殊類型的構件。由于這個原因,插件倉庫獨立于其它倉庫。pluginRepositories
元素的結構和repositories
元素的結構類似。每個pluginRepository
元素指定一個Maven可以用來尋找新插件的遠程地址。
作用:手動激活profiles的列表,按照profile
被應用的順序定義activeProfile
。該元素包含了一組activeProfile
元素,每個activeProfile
都含有一個profile id。任何在activeProfile
中定義的profile id,不論環境設置如何,其對應的 profile
都會被激活。如果沒有匹配的profile
,則什么都不會發生。例如,env-test是一個activeProfile,則在pom.xml(或者profile.xml)中對應id的profile會被激活。如果運行過程中找不到這樣一個profile,Maven則會像往常一樣運行。
... env-test ...
上一篇:世界最大的未解之謎 世界焦點
責任編輯: