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

    

posted @ 2020-07-08 16:04  jason47  阅读(2345)  评论(0编辑  收藏  举报