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

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

一、从本地资源库中查找并得到依赖包,若是没有,执行第2步。java

二、从Maven默认中央仓库中查找并得到依赖包(http://repo1.maven.org/maven2/),若是没有,执行第3步。spring

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

默认中央仓库的地址:eclipse

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

二、以上地址还配有搜索页面:http://search.maven.org/spring-boot

三、若是想要向中央仓库提交本身的依赖包,能够很确定的告诉你,此功能能够实现;参考:http://www.cnblogs.com/EasonJim/p/6671419.htmlui

 

maven替换中央仓库- 阿里云

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

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

能够直接修改Mavenconf文件夹中的setting.xml文件,或者在.m2文件夹下创建一个setting·xml文件。url

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)就能够检查地址,若是实在不行,将私服地址注掉便可,就留下一个阿里云的中央仓库地址。

相关文章
相关标签/搜索