Maven settings.xml中配置浅析

最近遇到过不少这样那样的问题,曾经做过maven的分享,但是发现当时部分内容还是太想当然了,下面经过尝试后简单总结下:

首先几个逻辑:

  • pom>启用的profile>maven原有配置

  • mirror配置mirrorOf和id匹配优先

    简单maven配置

    一般大家的配置(略去无关私有仓库配置)都是这样的

    <mirrors>
      <mirror>
          <id>nexus</id>
          <name>mvn.xxx.com</name>
          <mirrorOf>central</mirrorOf>
          <url>http://mvn.xxx.com/nexus/content/groups/t_repo_group/</url>         
      </mirror>
    </mirrors>
      <profile>
          <id>dev</id>
          <repositories>       
          <repository>
            <id>nexus</id>
            <url>http://mvn.xxx.com/nexus/content/groups/t_repo_group/</url>
            <releases><enabled>true</enabled></releases>
                      <snapshots><enabled>true</enabled></snapshots>
          </repository>
                      
              <repository>
                  <id>alibaba</id>
                  <url>http://code.alibabatech.com/mvn/releases/</url>
                  <releases>
                      <enabled>true</enabled>
                  </releases>
                  <snapshots>
                      <enabled>false</enabled>
                  </snapshots>
              </repository>
             
          </repositories>
          
          <pluginRepositories>
              <pluginRepository>
            <id>nexus</id>
            <url>http://mvn.xxx.com/nexus/content/groups/t_repo_group/</url>
            <releases><enabled>true</enabled></releases>
                      <snapshots><enabled>true</enabled></snapshots>
          </pluginRepository>
          </pluginRepositories>
      </profile>  
    <activeProfiles>
      <activeProfile>dev</activeProfile>
    </activeProfiles>
    

其实上面这个配置不能说错误,只能说有部分是没有生效的。几个重要的点逐一说明

mirrors

这个标签重要的属性包括idmirrorOf。id用来唯一区分。mirrorOf用来关联repository。
url用来表示私服地址。
mirrorOf常见大家配置成*、central、repo啥的。这里刚才提到了是用来关联respository的,等提到下面<respository>标签的时候在解释。
推荐大家阅读下http://blog.csdn.net/isea533/article/details/21560089

profile

这个就简单说下吧,就是算是个配置,可以配多个,具体哪个生效可以通过mvn命令指定,或者配置<activeProfiles>

repositories

这里面算是配置的重点

<repository>
      <id>alibaba</id>
      <url>http://code.alibabatech.com/mvn/releases/</url>
      <releases>
              <enabled>true</enabled>
      </releases>
      <snapshots>
               <enabled>false</enabled>
      </snapshots>
</repository>

几个重要的配置,一目了然吧,id标识,url地址,是否从该仓库下release,是否从该仓库下快照版本。
这里就有人会懵逼了,这里怎么又配了个地址,跟mirrors里面的地址哪个生效呢?

好的,那咱们试试。先规定一下配置:

<mirrors>
    <mirror>
        <id>nexus</id>
        <name>mvn.ws.netease.com</name>
        <mirrorOf>central</mirrorOf>
        <url>http://mvn.xxx.com/nexus/content/groups/t_repo_group/</url>       
    </mirror>
  </mirrors>
  <repositories>       
        <repository>
          <id>nexus</id>
          <url>http://mvn.ccc.com/nexus/content/groups/t_repo_group/</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </repository>
</repositories> 

把地址区分下,mirror里配成xxx,repository配成ccc
随便找一个项目,设定一个不存在的依赖,mvn -U compile下:

image.png

可以发现去ccc找了。说明repository里的生效了。
那么mirror里的地址什么时候生效呢?其实刚才说了,mirror里的是靠mirrorOf中的内容和repository中id关联的。比如我们把刚才配置改为

<mirrors>
    <mirror>
        <id>nexus</id>
        <name>mvn.ws.netease.com</name>
            <mirrorOf>central</mirrorOf>
        <url>http://mvn.xxx.com/nexus/content/groups/t_repo_group/</url>       
    </mirror>
  </mirrors>
  <repositories>       
        <repository>
          <id>central</id>
          <url>http://mvn.ccc.com/nexus/content/groups/t_repo_group/</url>
          <releases><enabled>true</enabled></releases>
                    <snapshots><enabled>true</enabled></snapshots>
        </repository>
