maven - [02] settings.xml配置

maven处理配置的优先级顺序

(1)全局settings.xml(优先级★☆☆☆☆)

位于Maven安装目录的conf/settings.xml,提供系统级的默认配置,比如本地仓库位置、远程仓库列表等。这些设置对所有用户和项目都有效,但优先级最低。

(2)用户级别的settings.xml(优先级★★☆☆☆)

位于用户主目录下的.m2/settings.xml,覆盖全局的settings.xml设置。这里的配置仅对当前用户生效,优先级高于全局settings.xml

(3)父项目POM(优先级★★★☆☆)

父项目POM定义了所有子项目的共享配置,如依赖管理(dependencyManagement)、插件管理(pluginManagement)、构建配置等。父POM的设置为所有继承它的子项目提供了基础配置,但是子项目可以直接覆盖这些配置。

(4)子项目POM(优先级★★★★☆)

子项目的POM文件具有最高优先级,它可以覆盖来自父POM的所有配置,包括直接声明依赖、构建属性、插件配置等。子项目的直接配置将优先于任何上级配置。

(5)profiles和profile-specifix配置(优先级★★★★★)

不论是在settings.xml还是POM.xml中,激活的profiles可以进一步调整配置。如果一个profile在多个层级被定义(比如既有用户级settings.xml中的又有项目POM中的),那么激活的profile中定义的设置将根据定义的顺序(局部优先于全局)来覆盖之前的配置。

(6)其他配置文件(如profiles.xml等)(优先级★★★★★)

虽然不常见,但在特定场景下,Maven也允许使用或自定义其他配置文件,比如通过profiles.xml来管理一组可激活的profiles。这些额外的配置同样遵循局部(项目或用户)优先于全局的原则。

总结来说,Maven的配置优先级遵循“局部覆盖全局”和“子项目覆盖父项目”的原则,确保了灵活性和可维护性。开发者可以在更具体的层面(如子项目POM或激活的profiles)上进行精确控制,而不影响到更广泛的配置环境。

 

 

 

 

一、本地仓库(Local Repository)

指定Maven下载所有依赖包的位置。默认情况下,Maven会在用户目录下的.m2.reposiriey目录存储这些依赖

<localRepository>D:\Environment\mvnrepo</localRepository>

 

 

二、镜像(Mirrors)

配置国内maven仓库(阿里云、华为、网易...)的镜像地址,用于替换默认的中央仓库或其他远程仓库,提高依赖下载速度或解决访问受限问题。

<!-- 阿里云maven仓库仓库 -->
<mirror>  
    <id>alimaven</id>  
    <name>aliyun maven</name>  
    <url>https://maven.aliyun.com/repository/central</url>  
    <mirrorOf>central</mirrorOf>  
</mirror>
<mirror>  
    <id>alimaven</id>  
    <name>aliyun maven</name>  
    <url>https://maven.aliyun.com/repository/spring</url>  
    <mirrorOf>spring</mirrorOf>  
</mirror>
<mirror>  
    <id>alimaven</id>  
    <name>aliyun maven</name>  
    <url>https://maven.aliyun.com/repository/releases</url>  
    <mirrorOf>releases</mirrorOf>  
</mirror>

<!-- 华为maven仓库 -->
<mirror>  
    <id>huaweicloud</id>  
    <name>mirrorfrommaven huaweicloud</name>  
    <url>https://repo.huaweicloud.com/repository/maven/</url>  
    <mirrorOf>central</mirrorOf>  
</mirror>

<!-- 163 -->
<mirror>
    <id>nexus-163</id>
    <mirrorOf>*</mirrorOf>
    <name>网易 Nexus 163</name>
    <url>http://mirrors.163.com/maven/repository/maven-public/</url>
</mirror>

 

 

三、代理(Proxies)

如果你的网络环境需要通过代理服务器访问外网,可以在这里配置HTTP、HTTPS或FTP代理

