maven浅谈之settings.xml和仓库及镜像

概述

settings.xml是maven最重要的配置,下面是其可以配置的元素,注意:settings配置的属性在pom也是可以引用的。

<localRepository/>
<interactiveMode/>
<usePluginRegistry/>
<offline/>
<pluginGroups/>
<servers/>
<mirrors/>
<proxies/>
<profiles/>
<activeProfiles/>
一、简单配置信息

1.1、配置本地仓库路径

<localRepository>D:/abc<localRepository>

1.2、maven是否需要和用户交互

<interactiveMode>true</interactiveMode>

1.3、是否需要使用plugin-registry.xml文件来管理插件版本。

<usePluginRegistry>false</usePluginRegistry>

1.4、是否离线模式运行(连接不上网)

<offline>false</offline>

1.5、当插件的组织id(groupId)没有显式提供时,供搜寻插件组织Id(groupId)的列表。
我们常常在pom中配置插件,都是可以不配置<groupId>的,因为默认提供了org.apache.maven.pluginsorg.codehaus.mojo

<pluginGroups>
    <pluginGroup>org.apache.maven.plugins</pluginGroup>
    <pluginGroup>org.codehaus.mojo</pluginGroup>
</pluginGroups>
二、复杂的配置
2.1 proxies
<proxies>
    <!--代理元素包含配置代理时需要的信息 -->
    <proxy>
      <!--代理的唯一定义符,用来区分不同的代理元素。 -->
      <id>myproxy</id>
      <!--该代理是否是激活的那个。true则激活代理。当我们声明了一组代理,而某个时候只需要激活一个代理的时候,该元素就可以派上用处。 -->
      <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>
2.2 profiles(配置仓库)

我们知道pom中也是有profiles这个属性的,那么他们的区别是settings.xml总的profile和pom中的profile的区别区别是,settings.xml中的profile只有activation,repositories,plugineReositories,properties四个子元素。通常是用来配置仓库信息的。

 <profiles>
   <profile>
     <!-- profile的唯一标识 -->
     <id>prod</id>
     <!-- 自动触发profile的条件逻辑 -->
     <activation />
     <!-- 扩展属性列表 -->
     <properties />
     <!-- 远程仓库列表 -->
     <repositories />
     <!-- 插件仓库列表 -->
     <pluginRepositories />
   </profile>
 </profiles>
2.2.1 profile中的仓库

我们下载maven之后,直接使用,也是可以直接下载依赖的,因为maven的安装包中已经放置了中央仓库,不同的版本路径是一样的,我安装的是3版本的,它的位置是:lib/maven-model-builder-3.3.3.jar解压中的org/apache/maven/model/pom-4.0.0.xml中。
由于中央仓库在国外,所以我们下载的速度比较慢,所以一般都会使用国内的仓库。国内一般都是用阿里的仓库(https://developer.aliyun.com/mvn/guide)
在settings.xml中定义仓库,注意由于settings.xml不直接支持repositories语法,所以一般都是把仓库定义在profile中。

<profiles>
    <profile>
      <id>maven-repo</id>
      <repositories>
          <repository>
            <id>central</id>
            <name>central</name>
            <url>https://maven.aliyun.com/repository/central/</url>
            <releases>
              <enabled>true</enabled>
            </releases>
            <snapshots>
              <enabled>false</enabled>
            </snapshots>
          </repository>
        </repositories>
    </profile>
</profiles>
<activeProfiles>
    <activeProfile>maven-repo</activeProfile>
</activeProfiles>
2.3 mirror镜像

镜像会对仓库的请求进行过滤,也就是说当我们下载依赖的时候,如果镜像中存在,就直接从镜像中下载。
国内阿里的镜像:

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

id:是镜像的标识
mirrorOf:被镜像仓库的id,例如central标识替代中央仓库,google标识替代谷歌套件仓库

问题1:那么就产生一个问题了,都已经有仓库了为什么需要镜像呢?我理解的镜像其实就是对仓库的集合。可以一次性解决多个仓库的配置。
问题2:那么我们直接配置镜像了,还需要配置仓库吗?有时候需要
比如,镜像中不存在对应的依赖,那我们就要独立配置仓库了,还有就是我们要上传jar也要配置仓库。
问题3:这么多途径获取依赖,那么一个依赖到底从哪里途径获取呢?
首先本地仓库没有,然后要经过镜像,没有,找自己配置的仓库,如果没有再去找中央仓库。

2.4 servers

一般,仓库的下载和部署是在pom.xml文件中的repositories和distributionManagement元素中定义的。然而,一般类似用户名、密码(有些仓库访问是需要安全认证的)等信息不应该在pom.xml文件中配置,这些信息可以配置在settings.xml中。
当我们要把自己的jar上传到仓库,供其他人使用,就需要上传mvn deploy
需要我们在项目的pom文件中定义上传的仓库

<distributionManagement>
    <repository>
      <id>nexus-releases</id>
      <name>Nexus Release Repository</name>
      <url>http://127.0.0.1:8080/nexus/content/repositories/releases/</url>
    </repository>
    <snapshotRepository>
      <id>nexus-snapshots</id>
      <name>Nexus Snapshot Repository</name>
      <url>http://127.0.0.1:8080/nexus/content/repositories/snapshots/</url>
    </snapshotRepository>
</distributionManagement>

一般来说分发包到仓库都是要经过用户验证的,用户的验证信息都是放在settings.xml中,避免信息不必要的泄露

<servers>
    <server>
      <id>nexus-releases</id>
      <username>admin</username>
      <password>admin123</password>
    </server>
    <server>
      <id>nexus-snapshots</id>
      <username>admin</username>
      <password>admin123</password>
    </server>
</servers>

注意:这里的id要和pom中distributionManagement中的仓库的id保持一致,确定是哪个仓库的验证信息。

nexus是私服,可以理解为运行仓库的服务器。所以是仓库的管理器,一个nexus可以设置多个仓库。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值