1. 程式人生 > >maven系列--settings.xml

maven系列--settings.xml

安裝maven,會有一份settings.xml檔案,我們可以理解這份檔案就是用來管理本地maven的一系列東西的。這裡我先貼出一份完整的原始xml。
<?xml version="1.0" encoding="UTF-8"?>

<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 http://maven.apache.org/xsd/settings-1.0.0.xsd">
	<!-- 該值表示構建系統本地倉庫的路徑。其預設值為 ${user.home}/.m2/repository 。 -->
	<localRepository>D:\repository</localRepository>
	<!-- 該元素包含一個 pluginGroup 元素列表,每個子元素包含了一個 groupId 。當我們使用某個外掛,並且沒有在命令列為其提供 
		groupId 的時候, Maven就會使用該列 表。預設情況下該列表包含了 org.apache.maven.plugins 。 -->
	<pluginGroups></pluginGroups>
	<!-- 代理 -->
	<proxies></proxies>
	<!-- POM中的 distributionManagement 元素定義了部署的倉庫。然而,一些設定如安全證書不應該和 pom.xml 一起分發。這種型別的資訊應該存在 
		於構建伺服器上的 settings.xml 檔案中。 -->
	<servers></servers>
	<!-- 映象 -->
	<mirrors></mirrors>
	<!-- settings.xml 中的 profile 元素是 pom.xml 中 profile 元素的裁剪版本。它包含了 activation , repositories , pluginRepositories 
	和 properties 元素。這裡的profile元素只包含這四個子元素是因為這裡只關心構建系統這個整體(這正是 settings.xml 檔案的角色定位),而非單
	 獨的專案物件模型設定。如果一個settings中的profile被啟用,它的值會覆蓋任何其它定義在POM中或者profile.xml中的帶有相同id的profile。 -->
	<profiles></profiles>
	<!-- 簡單值 -->
	<!-- 如果Maven需要和使用者互動以獲得輸入,則設定成 true ,反之則應為 false 。預設為 true 。 -->
	<interactiveMode>true</interactiveMode>
	<!-- 如果需要讓Maven使用檔案  ${user.home}/.m2/plugin-registry.xml 來管理外掛版本,則設為 true 。預設為 false 。 -->
	<usePluginRegistry>false</usePluginRegistry>
	<!-- 如果構建系統需要在離線模式下執行,則為 true ,預設為 false 。當由於網路設定原因或者安全因素,構建伺服器不能連線遠端倉庫的時候,
	該配置就十分有用。 -->
	<offline>false</offline>

</settings>

當Maven執行過程中的各種配置,例如pom.xml,不想繫結到一個固定的project或者要分配給使用者時,我們使用settings.xml中的settings元素來確定這些配置。這包含了本地倉庫位置,遠端倉庫伺服器以及認證資訊等。

  • settings.xml存在於兩個地方:
1.安裝的地方:$M2_HOME/conf/settings.xml
2.使用者的目錄:${user.home}/.m2/settings.xml

注意:前者又被叫做全域性配置,後者被稱為使用者配置。如果兩者都存在,它們的內容將被合併,並且使用者範圍的settings.xml優先。
  • 伺服器
用來下載和部署的倉庫是用POM中的repositories和distributionManagement元素來定義的。但是某些配置例如username和password就不應該隨著pom.xml來分配了。這種型別的資訊應該儲存在構建伺服器中的settings.xml中。
<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
            http://maven.apache.org/xsd/settings-1.0.0.xsd">
    ...
    <servers>
    <server>
    <id>server001</id>
    <username>my_login</username>
    <password>my_password</password>
    <privateKey>${user.home}/.ssh/id_dsa</privateKey>
    <passphrase>some_passphrase</passphrase>
    <filePermissions>664</filePermissions>
    <directoryPermissions>775</directoryPermissions>
    <configuration></configuration>
    </server>
    </servers>
    ...
</settings>

id:這是Server的ID(不是登入進來的user),與Maven想要連線上的repository/mirror中的id元素相匹配。
username,password:這兩個元素成對出現,表示連線這個server需要驗證username和password。
privateKey,passphrase:與前兩個元素一樣,這兩個成對出現,分別指向了一個私鑰(預設的是${user.home}/.ssh/id_dsa)和一個passphrase。passphrase和password元素可能在將來被客觀化,但是現在必須以文字形式在settings.xml中設定。
filePermissions,directoryPermissions:當一個倉庫檔案或者目錄在部署階段被建立的時候,就必須用到許可權許可。他們合法的值是三個數字,就像*nix中的檔案許可權,例如:664,775.
注意:如果你使用了一個私鑰來登入server,那麼password元素必須被省略,否則私鑰將被忽視。

