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工具使用说明

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容