</repositories> 

把repository中的id改成central

image.png

这样就行了。此外mirrorOf中可以配置通配符,例如*,表示任何repository都和这个关联。

其实简单来说就是如果repository的id能和mirrorOf关联上,那么url以mirror的为准,否则以repository中自己的url为准

其他还有一些点,repositories中可以配置多个repository,配置多个话,一个找不到会找下一个,比如我们在刚才基础上加上阿里的配置

<repositories>       
        <repository>
          <id>nexus</id>
          <url>http://mvn.ccc.com/nexus/content/groups/t_repo_group/</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </repository>
        <repository>
                <id>alibaba</id>
                <url>http://code.alibabatech.com/mvn/releases/</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>false</enabled>
                </snapshots>
            </repository> 
</repositories>

在构建一次:

image.png

当配置多个时,会逐一进行下载尝试。

总结

咱们在回顾下起初的配置,可以看到启用的profile是dev,dev中的repository的id是nexus,跟mirrorOf没有匹配,那么生效的配置就是repository中自己的url配置,所以这里完全可以省略掉mirror的配置。当然如果多个profile公用一个私服地址,也可以指定mirror地址,然后repository中的id指定成和mirrorOf相同,同时可以省略掉自己标签中url。

此外还有几个点要说,pluginRepositories,配置信息基本和repository一致,不过这个地址是用来下maven的插件的,就是pom中这样的配置

        <plugins>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <filteringDeploymentDescriptors>true</filteringDeploymentDescriptors>
                    <webResources>
                        <resource>
                            <directory>${basedir}/src/main/webapp/WEB-INF</directory>
                            <filtering>true</filtering>
                            <targetPath>WEB-INF</targetPath>
                            <includes>
                                <include>**</include>
                            </includes>
                        </resource>
                    </webResources>
                </configuration>
            </plugin>
        </plugins>

还有,pom也可以指定repository:

image.png

这样配置会和settings.xml中生效的配置合并,并优先从这个库找,找不到继续走settings.xml配置。

Servers:

 表示当需要连接到一个私有服务器的时候需要的认证信息 ,

<!--表示当需要连接到一个私有服务器的时候需要的认证信息 。-->
    <servers>
        <!--服务器元素包含配置服务器时需要的信息
  发布的服务器,发布的位置在POM中配置,以ID为关联,有很多公用的信息需要配置在POM文件里,最佳实践是定义一个公司级别的root pom - 
	-->
        <server>
            <!--这是server的id(注意不是用户登陆的id),该id与pom中的 
                             distributionManagement中repository元素的id相匹配。-->
            <id>frame-releases</id>
            <!--鉴权用户名。鉴权用户名和鉴权密码表示服务器认证所需要的登录名和密码。 -->
            <username>admin</username>
            <!--鉴权用户名。鉴权用户名和鉴权密码表示服务器认证所需要的登录名和密码。 -->
            <password>admin123</password>
        </server>
        <server>
            <id>siteServer</id>
            <!--鉴权时使用的私钥位置。和前两个元素类似,私钥位置和私钥密码指定了一个私钥的路径(默认是${user.home}/.ssh/id_dsa)以及如果需要的话,一个密语。将来passphrase和password元素可能会被提取到外部,但目前它们必须在settings.xml文件以纯文本的形式声明。 -->
            <privateKey>/path/to/private/key</privateKey>
            <!--鉴权时使用的私钥密码。-->
            <passphrase>optional; leave empty if not used.</passphrase>
            <!--文件被创建时的权限。如果在部署的时候会创建一个仓库文件或者目录,这时候就可以使用权限(permission)。这两个元素合法的值是一个三位数字,其对应了unix文件系统的权限,如664,或者775。 -->
            <filePermissions>664</filePermissions>
            <!--目录被创建时的权限。 -->
            <directoryPermissions>775</directoryPermissions>
        </server>
    </servers>
 

pom.xml中的配置

<distributionManagement>
    <repository>
        <!--frame-releases与setting文件的server id 对应 -->
        <id>frame-releases</id>
        <name>frame-releases</name>
        <url>http://localhost:8081/nexus/content/repositories/releases/</url>
    </repository>
    <snapshotRepository>
        <id>frame-snapshots</id>
        <name>frame Snapshots Repository</name>
        <url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
    </snapshotRepository>
</distributionManagement>
 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值