maven配置文件settings.xml詳解

maven運行時的配置文件settings.xml

安裝位置

  1. 全局配置:${maven_home}/config/settings.xml
  2. 用戶配置:~/.m2/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
                          https://maven.apache.org/xsd/settings-1.0.0.xsd">
      <localRepository/>
      <interactiveMode/>
      <offline/>
      <pluginGroups/>
      <servers/>
      <mirrors/>
      <proxies/>
      <profiles/>
      <activeProfiles/>
    </settings>

localRepository

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

interactiveMode

交互模式,是否接受用戶輸入,默認為true

offline

是否在離線模式下運行,默認是false

pluginGroups

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

<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>

如上配置可以在命令行執行如下命令:

mvn jetty:run

servers

在項目的POM中可以配置上傳和下載( repositories and distributionManagement )的倉庫,但是倉庫對應的用戶名、密碼、秘鑰、權限等敏感信息應當在構建服務器上配置,即在本節點之下。

<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:服務器的id,需要與倉庫和鏡像中對應(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:多個竟像時,id不能重復
  • mirrorOf:當前id對應的鏡像,不能與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表示當前代理有效,false表示當前代理無效
  • nonProxyHosts:不需要通過代理的主機

profiles

這里的profile是項目中pom.xml中profile節點的縮減版,是全局配置,并不針對于單獨的項目。這里的profile只包含四個子節點,分別是:activation,repositories,properties,pluginRepositories

settings.xml中profile配置會覆蓋項目中pom.xml或者 profiles.xml中配置

activation

這里配置的是profile的關鍵信息,與pom中的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">
  ...
  <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版本的前綴,會在環境中尋找符合前綴條件的jdk來使用,maven2.1開始支持區間配置。參考
  • os:系統配置。參考
  • porperty:當maven需要某些屬性的時候,會從這里尋找name,value對。可以與pom中配置相同的name,對應不同的value
  • file:通過給定文件,在哪個文件存在,哪個文件不存在的情況profile生效

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

通過maven-help-plugin查看當前構建下有效的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

遠程倉庫的配置

<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:更新策略。可選值:always, daily (默認),interval:X (X整數,單位:分鐘) , never
  • checksumPolicy:部署到倉庫策略。可選擇:ignore,fail, warn on missing , incorrect checksums
  • layout:maven2開始又默認配置

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轉發請求過程解析
Nginx中的負載均衡算法
Nginx upstream指令配置說明
Nginx中虛擬服務器server指令配置說明
Nginx中proxy_pass/proxy_redirect/proxy_set_header配置說明
Nginx中ngx_http_core_module相關指令配置說明
Java自帶JVM監控工具jstat使用詳細說明
Java自帶JVM監控工具jps使用詳細說明
Java自帶故障分析工具jmap工具使用說明
Java自帶故障分析工具jhat工具使用說明

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容