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,是不能成爲資深的。

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

相關文章
相關標籤/搜索