<proxies>
  <proxy>
    <id>example-proxy</id>
    <active>true</active>
    <protocol>http</protocol>
    <host>proxy.example.com</host>
    <port>8080</port>
    <username>proxyuser</username>
    <password>proxypass</password>
    <nonProxyHosts>localhost|127.0.0.1</nonProxyHosts>
  </proxy>
</proxies>

 

四、服务器(Servers)

配置访问私有仓库或部署时需要认证的服务器凭据。

<servers>
  <server>
    <id>server-id</id>
    <username>your-username</username>
    <password>your-password</password>
    <!-- Optionally, for more security, you can use encrypted passwords -->
    <!--<password>${env.MAVEN_PASSWORD}</password>-->
  </server>
</servers>

 

五、全局属性(Properties)

可以在<properties>标签内定义一些全局变量,这些变量可以在POM文件中引用。

<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <java.version>1.8</java.version>
</properties>

 

 

 

 

我的settings.xml(D:\Environment\apache-maven-3.8.7\conf\settings.xml

settings.xml
<?xml version="1.0" encoding="UTF-8"?>

<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
-->

<!--
 | This is the configuration file for Maven. It can be specified at two levels:
 |
 |  1. User Level. This settings.xml file provides configuration for a single user,
 |                 and is normally provided in ${user.home}/.m2/settings.xml.
 |
 |                 NOTE: This location can be overridden with the CLI option:
 |
 |                 -s /path/to/user/settings.xml
 |
 |  2. Global Level. This settings.xml file provides configuration for all Maven
 |                 users on a machine (assuming they're all using the same Maven
 |                 installation). It's normally provided in
 |                 ${maven.conf}/settings.xml.
 |
 |                 NOTE: This location can be overridden with the CLI option:
 |
 |                 -gs /path/to/global/settings.xml
 |
 | The sections in this sample file are intended to give you a running start at
 | getting the most out of your Maven installation. Where appropriate, the default
 | values (values used when the setting is not specified) are provided.
 |
 |-->
<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
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ${user.home}/.m2/repository
  <localRepository>/path/to/local/repo</localRepository>
  -->
  <localRepository>D:\Environment\mvnrepo</localRepository> 

  <!-- interactiveMode
   | This will determine whether maven prompts you when it needs input. If set to false,
   | maven will use a sensible default value, perhaps based on some other setting, for
   | the parameter in question.
   |
   | Default: true
  <interactiveMode>true</interactiveMode>
  -->

  <!-- offline
   | Determines whether maven should attempt to connect to the network when executing a build.
   | This will have an effect on artifact downloads, artifact deployment, and others.
   |
   | Default: false
  <offline>false</offline>
  -->

  <!-- pluginGroups
   | This is a list of additional group identifiers that will be searched when resolving plugins by their prefix, i.e.
   | when invoking a command line like "mvn prefix:goal". Maven will automatically add the group identifiers
   | "org.apache.maven.plugins" and "org.codehaus.mojo" if these are not already contained in the list.
   |-->
  <pluginGroups>
    <!-- pluginGroup
     | Specifies a further group identifier to use for plugin lookup.
    <pluginGroup>com.your.plugins</pluginGroup>
    -->
  </pluginGroups>

  <!-- proxies
   | This is a list of proxies which can be used on this machine to connect to the network.
   | Unless otherwise specified (by system property or command-line switch), the first proxy
   | specification in this list marked as active will be used.
   |-->
  <proxies>
    <!-- proxy
     | Specification for one proxy, to be used in connecting to the network.
     |
    <proxy>
      <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>

  <!-- servers
   | This is a list of authentication profiles, keyed by the server-id used within the system.
   | Authentication profiles can be used whenever maven must make a connection to a remote server.
   |-->
  <servers>
    <!-- server
     | Specifies the authentication information to use when connecting to a particular server, identified by
     | a unique name within the system (referred to by the 'id' attribute below).
     |
     | NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are
     |       used together.
     |
    <server>
      <id>deploymentRepo</id>
      <username>repouser</username>
      <password>repopwd</password>
    </server>
    -->

    <!-- Another sample, using keys to authenticate.
    <server>
      <id>siteServer</id>
      <privateKey>/path/to/private/key</privateKey>
      <passphrase>optional; leave empty if not used.</passphrase>
    </server>
    -->
  </servers>

  <!-- mirrors
   | This is a list of mirrors to be used in downloading artifacts from remote repositories.
   |
   | It works like this: a POM may declare a repository to use in resolving certain artifacts.
   | However, this repository may have problems with heavy traffic at times, so people have mirrored
   | it to several places.
   |
   | That repository definition will have a unique id, so we can create a mirror reference for that
   | repository, to be used as an alternate download site. The mirror site will be the preferred
   | server for that repository.
   |-->
  <mirrors>
<!-- 	<mirror>
      <id>alimaven</id>
      <name>aliyun maven</name>
      <url>https://maven.aliyun.com/repository/public</url>
      <mirrorOf>public</mirrorOf>
    </mirror> -->
	
	
	<!-- 阿里云maven仓库仓库 -->
	<mirror>  
		<id>alimaven</id>  
		<name>aliyun maven</name>  
		<url>https://maven.aliyun.com/repository/central</url>  
		<mirrorOf>central</mirrorOf>  
	</mirror>
	<mirror>  
		<id>alimaven</id>  
		<name>aliyun maven</name>  
		<url>https://maven.aliyun.com/repository/spring</url>  
		<mirrorOf>spring</mirrorOf>  
	</mirror>
	<mirror>  
		<id>alimaven</id>  
		<name>aliyun maven</name>  
		<url>https://maven.aliyun.com/repository/releases</url>  
		<mirrorOf>releases</mirrorOf>  
	</mirror>
	
	<!-- 华为maven仓库 -->
	<mirror>  
		<id>huaweicloud</id>  
		<name>mirrorfrommaven huaweicloud</name>  
		<url>https://repo.huaweicloud.com/repository/maven/</url>  
		<mirrorOf>central</mirrorOf>  
	</mirror>
	
	<!-- 163 -->
	<mirror>
		<id>nexus-163</id>
		<mirrorOf>*</mirrorOf>
		<name>网易 Nexus 163</name>
		<url>http://mirrors.163.com/maven/repository/maven-public/</url>
	</mirror>
	
    <!-- junit镜像地址 
    <mirror>
      <id>junit</id>
      <name>junit Address/</name>
      <url>http://jcenter.bintray.com/</url>
      <mirrorOf>central</mirrorOf>
    </mirror>
	-->
    <!-- mirror
     | Specifies a repository mirror site to use instead of a given repository. The repository that
     | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
     | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
     |
    <mirror>
      <id>mirrorId</id>
      <mirrorOf>repositoryId</mirrorOf>
      <name>Human Readable Name for this Mirror.</name>
      <url>http://my.repository.com/repo/path</url>
    </mirror>
     -->
  </mirrors>

  <!-- profiles
   | This is a list of profiles which can be activated in a variety of ways, and which can modify
   | the build process. Profiles provided in the settings.xml are intended to provide local machine-
   | specific paths and repository locations which allow the build to work in the local environment.
   |
   | For example, if you have an integration testing plugin - like cactus - that needs to know where
   | your Tomcat instance is installed, you can provide a variable here such that the variable is
   | dereferenced during the build process to configure the cactus plugin.
   |
   | As noted above, profiles can be activated in a variety of ways. One way - the activeProfiles
   | section of this document (settings.xml) - will be discussed later. Another way essentially
   | relies on the detection of a system property, either matching a particular value for the property,
   | or merely testing its existence. Profiles can also be activated by JDK version prefix, where a
   | value of '1.4' might activate a profile when the build is executed on a JDK version of '1.4.2_07'.
   | Finally, the list of active profiles can be specified directly from the command line.
   |
   | NOTE: For profiles defined in the settings.xml, you are restricted to specifying only artifact
   |       repositories, plugin repositories, and free-form properties to be used as configuration
   |       variables for plugins in the POM.
   |
   |-->
  <profiles>
    <!-- profile
     | Specifies a set of introductions to the build process, to be activated using one or more of the
     | mechanisms described above. For inheritance purposes, and to activate profiles via <activatedProfiles/>
     | or the command line, profiles have to have an ID that is unique.
     |
     | An encouraged best practice for profile identification is to use a consistent naming convention
     | for profiles, such as 'env-dev', 'env-test', 'env-production', 'user-jdcasey', 'user-brett', etc.
     | This will make it more intuitive to understand what the set of introduced profiles is attempting
     | to accomplish, particularly when you only have a list of profile id's for debug.
     |
     | This profile example uses the JDK version to trigger activation, and provides a JDK-specific repo.
    <profile>
      <id>jdk-1.4</id>

      <activation>
        <jdk>1.4</jdk>
      </activation>

      <repositories>
        <repository>
          <id>jdk14</id>
          <name>Repository for JDK 1.4 builds</name>
          <url>http://www.myhost.com/maven/jdk14</url>
          <layout>default</layout>
          <snapshotPolicy>always</snapshotPolicy>
        </repository>
      </repositories>
    </profile>
    -->
	<!-- jdk1.8 -->
	<profile>
		<!-- profile的唯一标识 -->
		<id>jdk-1.8</id>
		<!-- 自动触发profile的条件逻辑 -->
		<activation>
			<!-- 当其值为true的时候表示如果没有其他的profile处于激活状态的时候,该profile将自动被激活。 -->
			<activeByDefault>true</activeByDefault>
			<jdk>1.8</jdk>
		</activation>
		<!-- 扩展属性列表 -->
            <!-- 用于定义属性键值对的。当该profile是激活状态的时候,properties下面指定的属性都可以在pom.xml中使用。 -->
            <!--如果Maven检测到某一个属性(其值可以在POM中通过${name}引用),
                其拥有对应的name = 值,Profile就会被激活。如果值字段是空的,那么存在属性名称字段就会激活profile,
                否则按区分大小写方式匹配属性值字段 -->
		<properties>
			<maven.compiler.source>1.8</maven.compiler.source>
			<maven.compiler.target>1.8</maven.compiler.target>
			<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
		</properties>
	</profile>
	
	

    <!--
     | Here is another profile, activated by the system property 'target-env' with a value of 'dev',
     | which provides a specific path to the Tomcat instance. To use this, your plugin configuration
     | might hypothetically look like:
     |
     | ...
     | <plugin>
     |   <groupId>org.myco.myplugins</groupId>
     |   <artifactId>myplugin</artifactId>
     |
     |   <configuration>
     |     <tomcatLocation>${tomcatPath}</tomcatLocation>
     |   </configuration>
     | </plugin>
     | ...
     |
     | NOTE: If you just wanted to inject this configuration whenever someone set 'target-env' to
     |       anything, you could just leave off the <value/> inside the activation-property.
     |
    <profile>
      <id>env-dev</id>

      <activation>
        <property>
          <name>target-env</name>
          <value>dev</value>
        </property>
      </activation>

      <properties>
        <tomcatPath>/path/to/tomcat/instance</tomcatPath>
      </properties>
    </profile>
    -->
  </profiles>

  <!-- activeProfiles
   | List of profiles that are active for all builds.
   |
  <activeProfiles>
    <activeProfile>alwaysActiveProfile</activeProfile>
    <activeProfile>anotherAlwaysActiveProfile</activeProfile>
  </activeProfiles>
  -->
</settings>

 

 

 

— 业精于勤荒于嬉,行成于思毁于随 —

posted @   HOUHUILIN  阅读(169)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示