1. 程式人生 > >Maven學習總結(34)——Maven settings.xml配置解讀

Maven學習總結(34)——Maven settings.xml配置解讀

第一步:看settings.xml的內容解釋
<!-- 
| #使用者級別,面向單個使用者配置,即每個使用者都可以自定義settings.xml供己方使用 
| This is the configuration file for Maven. It can be specified at two levels: 
| 1. User Level. This settings.xml file provides configuration for a single user, 
| |#其中的CLI選項指的是mvn操作命令的相關引數比如:mvn -v 
| and is normally provided in ${user.home}/.m2/settings.xml. 
| NOTE: This location can be overridden with the CLI option: 
| #全域性級別,即面向所有使用者同一配置 
| | -s /path/to/user/settings.xml 
| 2. Global Level. This settings.xml file provides configuration for all Maven 
| users on a machine (assuming they're all using the same Maven | installation). It's normally provided in 
| ${maven.home}/conf/settings.xml. | | NOTE: This location can be overridden with the CLI option: 
| | -gs /path/to/global/settings.xml 
| | The sections in this sample file are intended to give you a running start at 
| getting the most out of your Maven installation. Where appropriate, the default 
| values (values used when the setting is not specified) are provided. 
-->
明白settings.xml具有兩個級別:使用者級別和全域性級別
settings.xml配置檔案屬於maven的相關配置,配置的是其他工程專案pom.xml中的全域性配置
第二步:瞭解settings.xml標準引數結構
localRepository 本地倉庫配置路徑 interactiveMode 是否互動式輸入提醒。預設true offline 是否每次編譯部署時候需要聯網,預設false pluginGroups 外掛組合,供編譯時選用 proxies 代理配置,連線本地無法遠端的網路 servers 服務配置,上傳構件/連線倉庫所用 mirrors 替代有網路問題的repository的訪問站點 profiles 應用於不同環境的配置 activeProfiles 啟用指定的配置,與profile搭配使用
第三步:詳解以上的引數含義
localRepository 儲存遠端下載的構建的本地位置,採用絕對路徑,預設存放在${user.home}/.m2/repository
<!-- 
localRepository 
| The path to the local repository maven will use to store artifacts. 
| | Default: ${user.home}/.m2/repository
-->
interactiveMode 互動式節點配置,預設為true,設定為false則會在mvn執行中使用預設的引數設定互動性問題,建議設定為true
<!-- 
interactiveMode 
| This will determine whether maven prompts you when it needs input. If set to false, 
| maven will use a sensible default value, perhaps based on some other setting, for 
| the parameter in question. 
| | Default: true
-->
offline 是否每次的編譯部署都需要聯網,預設為false。如果構件不需要更新下載則可設定為true,提高build效率、成功率。
<!-- 
offline 
| Determines whether maven should attempt to connect to the network when executing a build. 
| This will have an effect on artifact downloads, artifact deployment, and others. 
| | Default: false
-->
pluginGroups 外掛組,預設包含maven外掛、mojo外掛,用於外掛在使用時沒有指定groupId時,這個列表就會被搜尋。
<!-- 
pluginGroups 
| This is a list of additional group identifiers that will be searched when resolving plugins by their prefix, i.e. 
| when invoking a command line like "mvn prefix:goal". Maven will automatically add the group identifiers 
| "org.apache.maven.plugins" and "org.codehaus.mojo" if these are not already contained in the list. 
-->
示例:mvn jetty run替代org.morbay.jetty:jetty-maven-plugin:run
<pluginGroups> 
<pluginGroup> org.mortbay.jetty </pluginGroup> 
</pluginGroups>
proxies 代理配置,預設第一個proxy配置起效
<!-- 
proxies 
| This is a list of proxies which can be used on this machine to connect to the network. 
| Unless otherwise specified (by system property or command-line switch), the first proxy 
| specification in this list marked as active will be used. 
--> 
<proxies> 
<!-- 
proxy 新增一個代理配置示例 
| Specification for one proxy, to be used in connecting to the network. 
| <proxy> 
<id>optional</id> 
<active>true</active> 
<protocol>http</protocol> 
<username>proxyuser</username> 
<password>proxypass</password> 
<host>proxy.host.net</host> 
<port>80</port> 
<nonProxyHosts>local.net|some.host.com</nonProxyHosts> 
</proxy> 
--> 
</proxies>
servers配置,上傳構件需要驗證的server配置,通過id來應用,與mirror/repository標籤的id相一致
<!-- 
servers 
| This is a list of authentication profiles, keyed by the server-id used within the system. 
| Authentication profiles can be used whenever maven must make a connection to a remote server. 
--> 
<servers> 
<!-- 
server 
| Specifies the authentication information to use when connecting to a particular server, identified by 
| a unique name within the system (referred to by the 'id' attribute below). 
| | NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are 
| used together. 
| <server> 
<id>deploymentRepo</id> 
<username>repouser</username> 
<password>repopwd</password> 
</server>
-->
<!-- Another sample, using keys to authenticate. 
<server> 
<id>siteServer</id> 
<privateKey>/path/to/private/key</privateKey> 
<passphrase>optional; leave empty if not used.</passphrase> 
</server> 
</servers>
mirrors 構建映象,替代有網路問題的repository的訪問站點,即當下載的maven構建有問題時可通過此配置替換原有的下載地址
<!-- 
mirrors 
| This is a list of mirrors to be used in downloading artifacts from remote repositories. 
| | It works like this: a POM may declare a repository to use in resolving certain artifacts. 
| However, this repository may have problems with heavy traffic at times, so people have mirrored 
| it to several places. | | That repository definition will have a unique id, so we can create a mirror reference for that 
| repository, to be used as an alternate download site. The mirror site will be the preferred 
| server for that repository. //central代表maven官網的中央倉庫 
-->
示例:
<mirror> 
<id>mirrorId</id> 
#指定替代的唯一倉庫id或者*代表所有都替代,支援多倉庫id,以逗號分隔 
<mirrorOf>repositoryId/*</mirrorOf>
<name>Human Readable Name for this Mirror.</name> 
#映象url 
<url>http://my.repository.com/repo/path</url>
</mirror>
profiles 多條件集合配置,用於不同條件下可應用不同的配置,其中也可配置自定義的repository/pluginRepository
| This profile example uses the JDK version to trigger activation, and provides a JDK-specific repo. 
<profile> 
#唯一id 
<id>jdk-1.4</id>
#在此配置的任何一個節點條件滿足則觸發此配置 
<activation>
<jdk>1.4</jdk> 
</activation> 
<repositories> 
<repository> 
#與server的id一致 
<id>jdk14</id>
<name>Repository for JDK 1.4 builds</name> 
#釋出版本 
<releases>
#不允許在釋出版本中尋找構件 
<enabled>false</enabled>
<updatePolicy>always</updatePolicy> 
<checksumPolicy>warn</checksumPolicy> 
</releases> 
#快照版本 
<snapshots>
#允許在快照版本中尋找構件 
<enabled>true</enabled>
<updatePolicy>never</updatePolicy> 
<checksumPolicy>fail</checksumPolicy> 
</snapshots> 
<url>http://www.myhost.com/maven/jdk14</url> 
<layout>default</layout> 
<snapshotPolicy>always</snapshotPolicy> 
</repository> 
</repositories>
</profile>
-->
activeProfiles 啟用相應的profile配置,可啟用多個配置
activeProfiles //List of profiles that are active for all builds. 
  <activeProfiles> 
<activeProfile>alwaysActiveProfile</activeProfile> 
<activeProfile>anotherAlwaysActiveProfile</activeProfile> 
  </activeProfiles>
distributionManagement(新增) 上傳構件
<distributionManagement> 
<repository> 
#對應釋出版本的server.id 
<id>release_server</id>
<url></url> 
</repository> 
<snapshotRepository> 
#對應快照版本的server.id
<id>snapshot_server</id>
<url></url> 
</snapshotRepository>
repositories(新增) 統一的下載倉庫
<repositories> 
<repository>
#對應server.id 
<id>snapshot</id> 
#下載構件的倉庫url 
<url></url>
<releases> 
#不支援釋出版本的查詢
<enabled>false</enabled>
</releases> 
<snapshots> 
#支援快照版本的查詢 
<enabled>true</enabled>
</snapshots> 
</repository> 
</repositories>
總結
mirror是下載構件的配置,其中的id屬性在其下載構件需要server驗證時候對應的server.id
mirror的優先順序高於repository
distributionManagement是用在上傳構件到遠端伺服器的配置,其中的repository配置中的id也對應server.id,畢竟上傳構件需要server驗證