目录

  • 1、操作步骤
  • 2、完整配置


版本说明

maven版本: 3.6.3



1、操作步骤

1、在本地maven的 settings.xml 配置使用公司maven仓库时,配置个人账号,在 servers 节点增加配置(需要则配置,不需要则跳过)

本地密码如果不想以明文方式暴露可以参考: maven仓库密码加密

<server>
    <id>pds-repo</id>
    <username>个人账号</username>
    <password>个人账号的密码</password>
</server>

2、在本地maven的 settings.xml 配置使用公司maven仓库,在 mirrors 节点增加配置

注意1:要使用用户名密码的话 mirrorid 就需要跟 serverid 保持一致。

<mirrors>
    <!-- 私库镜像
    如果私库要使用账号密码那么这里 mirror中 的id就要跟 server 中的id保持一致
    因为 Maven 是通过 id 来对比变量和匹配的
    mirrorOf 替代方式, 
      *: 匹配所有,所有内容都从这个镜像拉取
      external:*: 除了本地缓存的所有从镜像仓库拉取;
      repo,repo1: repo或者repo1从这个镜像进行拉取,这里的repo指的profile的ID
      *,!repo1: 除了repo1的所有仓库;
    -->
    <mirror>
      <id>pds-repo</id>
      <!-- dev 的从这个镜像拉取, 这里的名称跟下面对应的 profile 的id保持一致 -->
      <mirrorOf>dev</mirrorOf>
      <name>公司私库</name>
      <url>http://192.10.10.70:8081/repository/maven-public/</url>
    </mirror>

    <!-- 阿里云镜像 -->
    <mirror>
      <id>aliyunmaven</id>
      <!-- aliyun 的从这个镜像拉取, 这里的名称跟下面对应的 profile 的id保持一致 -->
      <mirrorOf>aliyun</mirrorOf>
      <name>阿里云公共仓库</name>
      <!-- <url>https://maven.aliyun.com/repository/public</url> -->
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
    </mirror>
  </mirrors>

3、在本地maven的 settings.xml 配置支持从snapshots仓库拉取快照,在 profiles 节点增加配置

<profiles>
    <!-- 阿里云仓库配置 -->
    <profile>
      <!-- 这里的id名称跟上面对应的 mirror 镜像的 mirrorOf 名称保持一致 -->
      <id>aliyun</id> 
      <repositories>
        <repository>
          <id>aliyun</id>
          <!-- <url>https://maven.aliyun.com/repository/public</url> -->
          <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
          <releases>
            <enabled>true</enabled>
          </releases>
          <snapshots>
            <enabled>true</enabled>
          </snapshots>
        </repository>
      </repositories>
    </profile>

    <!-- 私库仓库配置 -->
    <profile>
        <!-- 这里的id名称跟上面对应的 mirror 镜像的 mirrorOf 名称保持一致 -->
		<id>dev</id>
		<repositories>
		  <repository>
	          <!-- 如果要使用用户名密码的话 repository 的id就要和mirror的id保持一致 -->
	          <id>pds-repo</id>
	          <url>http://192.10.10.70:8081/repository/maven-public/</url>
	          <releases>
	            <enabled>true</enabled>
	          </releases>
	          <snapshots>
	            <enabled>true</enabled>
	          </snapshots>
		  </repository>
		</repositories>
			<!-- <pluginRepositories>
				<pluginRepository>
					<id>pds-repo</id>
					<name>deployment</name>
					<url>http://192.10.10.70:8081/repository/maven-public/</url>
				</pluginRepository>
			</pluginRepositories> -->
		</profile>
  </profiles>

4、在本地maven的 settings.xml 激活配置,在 activeProfiles 节点增加配置

<activeProfiles>
    <activeProfile>dev</activeProfile>
    <activeProfile>aliyun</activeProfile>
  </activeProfiles>



2、完整配置

<?xml version="1.0" encoding="UTF-8"?>
<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>C:\environment\apache-maven-3.6.3\maven-repo</localRepository>

  <pluginGroups>
  </pluginGroups>

  <proxies>
  </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>
      <id>pds-repo</id>
      <username>ibpds</username>
      <password>rootabc123</password>
    </server>
  </servers>

  <mirrors>
    <!-- 私库镜像
    如果私库要使用账号密码那么这里 mirror中 的id就要跟 server 中的id保持一致
    因为 Maven 是通过 id 来对比变量和匹配的
    mirrorOf 替代方式, 
      *: 匹配所有,所有内容都从这个镜像拉取
      external:*: 除了本地缓存的所有从镜像仓库拉取;
      repo,repo1: repo或者repo1从这个镜像进行拉取,这里的repo指的profile的ID
      *,!repo1: 除了repo1的所有仓库;
    -->
    <mirror>
      <id>pds-repo</id>
      <!-- dev 的从这个镜像拉取, 这里的名称跟下面对应的 profile 的id保持一致 -->
      <mirrorOf>dev</mirrorOf>
      <name>公司私库</name>
      <url>http://192.10.10.70:8081/repository/maven-public/</url>
    </mirror>

    <!-- 阿里云镜像 -->
    <mirror>
      <id>aliyunmaven</id>
      <!-- aliyun 的从这个镜像拉取, 这里的名称跟下面对应的 profile 的id保持一致 -->
      <mirrorOf>aliyun</mirrorOf>
      <name>阿里云公共仓库</name>
      <!-- <url>https://maven.aliyun.com/repository/public</url> -->
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
    </mirror>
  </mirrors>


  <profiles>
    <!-- 阿里云仓库配置 -->
    <profile>
      <!-- 这里的id名称跟上面对应的 mirror 镜像的 mirrorOf 名称保持一致 -->
      <id>aliyun</id> 
      <repositories>
        <repository>
          <id>aliyun</id>
          <!-- <url>https://maven.aliyun.com/repository/public</url> -->
          <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
          <releases>
            <enabled>true</enabled>
          </releases>
          <snapshots>
            <enabled>true</enabled>
          </snapshots>
        </repository>
      </repositories>
    </profile>

    <!-- 私库仓库配置 -->
    <profile>
    	<!-- 这里的id名称跟上面对应的 mirror 镜像的 mirrorOf 名称保持一致 -->
		<id>dev</id>
		<repositories>
		 <repository>
          <!-- 如果要使用用户名密码的话 repository 的id就要和mirror的id保持一致 -->
          <id>pds-repo</id>
          <url>http://192.10.10.70:8081/repository/maven-public/</url>
          <releases>
            <enabled>true</enabled>
          </releases>
          <snapshots>
            <enabled>true</enabled>
          </snapshots>
		 </repository>
		</repositories>
			<!-- <pluginRepositories>
				<pluginRepository>
					<id>pds-repo</id>
					<name>deployment</name>
					<url>http://192.10.10.70:8081/repository/maven-public/</url>
				</pluginRepository>
			</pluginRepositories> -->
		</profile>
  </profiles>

  <!-- 要激活的配置 -->
  <activeProfiles>
    <activeProfile>aliyun</activeProfile>
    <activeProfile>dev</activeProfile>
  </activeProfiles>
</settings>