转:Maven的默认中央仓库以及修改默认仓库&配置第三方jar包从私服下载

当构建一个Maven项目时,首先检查pom.xml文件以确定依赖包的下载位置,执行顺序如下:

1、从本地资源库中查找并获得依赖包,如果没有,执行第2步。

2、从Maven默认中央仓库中查找并获得依赖包(http://repo1.maven.org/maven2/),如果没有,执行第3步。

3、如果在pom.xml中定义了自定义的远程仓库,那么也会在这里的仓库中进行查找并获得依赖包,如果都没有找到,那么Maven就会抛出异常。

默认中央仓库的地址:

1、http://repo1.maven.org/maven2/

2、以上地址还配有搜索页面:http://search.maven.org/

3、如果想要向中央仓库提交自己的依赖包,可以很肯定的告诉你,此功能可以实现;参考:http://www.cnblogs.com/EasonJim/p/6671419.html

maven替换中央仓库- 阿里云

在国内访问Maven仓库,连接速度太慢。下面是将中央仓库替换成阿里云的中央仓库的方法。

第一种,统一修改仓库地址

可以直接修改Mavenconf文件夹中的setting.xml文件,或者在.m2文件夹下建立一个setting·xml文件。

setting.xml里面有个mirrors节点,用来配置镜像URL。mirrors可以配置多个mirror,每个mirror有id,name,url,mirrorOf属性。

  • id是唯一标识一个mirror
  • name貌似没多大用,相当于描述
  • url是官方的库地址
  • mirrorOf代表了一个镜像的替代位置,例如central就表示代替官方的中央库。

mirror也不是按settings.xml中写的那样的顺序来查询的。所谓的第一个并不一定是最上面的那个。

当有id为B,A,C的顺序的mirror在mirrors节点中,maven会根据字母排序来指定第一个,所以不管怎么排列,一定会找到A这个mirror来进行查找,当A无法连接,出现意外的情况下,才会去B查询。

在setting·xml中添加如下代码:

复制代码
...
<mirrors>  
    ...   
    <mirror>  
      <id>alimaven</id>  
      <name>aliyun maven</name>  
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>  
      <mirrorOf>central</mirrorOf>          
    </mirror>
</mirrors>
复制代码

image

第二种,分别给每个项目配置不同的中央库

直接在项目的pom.xml中修改中央库的地址。如下:

复制代码
<repositories>
    <repository>
        <id>alimaven</id>
        <name>aliyun maven</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
复制代码

完整的pom:

复制代码
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.xiaolyuh</groupId>
    <artifactId>spring-boot-student</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
    <name>spring-boot-student</name>

    <!-- 添加Spring Boot的父类依赖,这样当前项目就是Spring Boot项目了。 spring-boot-starter-parent是一个特殊的starter,他用来 
        提供相关的maven默认依赖, 使用它之后,常用的依赖可以省去version标签 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.3.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <repositories>
        <repository>
            <id>alimaven</id>
            <name>aliyun maven</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
        </repository>
    </repositories>

    <!-- 或者在maven的setting文件中加入 -->
    <!--<mirror>
        <id>alimaven</id>
        <name>aliyun maven</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
        <mirrorOf>central</mirrorOf>
    </mirror>-->

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <modules>
        <module>spring-boot-student-banner</module>
    </modules>

</project>
复制代码

补充:设置我们第三方jar包的下载地址为私服地址的方法(如果私服仓库不存在jar包就会从私服的代理镜像下载地址并保存到私服仓库)

  如果你想讲jar包下载改为从私服下载也是修改这里,前提是先搭建好私服,然后从后台设置私服仓库的远程地址为阿里云的镜像地址即可:

(1)在私服的后台设置仓库的镜像地址(用于私服不存在时候从该地址下载)

(2)修改maven的settings.xml的配置文件:配置下载地址

复制代码
  <mirrors>
    <mirror>
      <id>nexus</id>
      <name>internal nexus repository</name>
      <mirrorOf>*</mirrorOf>
      <url>http://localhost:8081/nexus/content/repositories/central/</url>
    </mirror>
  </mirrors>
复制代码

(3)我们在eclipse也可以查看默认使用的下载地址:

(4)当我们引入一个不存在的jar包的时候会先下载到私服的仓库地址,然后下载到本地的仓库地址,如下:

  如果maven项目下载不下来jar包先检查一下下载地址,eclipse中很好检查,用上面(3)就可以检查地址,如果实在不行,将私服地址注掉即可,就留下一个阿里云的中央仓库地址。

原文地址:https://www.cnblogs.com/jinloooong/p/10649223.html