1. 程式人生 > 實用技巧 >maven配置多倉庫的方法

maven配置多倉庫的方法

轉載:https://www.cnblogs.com/yougewe/p/7294265.html

目錄


  剛接觸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改掉就行了,如:

 <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. 新增一個類似結構的倉庫配置,這樣的話就不切來切去的了,一勞永逸。

  相當於添加了多倉庫,如下:

<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將會很慢,嚴重影響心情,所以,其實是不建議這麼幹的。

返回頂部

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>

  這樣,既不影響原來的結構,也不影響現在使用,在家的時候,可以將私有倉庫註釋掉,以提高訪問速度。

  注意: 最後一個 activeProfiles 的屬性是必須的,否則你可能發現你改了配置,然而並沒有什麼卵用!

<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.一個完整的配置樣例參考

  前面說的基本是從解決問題的思路去講解,可能還是不夠形象,或者每個人的理解總是有些差異,下面來一個完整的配置參考。你可以在這上面繼續任意發揮:

<?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>${user.home}/.m2/repository</localRepository>
  <servers>
    <!-- 賬號資訊保留,私服使用 -->
    <server>
        <id>sec@public-snapshots</id>
        <username>snapshot_user</username>
        <password>snapshot123</password>
    </server>
    <server>
        <id>mirror-sec</id>
        <username>snapshot_user</username>
        <password>snapshot123</password>
    </server>
  </servers>

  <!-- 註釋掉該mirror 配置項 -->
  <!-- <mirrors>
    <mirror>
      <id>mirror-sec</id>
      <mirrorOf>sec@public-snapshots,mirror-sec</mirrorOf>
      <url>http://maven.sec-ins.com.cn/repository/maven-public/</url>
    </mirror>
  </mirrors>
  -->
  
 
  <profiles>
   <profile>
      <id>sec</id>
      <!-- 註釋掉該預設啟用,統一在 activeProfiles 中配置-->
      <!-- <activation>
          <activeByDefault>true</activeByDefault>
      </activation> -->
      
      <repositories> 
        <!-- 直接新增一個 repository, 執行maven更新時,就會先嚐試使用該repo進行拉取了
            可新增任意多個倉庫,但如果網路不通,這樣會導致很長時間的切換重試,可在ide中檢視maven正在嘗試哪個repository
        -->  
        <repository>    
            <id>repo1</id>    
            <name>org.maven.repo1</name>    
            <layout>default</layout>  
            <url>https://repo1.maven.org/</url>
            <!-- <url>http://maven.aliyun.com/nexus/content/groups/public</url> -->   
            <snapshots>    
                <enabled>false</enabled>    
            </snapshots>    
        </repository>
        <!-- 公司的配置保留,新增自定義的倉庫即可 -->
           <repository> 
            <id>sec@public-snapshots</id> 
            <name>sec-snapshots</name> 
            <url>http://maven.sec-ins.com.cn/repository/maven-public</url> 
            <releases>
                <enabled>true</enabled>
                <updatePolicy>always</updatePolicy>
                <checksumPolicy>warn</checksumPolicy>
            </releases> 
            <snapshots>
                <enabled>true</enabled>
            </snapshots> 
        </repository>
      </repositories>
      </profile>
      <!-- 註釋掉該plugin 配置 -->
      <!-- <pluginRepositories> 
        <pluginRepository> 
            <id>sec@public-snapshots</id> 
            <name>sec_plugin</name> 
            <url>http://maven.sec-ins.com.cn/repository/maven-public</url> 
            <releases>
                <enabled>true</enabled>
                <updatePolicy>always</updatePolicy>
                <checksumPolicy>warn</checksumPolicy>
            </releases> 
            <snapshots>
                <enabled>true</enabled>
            </snapshots> 
        </pluginRepository>       
       </pluginRepositories>
       -->
    <!-- 你也可以另開一個 profile, 儲存自定義的 repositories, 在 activeProfiles 里加上此id 即可遍歷以下 repository -->
   <profile>
      <id>my-profile</id>
      <repositories>    
        <repository>
            <id>alirepo1</id>    
            <name>org.maven.repo1</name>    
            <layout>default</layout>  
            <!-- <url>https://repo1.maven.org/</url> -->
            <url>http://maven.aliyun.com/nexus/content/groups/public</url>
            <snapshots>    
                <enabled>false</enabled>    
            </snapshots>    
        </repository>
        
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>repository.springframework.maven.release</id>
            <name>Spring Framework Maven Release Repository</name>
            <url>http://maven.springframework.org/milestone/</url>
        </repository>
        <repository>
            <id>org.springframework</id>
            <url> http://maven.springframework.org/snapshot</url>
        </repository>
        <repository>
            <id>spring-milestone</id>
            <name>Spring Maven MILESTONE Repository</name>
            <url>http://repo.spring.io/libs-milestone</url>
        </repository>
        <repository>
            <id>spring-release</id>
            <name>Spring Maven RELEASE Repository</name>
            <url>http://repo.spring.io/libs-release</url>
        </repository>
      </repositories>
      </profile>
  </profiles>

  <activeProfiles>
    <!-- 新增此屬性,需要啟用自定義的repo 的profile配置 -->
      <activeProfile>sec</activeProfile>
      <activeProfile>my-profile</activeProfile>
  </activeProfiles>
</settings>

返回頂部

6. 另附一個idea maven配置的方法

  maven作為基礎輔助工具,雖不需去深入的理解,也沒有高深的技巧,但是作為沒有處理過幾個相關問題的同學,還是很有必要了解的。

  在處理問題的過程,一步步成長了。

  畢竟,資深工程師,沒有解決過很多bug,是不能成為資深的。