maven3.9的settings.xml 内容学习

发布于:2024-04-19 ⋅ 阅读:(20) ⋅ 点赞:(0)

settings.xml 文件介绍

settings.xml 是 Maven 的配置文件,它允许你自定义 Maven 的行为,比如设置仓库、代理、认证信息等。在 Maven 3.9 中,settings.xml 的结构和内容可能与之前的版本相似,但可能会有一些小的改进或变化。下面我们以3.9.6版本介绍。

首先打开maven的解压目录的 conf 文件夹

打开settings.xml文件

下面我们一个配置一个的过

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

设置本地仓库的位置。

示例:

 <localRepository>D:\repository\.m2</localRepository>
interactiveMode

设置Maven是否应该以交互模式运行。默认值为true,表示Maven在需要时会提示用户输入。如果设置为false,Maven将使用默认值或基于其他设置的值。

示例:

  <interactiveMode>true</interactiveMode>
offline

<offline>设置Maven是否应该尝试联网执行构建。默认值为false。当由于网络设置或安全因素,构建服务器不能连接远程仓库时,这个设置非常有用。

 示例:

  <offline>false</offline>

pluginGroups


定义插件组。<pluginGroups>允许定义额外的插件组,这些组在解析插件时会被搜索。默认情况下,Maven会自动添加org.apache.maven.pluginsorg.codehaus.mojo

 示例:

  <pluginGroups>
    <pluginGroup>org.mortbay.jetty</pluginGroup>
  </pluginGroups>

servers

<servers>用于定义服务器配置,如认证信息,这些信息通常不应该在pom.xml中配置,以避免安全问题。

  示例:

  <interactiveMode>true</interactiveMode>

mirrors


配置镜像站点,用于加速 Maven 下载。

 配置阿里云镜像示例:

  <mirrors>
      <mirror>
          <id>alimaven</id>
          <name>aliyun maven</name>
          <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
          <mirrorOf>central</mirrorOf>
      </mirror>
  </mirrors>

proxies


<proxies>用于配置网络代理,当Maven需要通过代理服务器连接外部网络时使用。

 示例:

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

profiles

<profiles>定义了一组构建配置文件,每个<profile>可以包含不同的设置和插件组。<activation>子元素定义了激活该配置文件的条件。<activeProfiles>列出了需要激活的配置文件ID。

 示例:

  <profiles>
    <profile>
      <id>tsetRepositories</id>
      <repositories>
        <repository>
          <id>tset</id>                                           
        <url>http://maven.aliyun.com/nexus/content/groups/public/</url>             
          <releases>
            <enabled>true</enabled>
          </releases>
          <snapshots>
            <enabled>true</enabled>
          </snapshots>
        </repository>
        
      </repositories>     
    </profile>
  </profiles>

activeProfiles


指定默认激活的配置文件。

 示例:

  <activeProfiles>
    <activeProfile>testRepositories</activeProfile>
  </activeProfiles>

注意

settings.xml 文件通常位于你的用户目录下

在 Windows 上可能是 C:\Users\username

在 macOS 或 Linux 上可能是 ~/.m2

可以在命令行中使用 -s 参数指定一个不同的 settings.xml 文件。