美文网首页
Maven settings.xml中私有仓库配置浅析

Maven settings.xml中私有仓库配置浅析

作者: happut | 来源:发表于2017-12-25 16:24 被阅读0次

    Maven setting中私有仓库配置浅析

    最近遇到过不少这样那样的问题,曾经做过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配置。

    相关文章

      网友评论

          本文标题:Maven settings.xml中私有仓库配置浅析

          本文链接:https://www.haomeiwen.com/subject/puvlgxtx.html