Maven配置文件settings.xml

一、作用

# settings.xml是配置Maven全局參數的文件,包含本地倉庫、中央倉庫等
# settings.xml一般存在Maven的安裝目錄下的conf文件夾中

二、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">
  <localRepository/>
  <interactiveMode/>
  <offline/>
  <pluginGroups/>
  <servers/>
  <mirrors/>
  <proxies/>
  <profiles/>
  <activeProfiles/>
</settings>

1、localRepository

標識本地倉庫路徑。

# Default: ${user.home}/.m2/repository
# 可自己修改
<localRepository>D:\Java\Maven-3.6\maven_dependcies</localRepository>

2、interactiveMode

Maven是否需要和用戶交互

# Default: true
# 默認情況下是註釋掉的
<interactiveMode>true</interactiveMode>

3、offline

Maven在項目編譯和部署等操作時是否允許Maven聯網下載相關信息

# Default: false
<offline>false</offline>

4、pluginGroups

在pluginGroups下可以定義一系列的pluginGroup元素。表示當通過plugin的前綴來解析plugin的時候到哪裏尋找。pluginGroup元素指定的是plugin的groupId。默認情況下,Maven會自動把org.apache.maven.plugins和org.codehaus.mojo添加到pluginGroups下。

<pluginGroups>
    <!-- pluginGroup
     | Specifies a further group identifier to use for plugin lookup.
    <pluginGroup>com.your.plugins</pluginGroup>
    -->
</pluginGroups>

5、Proxies

配置代理列表,可在此計算機上用於連接到網絡。除非另有規定(通過系統屬性或命令行開關),否則第一個代理將使用此列表中標記爲活動的規範。

<proxies>
    <!-- proxy
     | Specification for one proxy, to be used in connecting to the network.
     |
    <proxy>
        <!-- ID,用於區分各個代理 -->
      <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>

6、servers

身份驗證配置文件列表,由系統中使用的服務器ID鍵入。只要Maven必須連接到遠程服務器,就可以使用身份驗證配置文件。指定連接到特定服務器時要使用的身份驗證信息,由系統中的唯一名稱(由下面的“id”屬性引用)。

注意:您應該指定用戶名/密碼或私鑰/密碼短語,因爲這些配對是一起使用

<!--配置服務端的一些設置。一些設置如安全證書不應該和pom.xml一起分發。這種類型的信息應該存在於構建服務器上的settings.xml文件中。 -->
<servers>
    <!--這是server的id(注意不是用戶登陸的id),該id與distributionManagement中repository元素的id相匹配。 -->
	<server>
		<id>maven-central</id>
		<username>zhangsan</username>
		<password>123456</password>
<!--鑑權時使用的私鑰位置。和前兩個元素類似,私鑰位置和私鑰密碼指定了一個私鑰的路徑(默認是${user.home}/.ssh/id_dsa)以及如果需要的話,一個密語。將來passphrase和password元素可能會被提取到外部,但目前它們必須在settings.xml文件以純文本的形式聲明。 -->
        <privateKey>${usr.home}/.ssh/id_dsa</privateKey>
<!--鑑權時使用的私鑰密碼。 -->
        <passphrase>some_passphrase</passphrase>
 <!--文件被創建時的權限。如果在部署的時候會創建一個倉庫文件或者目錄,這時候就可以使用權限(permission)。這兩個元素合法的值是一個三位數字,其對應了unix文件系統的權限,如664,或者775。 -->
        <filePermissions>664</filePermissions>
<!--目錄被創建時的權限。 -->
        <directoryPermissions>775</directoryPermissions>
	</server>
</servers>

7、mirrors

定義倉庫位置

<mirrors>
	 <mirror>
        <!-- ID標識 -->
		<id>alimaven</id>
		<name>aliyun maven</name>
		<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
        <!-- 被鏡像的服務器的id -->
		<mirrorOf>central</mirrorOf>
	 </mirror>
	 <!--<mirror>
		<id>maven-central</id>
		<name>maven-central</name>
		<mirrorOf>repositoryId</mirrorOf>
		<url>http://47.104.195.115:8081/repository/maven-public/</url>  
	</mirror>-->
</mirrors>

8、profiles

根據環境參數來調整構建配置的列表,相當於pom.xml中profile元素,不過是精簡版本;當被激活時,會覆蓋任何其它定義在pom.xml中帶有相同id的profile。當所有的約束條件都滿足的時候就會激活這個profile。

<profiles>
    <profile>
  <!-- profile的唯一標識 -->
        <id>test</id>     
        <!-- 自動觸發profile的條件邏輯 -->
        <activation>
            <activeByDefault>false</activeByDefault>
            <jdk>1.6</jdk>
            <os>
                <name>Windows 7</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>
        <!-- 擴展屬性列表 -->
        <properties>
            <maven.compiler.source>1.6</maven.compiler.source>
            <maven.compiler.target>1.7</maven.compiler.target>
            <maven.compiler.compilerVersion>1.7</maven.compiler.compilerVersion>
        </properties>
        <!-- 遠程倉庫列表 -->
        <repositories />
        <!-- 插件倉庫列表 -->
        <pluginRepositories />
      ...
    </profile>
</profiles>

9、activeProfiles

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

<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">
  ...
  <activeProfiles>
    <!-- 要激活的profile id -->
    <activeProfile>env-test</activeProfile>
  </activeProfiles>
  ...
</settings>

 

PS:部分是翻譯原文件,部分是從以下網址借鑑,沒它的詳細

https://www.cnblogs.com/hongmoshui/p/10762272.html

發佈了83 篇原創文章 · 獲贊 16 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章