maven配置多仓库的方法

刚接触maven就是在公司里配置好的,因此一直以来使用都没毛病,因此一直没有去动这些固有的东西。apache

  

可是,后来把公司的电脑拿回家以后,发现有的东西就搞不起来了。缘由也看一下就明白了,由于在公司的时候用的是公司的maven私服,因此回家后,用不了也是正常。安全

  

可是,真的脱离了公司,本身就不能工做了吗?不可能吧。难道一下开源工具都必需要依赖于公司的网络?这明显是不合理的。网络

  

那么,就扯出本次文章的意义了,在家里,天然是要公有的maven仓库了,那么,怎样配置maven仓库才能让本身用起来顺心呢?maven

 

1. 改掉原有的maven仓库地址,让maven从公网上摘取jar包下载,方便、快捷

原私有配置示例以下:
<?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   
       | The path to the local repository maven will use to store artifacts.  
       |  
       | Default: ${user.home}/.m2/repository -->  
      <localRepository>${user.home}/.m2/repository</localRepository>  
      <!--pluginGroups></pluginGroups-->   
      <!--proxies></proxies-->  
      <servers>  
        <server>  
            <id>releases</id>  
            <username>admin</username>  
            <password>123</password>  
        </server>  
        <server>  
            <id>snapshots</id>  
            <username>admin</username>  
            <password>123</password>  
        </server>     
      </servers> 
     <pluginRepositories>
        <pluginRepository>
            <id>mypublic</id>
            <name>Public</name>
            <url>http://test.nexus.com/nexus/content/groups/public/</url>
        </pluginRepository>
     </pluginRepositories>
      <mirrors>  
        <mirror>    
            <id>central</id>    
            <name>internal</name>    
            <url>http://test.nexus.com/nexus/content/groups/public/</url>    
            <mirrorOf>central</mirrorOf> 
        </mirror>  
      </mirrors>  
    <profiles>  
        <profile>  
              <id>nexus</id>  
              <!--Enable snapshots for the built in central repo to direct -->  
              <!--all requests to nexus via the mirror -->  
              <repositories>  
                <repository>  
                    <id>central</id>  
                    <url>http://central</url>  
                    <releases><enabled>true</enabled></releases>  
                    <snapshots>
                        <enabled>true</enabled>
                        <updatePolicy>always</updatePolicy>
                    </snapshots>  
                </repository>
              </repositories>
             <pluginRepositories>  
                <pluginRepository>  
                  <id>central</id>  
                  <url>http://central</url>  
                  <releases><enabled>true</enabled></releases>  
                  <snapshots><enabled>true</enabled></snapshots>  
                </pluginRepository>
             </pluginRepositories>  
        </profile> 
    </profiles>  
  <activeProfiles>  
    <!--make the profile active all the time -->  
    <activeProfile>nexus</activeProfile>  
  </activeProfiles>  
</settings>  

 

若是想直接把私有的地方干掉,那么,这是最快的,直接把mirror的url改掉就好了,如:ide

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

 

固然了,到须要的地方,再把这个地址改回来就能够了,这多是改动最小的方法了。可是也很恼火的一种配置方式,由于你要不时地记得切换(谁有那闲心)!!!工具

 

2. 添加一个相似结构的仓库配置,这样的话就不切来切去的了,一劳永逸

至关于添加了多仓库,以下:ui

<mirrors>
    <!-- 再添加一个mirror, 注意mirrorsOf 为 * -->
    <mirror>
        <id>alimaven</id>
        <name>aliyun maven</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
        <mirrorOf>*</mirrorOf>
    </mirror>        
</mirrors> 

<repositories>
    <!-- 添加一个 repository -->
    <repository>  
        <id>alimaven</id>  
        <url>http://alimaven</url>  
        <releases><enabled>true</enabled></releases>  
        <snapshots>
            <enabled>true</enabled>
            <updatePolicy>always</updatePolicy>
        </snapshots>  
    </repository>
</repositories>

 

这样的话,就不用再切换了。可是,这会致使一种状况,在某环境不可用时,maven下载jar将会很慢,严重影响心情,因此,实际上是不建议这么干的。url

 

3. 按照最简单的方式,新增一个仓库地址,随时切换

不用去添加mirror了,直接以url的形式,配置到reponsitory里便可,以下:
<repository>    
    <!-- 直接添加一个 repository,在 activeProfiles 里加上此id 便可 -->
    <id>repo1</id>    
    <name>org.maven.repo1</name>    
    <layout>default</layout>  
    <url>https://repo1.maven.org/</url>    
    <snapshots>    
        <enabled>false</enabled>    
    </snapshots>    
</repository>
 <activeProfiles>
      <activeProfile>nexus</activeProfile>
     <!-- 添加此属性,以便激活repo1的配置 -->
      <activeProfile>repo1</activeProfile>
  </activeProfiles>

 

这样,既不影响原来的结构,也不影响如今使用,在家的时候,能够将私有仓库注释掉,以提升访问速度。idea

  

注意:最后一个 activeProfiles 的属性是必须的,不然你可能发现你改了配置,然而并无什么卵用!code

<activeProfiles>
    <!-- 放心,此处的 nexus 是多个仓库的配置 -->
    <activeProfile>nexus</activeProfile>
</activeProfiles>

 

4. 没法拉取包的困惑?你可能发现,你的maven没法拉取 SNAPSHOT 包,然而包明明就在那里

是的,出于安全的考虑,maven 默认是不会去使用 snapshot 包的,因此,若是你有须要使用 snapshot 包(不少公司可能大量使用),那么你就须要配置 SNAPSHOT 为容许了!
<repository>
          <id>central</id>
          <url>http://central</url>
          <releases>
                <enabled>true</enabled>
          </releases>
          <!-- 以下开启 snapshots 功能 -->
          <snapshots>
                <enabled>true</enabled>
                <updatePolicy>always</updatePolicy>
          </snapshots>
    </repository>

5. 另附一个idea maven配置的方法

watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=

watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk= 

maven做为基础辅助工具,虽不需去深刻的理解,也没有高深的技巧,可是做为没有处理过几个相关问题的同窗,仍是颇有必要了解的。

  

在处理问题的过程,一步步成长了。

  

毕竟,资深工程师,没有解决过不少bug,是不能成为资深的。

不要惧怕今日的苦,你要相信明天,更苦!

相关文章
相关标签/搜索