maven配置文件settings.xml詳解

maven運(yùn)行時(shí)的配置文件settings.xml

安裝位置

  1. 全局配置:${maven_home}/config/settings.xml
  2. 用戶配置:~/.m2/settings.xml

如果全局配置與用戶配置同時(shí)存在,會進(jìn)行合并,相同配置則以用戶配置優(yōu)先。

配置節(jié)點(diǎn)

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

localRepository

本地倉庫路徑,不配置則默認(rèn)為${user.home}/.m2/repository

interactiveMode

交互模式,是否接受用戶輸入,默認(rèn)為true

offline

是否在離線模式下運(yùn)行,默認(rèn)是false

pluginGroups

插件。通過在pluginGroup節(jié)點(diǎn)下配置id標(biāo)識,在命令行中使用到的插件需要在這里配置。注:org.apache.maven.plugins 和 org.codehaus.mojo里面的差價(jià)自動包含不需要配置。如下配置:

<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">
  ...
  <pluginGroups>
    <pluginGroup>org.eclipse.jetty</pluginGroup>
  </pluginGroups>
  ...
</settings>

如上配置可以在命令行執(zhí)行如下命令:

mvn jetty:run

servers

在項(xiàng)目的POM中可以配置上傳和下載( repositories and distributionManagement )的倉庫,但是倉庫對應(yīng)的用戶名、密碼、秘鑰、權(quán)限等敏感信息應(yīng)當(dāng)在構(gòu)建服務(wù)器上配置,即在本節(jié)點(diǎn)之下。

<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">
  ...
  <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:服務(wù)器的id,需要與倉庫和鏡像中對應(yīng)(repository/mirror)

mirrors

鏡像倉庫地址配置,參考地址

<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">
  ...
  <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:多個(gè)竟像時(shí),id不能重復(fù)
  • mirrorOf:當(dāng)前id對應(yīng)的鏡像,不能與id相同

proxies

代理信息的配置

<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">
  ...
  <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>
  • active:true表示當(dāng)前代理有效,false表示當(dāng)前代理無效
  • nonProxyHosts:不需要通過代理的主機(jī)

profiles

這里的profile是項(xiàng)目中pom.xml中profile節(jié)點(diǎn)的縮減版,是全局配置,并不針對于單獨(dú)的項(xiàng)目。這里的profile只包含四個(gè)子節(jié)點(diǎn),分別是:activation,repositories,properties,pluginRepositories

settings.xml中profile配置會覆蓋項(xiàng)目中pom.xml或者 profiles.xml中配置

activation

這里配置的是profile的關(guān)鍵信息,與pom中的profile類似,可以定義指定環(huán)境下參數(shù)使用。

<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">
  ...
  <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>
  • jdk:jdk版本配置,只是jdk版本的前綴,會在環(huán)境中尋找符合前綴條件的jdk來使用,maven2.1開始支持區(qū)間配置。參考
  • os:系統(tǒng)配置。參考
  • porperty:當(dāng)maven需要某些屬性的時(shí)候,會從這里尋找name,value對??梢耘cpom中配置相同的name,對應(yīng)不同的value
  • file:通過給定文件,在哪個(gè)文件存在,哪個(gè)文件不存在的情況profile生效

profile的生效與否不僅可以在這里配置,同時(shí)可以在命令行通過 -P profileid使其生效

通過maven-help-plugin查看當(dāng)前構(gòu)建下有效的profile

mvn help:active-profiles

properties

maven的屬性通過占位符 ${X}來配置,在settings.xml中有5中不同格式的配置。

  1. env.X: Prefixing a variable with “env.” will return the shell’s environment variable. For example, {env.PATH} contains thepath environment variable (%PATH% in Windows).
  2. project.x: A dot (.) notated path in the POM will contain the corresponding element’s value. For example: <project><version>1.0</version></project> is accessible via ${project.version}.
  3. settings.x: A dot (.) notated path in the settings.xml will contain the corresponding element’s value. For example: <settings><offline>false</offline></settings> is accessible via ${settings.offline}.
  4. Java System Properties: All properties accessible via java.lang.System.getProperties() are available as POM properties, such as ${java.home}.
  5. x: Set within a <properties /> element or an external files, the value may be used as ${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
                      https://maven.apache.org/xsd/settings-1.0.0.xsd">
  ...
  <profiles>
    <profile>
      ...
      <properties>
        <user.install>${user.home}/our-project</user.install>
      </properties>
      ...
    </profile>
  </profiles>
  ...
</settings>

repository

遠(yuǎn)程倉庫的配置

<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">
  ...
  <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>
  • enabled:true表示生效,false表示不生效
  • updatePolicy:更新策略??蛇x值:always, daily (默認(rèn)),interval:X (X整數(shù),單位:分鐘) , never
  • checksumPolicy:部署到倉庫策略??蛇x擇:ignore,fail, warn on missing , incorrect checksums
  • layout:maven2開始又默認(rèn)配置

pluginRepositories

插件倉庫配置,配置類似于repositories

activeProfiles

配置哪些profile是生效的,指向是profile的id,如果配置的id不存在,不會有任何問題。配置在settings.xml、pom.xml、和profile.xml中的profile都可以在這里指定。

<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>
    <activeProfile>env-test</activeProfile>
  </activeProfiles>
</settings>

其他文章列表

spring web service系列1
spring web service系列2
spring web service系列3
Nginx轉(zhuǎn)發(fā)請求過程解析
Nginx中的負(fù)載均衡算法
Nginx upstream指令配置說明
Nginx中虛擬服務(wù)器server指令配置說明
Nginx中proxy_pass/proxy_redirect/proxy_set_header配置說明
Nginx中ngx_http_core_module相關(guān)指令配置說明
Java自帶JVM監(jiān)控工具jstat使用詳細(xì)說明
Java自帶JVM監(jiān)控工具jps使用詳細(xì)說明
Java自帶故障分析工具jmap工具使用說明
Java自帶故障分析工具jhat工具使用說明

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容