maven自定義遠程倉庫


  

  本地倉庫(.m2) vs. 遠程倉庫(聯網)

    運行Maven的時候,Maven所需要的任何構件都是直接從本地倉庫獲取的。如果本地倉庫沒有,它會首先嘗試從遠程倉庫下載構件至本地倉庫,

  然后再使用本地倉庫的構件。如果嘗試過所有遠程倉庫之后,Maven還是沒能夠下載到該文件,它就會報錯。

  Maven缺省的本地倉庫地址為${user.home}/.m2/repository 。也就是說,一個用戶會對應的擁有一個本地倉庫。

  也可以自定義本地倉庫的位置,修改${user.home}/.m2/settings.xml :

  <!-- localRepository
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ${user.home}/.m2/repository-->
  <localRepository>D:/.m2/repository</localRepository>

  還可以在運行時指定本地倉庫位置:

  mvn clean install -Dmaven.repo.local=/home/juven/myrepo/

 

  1. 確定使用安裝版maven or eclipse Embedded maven

       

  2. eclipse Embedded maven

  eclipse安裝后會有兩個目錄 .eclipse 和 .p2

  所有的插件都在 .p2目錄里

  

  3. Super POM

  maven項目中的pom.xml繼承於Super POM.

  官網的描述:http://maven.apache.org/ref/3.0.4/maven-model-builder/super-pom.html

  其中倉庫配置:

  <repositories>
    <repository>
      <id>central</id>
      <name>Central Repository</name>
      <url>http://repo.maven.apache.org/maven2</url>
      <layout>default</layout>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>
  </repositories>

  <pluginRepositories>
    <pluginRepository>
      <id>central</id>
      <name>Central Repository</name>
      <url>http://repo.maven.apache.org/maven2</url>
      <layout>default</layout>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <releases>
        <updatePolicy>never</updatePolicy>
      </releases>
    </pluginRepository>
  </pluginRepositories>

 

  4. Super POM位於maven.model.builder-3.6.3.jar里 (在maven2里,maven-2.0.10-uber.jar)

    ->安裝版maven的maven.model.builder-3.6.3.jar

    

    ->eclipse maven插件的maven.model.builder-3.6.3.jar

    

    ->maven.model.builder-3.6.3.jar中的Super POM(pom-4.0.0.xml)

    

  

  5. 通過項目中的pom.xml中添加配置,修改(添加)倉庫源  

   好處是跟着項目走,壞處是所有需要的項目都要加。

   此方法測了一下估計是覆蓋SuperPOM.

 例如改為阿里雲的:
<repositories> <repository> <id>aliyun</id> <url>https://maven.aliyun.com/repository/public</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>aliyun</id> <url>https://maven.aliyun.com/repository/public</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories

 

  6. 通過settings.xml中配置遠程倉庫,修改(添加)倉庫源

   注意1:eclipse 插件maven默認沒有這個文件,需要自己創建。安裝的maven在apache-mavne-3.6.3 > conf > settings.xml。

   注意2: 在Maven安裝目錄的conf子目錄下面的settings.xml是全局的配置。而用戶目錄的.m2子目錄下面的settings.xml的配置只是針對當前用戶的。

       當這兩個文件同時存在的時候,相同的配置信息用戶目錄下面的settings.xml會覆蓋Maven安裝目錄下面的settings.xml中的定義。

       用戶目錄下的settings.xml文件一般是不存在的,但是Maven允許我們在這里定義我們自己的settings.xml。

 

   兩種方式:profiles/mirror,詳細細節可以參考安裝版的settings.xml

   使用profiles: (先從此profile下載,如果沒有再通過SuperPom指定的中央倉,sources and javadoc也是這樣)

<settings>
  ...
  <profiles>
    <profile>
      <id>dev</id>
      <!-- repositories and pluginRepositories here-->
    </profile>
  </profiles>
  <activeProfiles>
    <activeProfile>dev</activeProfile>
  </activeProfiles>
  ...
</settings>

    使用mirror:   (這將覆蓋central倉庫配置)

<settings>
...
  <mirrors>
    <mirror>
      <id>maven-net-cn</id>
      <name>Maven China Mirror</name>
      <url>http://maven.net.cn/content/groups/public/</url>
      <mirrorOf>central</mirrorOf>
    </mirror>
  </mirrors>
...
</settings>

 

<settings>
...
  <mirrors>
    <mirror>
      <id>my-org-repo</id>
      <name>Repository in My Orgnization</name>
      <url>http://192.168.1.100/maven2</url>
      <mirrorOf>*</mirrorOf>
    </mirror>
  </mirrors>
...
</settings>

 

參考:https://www.cnblogs.com/tinyj/p/9803081.html

    


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM