zoukankan      html  css  js  c++  java
  • Maven仓库配置

    1.什么是Maven仓库

    Maven仓库就是放置所有JAR文件(WAR,ZIP,POM等等)的地方,所有Maven项目可以从同一个Maven仓库中获取自己所需要的依赖JAR,这节省了磁盘资源。此外,由于Maven仓库中所有的JAR都有其自己的坐标,该坐标告诉Maven它的组ID,构件ID,版本,打包方式等等,因此Maven项目可以方便的进行依赖版本管理。你也不在需要提交JAR文件到SCM仓库中,你可以建立一个组织层次的Maven仓库,供所有成员使用。
    简言之,Maven仓库能帮助我们管理构件(主要是JAR)。

    2.本地仓库(.m2)

    运行Maven的时候,Maven所需要的任何构件都是直接从本地仓库获取的。如果本地仓库没有,它会首先尝试从远程仓库下载构件至本地仓库,然后再使用本地仓库的构件。

    比如说,你的项目配置了junit-3.8的依赖,在你运行mvn test 的时候,Maven需要使用junit-3.8的jar文件,它首先根据坐标查找本地仓库,如果找到,就直接使用。如果没有,Maven会检查可用的远程仓库配置,然后逐个尝试这些远程仓库去下载junit-3.8的jar文件,如果远程仓库存在该文件,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>

    3.远程仓库

    Maven缺省的远程仓库,即Maven中央仓库。

    中央仓库的id为central,远程url地址为http://repo.maven.apache.org/maven2,它关闭了snapshot版本构件下载的支持。

    1>  在POM中配置远程仓库

    前面我们看到超级POM配置了ID为central的远程仓库,我们可以在POM中配置其它的远程仓库。这样做的原因有很多,比如你有一个局域网的远程仓库,使用该仓库能大大提高下载速度,继而提高构建速度,也有可能你依赖的一个jar在central中找不到,它只存在于某个特定的公共仓库,这样你也不得不添加那个远程仓库的配置。
    这里我配置一个远程仓库指向中央仓库的中国镜像:

    <project>
    ...
      <repositories>
        <repository>
          <id>maven-net-cn</id>
          <name>Maven China Mirror</name>
          <url>http://maven.net.cn/content/groups/public/</url>
          <releases>
            <enabled>true</enabled>
          </releases>
          <snapshots>
            <enabled>false</enabled>
          </snapshots>
        </repository>
      </repositories>
      <pluginRepositories>
        <pluginRepository>
          <id>maven-net-cn</id>
          <name>Maven China Mirror</name>
          <url>http://maven.net.cn/content/groups/public/</url>
          <releases>
            <enabled>true</enabled>
          </releases>
          <snapshots>
            <enabled>false</enabled>
          </snapshots>    
        </pluginRepository>
      </pluginRepositories>
    ...
    </project>

    我们先看一下<repositories>的配置,你可以在它下面添加多个<repository> ,每个<repository>都有它唯一的ID,一个描述性的name,以及最重要的,远程仓库的url。此外,<releases><enabled>true</enabled></releases>告诉Maven可以从这个仓库下载releases版本的构件,而<snapshots><enabled>false</enabled></snapshots>告诉Maven不要从这个仓库下载snapshot版本的构件。禁止从公共仓库下载snapshot构件是推荐的做法,因为这些构件不稳定,且不受你控制,你应该避免使用。当然,如果你想使用局域网内组织内部的仓库,你可以激活snapshot的支持。

    2>  在settings.xml中配置远程仓库
    我们知道了如何在POM中配置远程仓库,但考虑这样的情况:在一个公司内部,同时进行这3个项目,而且以后随着这几个项目的结束,越来越多的项目会开始;同时,公司内部建立一个Maven仓库。我们统一为所有这些项目配置该仓库,于是不得不为每个项目提供同样的配置。问题出现了,这是重复 !
    其实我们可以做到只配置一次,在哪里配置呢?就是settings.xml。
    不过事情没有那么简单,不是简单的将POM中的<repositories>及<pluginRepositories>元素复制到settings.xml中就可以,setting.xml不直接支持 这两个元素。但我们还是有一个并不复杂的解决方案,就是利用profile,如下:

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

    这里我们定义一个id为dev的profile,将所有repositories以及pluginRepositories元素放到这个profile中,然后,使用<activeProfiles>元素自动激活该profile。这样,你就不用再为每个POM重复配置仓库。
    使用profile为settings.xml添加仓库提供了一种用户全局范围的仓库配置。

    3>  使用镜像

    如果你的地理位置附近有一个速度更快的central镜像,或者你想覆盖central仓库配置,或者你想为所有POM使用唯一的一个远程仓库(这个远程仓库代理的所有必要的其它仓库),你可以使用settings.xml中的mirror配置。
    以下的mirror配置用maven.NET.cn覆盖了Maven自带的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>

    这里唯一需要解释的是<mirrorOf>,这里我们配置central的镜像,我们也可以配置一个所有仓库的镜像,以保证该镜像是Maven唯一使用的仓库:

      <mirrors>
        <!-- mirror
         | Specifies a repository mirror site to use instead of a given repository. The repository that
         | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
         | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
         |
         -->
        <mirror>
          <id>nexus</id>
          <mirrorOf>*</mirrorOf>
          <name>Nexus Mirror</name>
          <url>http://xx.xx/nexus/content/groups/public</url>
        </mirror>
      </mirrors>
    <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/gengaixue/p/6933773.html

  • 相关阅读:
    hexo常用命令笔记
    给hexo添加评论系统
    npm 使用代理
    几种网页重定向(自动跳转)的方法
    使用hexo搭建github博客
    在Pythonanywhere上部署Django
    Apache .htaccess语法之RewriteRule
    为Github项目创建文档
    Windows下WordPress搭建博客过程
    Windows下Discuz搭建论坛过程
  • 原文地址:https://www.cnblogs.com/xulz/p/10072156.html
Copyright © 2011-2022 走看看