密碼加密:一個新特徵:伺服器password和passphrase加密已經被升到2.1.0+

  • 映象
<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
            http://maven.apache.org/xsd/settings-1.0.0.xsd">
    ...
    <mirrors>
        <mirror>
            <id>planetmirror.com</id>
            <name>PlanetMirror Australia</name>
            <url>http://downloads.planetmirror.com/pub/maven2</url>
            <mirrorOf>central</mirrorOf>
        </mirror>
    </mirrors>
    ...
</settings>

id,name:唯一的映象標識和使用者友好的映象名稱。id被用來區分mirror元素,並且當連線時候被用來獲得相應的證書。
url:映象基本的URL,構建系統敬將使用這個URL來連線倉庫,而不是原來的倉庫URL。
mirrorOf:映象所包含的倉庫的Id。例如,指向Maven central倉庫的映象(http://repo1.maven.org/maven2/),設定這個元素為central。更多的高階對映例如repo1,repo2 或者*,!inhouse都是可以的。沒必要一定和mirror的id相匹配。

  • 代理
<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
            http://maven.apache.org/xsd/settings-1.0.0.xsd">
    ...
    <proxies>
        <proxy>
            <id>myproxy</id>
            <active>true</active>
            <protocol>http</protocol>
            <host>proxy.somewhere.com</host>
            <port>8080</port>
            <username>proxyuser</username>
            <password>somepassword</password>
            <nonProxyHosts>*.google.com|ibiblio.org</nonProxyHosts>
        </proxy>
    </proxies>
    ...
</settings>

id:proxy的唯一標識,用來區別proxy元素。
active:當proxy被啟用的時候為true。當申明的代理很多的時候,這個很有用,但是同一時間僅有一個被啟用。
protocol,host,port:代理地址protocol://host:port的分散形式。
username,password:兩個元素成對出現,提供連線proxy伺服器時的認證。
nonProxyHosts:這裡列出了不需要使用代理的hosts。列表的分隔符是proxy伺服器想要的型別。上面例子使用了pipe分隔符,逗號分隔符也比較通用。

  • 配置檔案

settings.xml中的profile是pom.xml中的profile的簡潔形式。它包含了啟用(activation),倉庫(repositories),外掛倉庫(pluginRepositories)和屬性(properties)元素。profile元素僅包含這四個元素是因為他們涉及到整個的構建系統,而不是個別的POM配置。如果settings中的profile被啟用,那麼它的值將過載POM或者profiles.xml中的任何相等ID的profiles。

  • 啟用(activation)

activations是profile的關鍵,就像POM中的profiles,profile的能力在於它在特定情況下可以修改一些值。而這些情況是通過activation來指定的。

<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
            http://maven.apache.org/xsd/settings-1.0.0.xsd">
    ...
    <profiles>
        <profile>
            <id>test</id>
            <activation>
                <activeByDefault>false</activeByDefault>
                <jdk>1.5</jdk>
                <os>
                    <name>Windows XP</name>
                    <family>Windows</family>
                    <arch>x86</arch>
                    <version>5.1.2600</version>
                </os>
                <property>
                    <name>mavenVersion</name>
                    <value>2.0.3</value>
                </property>
                <file>
                    <exists>${basedir}/file2.properties</exists>
                    <missing>${basedir}/file1.properties</missing>
                </file>
            </activation>
            ...
        </profile>
    </profiles>
    ...
</settings>

如果所有指定的條件都達到了,那麼,activation就被觸發,而且不需要一次性全部達到。
jdk:在jdk元素中,activation有一個內建的,java版本檢測。如果檢測到jdk版本與期待的一樣,那麼就啟用。在上面的例子中,1.5.0_06是滿足的。
os:os元素可以定義一些上面所示的作業系統特定的屬性。
property:如果Maven檢測到相應的名值對的屬性,那麼,這個profile將被啟用。
file:如果給定的檔案存在,或者不存在那麼將啟用這個profile。
activation並不是唯一啟用profile的途徑。settings.xml中的activeProfile包含了profile的id。他們也可以通過命令列來顯式的啟用,例如-P test。
如果你想檢視在一個構建過程中有哪些profile會被啟用。就使用maven-help-plugin  mvn help:active-profiles
  • 屬性(properites)

Maven的屬性是值佔位符,就像Ant中的屬性。如果X是一個屬性的話,那麼它的值在POM中可以使用${X}來進行任意地方的訪問。他們來自於五種不同的風格,所有都可以從settings.xml檔案中訪問到。
1.env.X:使用“env.”字首將會返回當前的環境變數。例如${env.PATH}就是使用了$path環境變數。
2.project.X:一個點“.”分割的路徑,在POM中就是相關的元素的值。例如:
<project><version>1.0</version></project>就可以通過${project.version}來訪問。
3.settings.X:一個點“.”分割的路徑,在settings.xml中就是相對應的元素的值,例如:
<settings><offline>false</offline></settings>就可以通過${settings.offline}來訪問。
4.Java系統屬性:所有通過java.lang.System.getProperties()來訪問的屬性都可以像POM中的屬性一樣訪問,例如:${java.home}
5.X:被<properties/>或者外部檔案定義的屬性,值可以這樣訪問${someVar}

<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
            http://maven.apache.org/xsd/settings-1.0.0.xsd">
    ...
    <profiles>
        <profile>
            ...
            <properties>
                <user.install>${user.home}/our-project</user.install>
            </properties>
            ...
        </profile>
    </profiles>
    ...
</settings>

如果這個profile被啟用,那麼屬性${user.install}就可以被訪問了。
  • 倉庫(repositories)
倉庫是Maven用來構築構建系統的本地倉庫的遠端專案集合。它來自於被Maven叫做外掛和依賴的本地倉庫。不同的遠端倉庫包含不同的專案,當profile被啟用,他們就會需找匹配的release或者snapshot構件。
<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
            http://maven.apache.org/xsd/settings-1.0.0.xsd">
    ...
    <profiles>
        <profile>
        ...
        <repositories>
            <repository>
                <id>codehausSnapshots</id>
                <name>Codehaus Snapshots</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://snapshots.maven.codehaus.org/maven2</url>
                <layout>default</layout>
            </repository>
        </repositories>
        <pluginRepositories>
        ...
        </pluginRepositories>
        ...
        </profile>
    </profiles>
    ...
</settings>
releases,snapshots:這是各種構件的策略,release或者snapshot。因了這兩個集合,POM可以在單個的倉庫中不依賴於另外一個的策略而改變當前策略。例如:一個人可能只下載snapshot用來開發。

enable:true或者false,來標記倉庫是否為各自的型別啟用(release 或者 snapshot)。
updatePolicy:這個元素指明瞭更新的頻率。Maven會比較本地POM與遠端的時間戳。可選的專案為:always,daily,interval:X,nerver。
checksumPolicy:當Maven向倉庫部署檔案的時候,它也部署了相應的校驗和檔案。可選的為:ignore,fail,warn,或者不正確的校驗和。
layout:在上面描述倉庫的時候,我們提到他們有統一的佈局。這完全正確。使用這個來表明它是default還是legacy。


  • 外掛倉庫(plugin repositories)

倉庫包含了兩種重要型別的構件。第一種是用來做其他構件依賴的構件,這是在中央倉庫中的大多數外掛。另外一種型別的構件就是外掛。Maven的外掛本身就是一種特殊的構件。因此,外掛倉庫被從其他倉庫中分離出來。無論怎麼說,pluginRepositories元素模組的結構與repositories模組很相似。luginRepository元素指向一個可以找到新外掛的遠端地址。

  • 啟用配置(Active Profiles)
<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
            http://maven.apache.org/xsd/settings-1.0.0.xsd">
    ...
    <activeProfiles>
        <activeProfile>env-test</activeProfile>
    </activeProfiles>
</settings>
settings.xml最後一個謎題是activeProfiles元素。它包含一系列的activeProfile元素,每個都有一個profile id的值,任何profile id被定義到activeProfile的profile將被啟用,不管其他的環境設定怎麼樣。如果沒有匹配的profile被找到,那麼就什麼事情也不做。例如:如果env-test是一個activeProfile,一個在pom.xml或者profile.xml中的具有相應id的profile將被啟用。如果沒有這樣的profile被找到,就什麼事也不做,一切照常。

最後settings.xml總結:

一般情況下我們不需要修改settings.xml檔案裡面的內容,我們只是重新設定下這個檔案的位置,然後修改裡面的本地庫標籤(localRepository)。如果公司裡面不是連線的遠端公共倉庫,有自己的倉庫的話,就自己新建一個server就OK。

<localRepository>@[email protected]</localRepository>
    <server>
        <id>branchitech-maven</id>
        <username>@[email protected]</username>
        <password>@[email protected]</password>
    </server>


相關推薦

maven系列--settings.xml

安裝maven,會有一份settings.xml檔案,我們可以理解這份檔案就是用來管理本地maven的一系列東西的。這裡我先貼出一份完整的原始xml。<?xml version="1.0" encoding="UTF-8"?> <settings xml

MAVENmaven系列--pom.xml標簽詳解

als lex script 是否 mod resource ica 資源 bugzilla pom文件作為MAVEN中重要的配置文件,對於它的配置是相當重要。文件中包含了開發者需遵循的規則、缺陷管理系統、組織、licenses、項目信息、項目依賴性等。下面將重點介紹一下該

Mavensettings.xml文件

releases XML nexus release ali settings body private files <?xml version="1.0"?> <settings> <localRepository>D:\re

Mavensettings.xml文件各標簽含義

x文件 rev 擁有 $path eprof 更新 directory 屬性 地址 原文地址:http://www.cnblogs.com/jingmoxukong/p/6050172.html?utm_source=gold_browser_extension sett

maven配置settings.xml

<?xml version="1.0" encoding="UTF-8"?> <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSc

mavensettings.xml配置檔案詳解

一、Settings.xml基本資訊 Settings.xml是maven全域性引數的配置檔案,其中包含本地倉儲位置、遠端倉儲伺服器、認證資訊等等。 settings.xml檔案一般存在於兩個位置: 全域性配置(global settings): maven安裝目錄/conf/settings

maven新增settings.xml使用阿里雲倉庫

在~/.m2/repository目錄下新建settings.xml,內容如下: <mirrors> <!-- mirror | Specifies a repository mirror site to use instead of a given repository

Apache-mavensettings.xml中元素參考

Apache-maven中settings.xml檔案中的元素: <settings>:根元素 <localRepository>:本地倉庫 <interactiveMode>:是否與使用者互動,預設true <offline

分享一個快的飛起的mavensettings.xml檔案

<?xml version="1.0" encoding="UTF-8"?> <settings> <localRepository>/home/yizhen/.m

mavensettings.xml基本配置

1. 設定本地倉庫位置 <localRepository>E:\Repertory\RepMaven</localRepository> 2. 將阿里雲作為映

Maven配置settings.xml存檔記錄

配置阿里雲映象 <mirrors> <mirror> <id>alimaven</id> <mirrorOf>central</mirrorOf>

Maven 原生 settings.xml 說明【翻譯】

<?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agre

maven倉庫settings.xml檔案配置阿里雲下載

<?xml version="1.0" encoding="UTF-8"?>  <settings xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XM

MAVENmaven系列--pom.xml標籤詳解

pom檔案作為MAVEN中重要的配置檔案,對於它的配置是相當重要。檔案中包含了開發者需遵循的規則、缺陷管理系統、組織、licenses、專案資訊、專案依賴性等。下面將重點介紹一下該檔案的基本組成與功能。 標籤預覽 <project> <modelVersion>4.0.0<

Mavensettings.xml的配置

安裝配置好maven之後 http://blog.csdn.net/qq_22348309/article/details/68925131 則可以進行maven的settings配置了 1.首先

Maven full settings.xml

<?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements

mavensettings.xml

maven的映象以及倉庫位置配置(現成的settings.xml檔案) <?xml version="1.0" encoding="UTF-8"?> <settings xmlns="http://maven.apache.org/SETTINGS/1.0

配置Mavensettings.xml

在Maven中提供了一個settings.xml檔案來定義Maven的全域性環境資訊。這個檔案會存在於Maven的安裝目錄的conf子目錄下面,或者是使用者Home目錄的.m2子目錄下面。我們可以通過這個檔案來定義本地倉庫、遠端倉庫和聯網使用的代理資訊等。安裝好maven

阿里Maven倉庫 -- Settings.xml

國內連線maven官方的倉庫更新依賴庫,網速一般很慢,收集一些國內快速的maven倉庫映象以備用。 最新更新:2016年11月11日 18:05:40 阿里雲提供Maven私服,我把配置檔案貼一下,自己放在maven的conf下就行,setting.xml

mavensettings.xml配置詳解

maven的配置檔案settings.xml存在於兩個地方: 1.安裝的地方:${M2_HOME}/conf/settings.xml 2.使用者的目錄:${user.home}/.m2/settings.xml 前者又被叫做全域性配置,對作業系統的