一点一点学maven(03)——maven的坐标、构件、仓库(中央仓库、镜像仓库、私服)使用

1、构件:

在maven里面,任何一个依赖、插件或者项目构建的输出,都可以称之为构件。

2、仓库:

2.1、仓库概念: 
存储构件的位置,用来管理项目依赖、插件等。

2.2、仓库分类: 
maven的仓库分为两大类: 
1)、本地仓库 
2)、远程仓库,在远程仓库中又分成了3种:

  • 中央仓库:http://repo1.maven.org/maven2/
  • 私服:内网自建的maven repository,其URL是一个内部网址
  • 其他公共仓库:其他可以互联网公共访问maven repository,例如 jboss repository等

1)、本地仓库:

maven本地仓库的默认位置:用户的目录下.m2\repository\目录。

修改maven本地仓库默认位置:找到MAVEN_HOME目录下conf文件夹里面的settings.xml文件,设置settings.xml文件中localRepository元素的值为新的仓库地址(大概位于53行):

<settings>  
 <localRepository>D:/maven_new_repository</localRepository>  
</settings>  
  • 1
  • 2
  • 3



2)、远程仓库:

2.1)、中央仓库:

maven安装后,自带中央仓库的配置,所有的maven项目都会继承超级pom,超级pom位置在maven\lib\maven-model-builder-x.x.x.jar中,解压文件在\org\apache\maven\model目录中即可找到,包含的中央仓库配置部分如下:

  <repositories>
    <repository>
      <!-- id为当前中央仓库的id:central -->
      <id>central</id>
      <name>Central Repository</name>
      <url>https://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>https://repo.maven.apache.org/maven2</url>
      <layout>default</layout>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <releases>
        <updatePolicy>never</updatePolicy>
      </releases>
    </pluginRepository>
  </pluginRepositories>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

https://repo.maven.apache.org/maven2 即为maven提供的中央仓库地址,搜索地址:http://search.maven.org 


2.2)、私服:

私服是架设在局域网的一种特殊的远程仓库,目的是代理远程仓库及部署第三方构件。

有了私服之后,当 Maven 需要下载构件时,直接请求私服,私服上存在则下载到本地仓库;否则,私服请求外部的远程仓库,将构件下载到私服,再提供给本地仓库下载。



maven私服优点:

1.节省外网带宽:减少重复请求造成的外网带宽消耗;

2.加速Maven构件:如果项目配置了很多外部远程仓库的时候,构建速度就会大大降低;

3.部署第三方构件:有些构件无法从外部仓库获得的时候,我们可以把这些构件部署到内部仓库(私服)中,供内部maven项目使用;

4.提高稳定性,增强控制:Internet不稳定的时候,maven构建也会变的不稳定,一些私服软件还提供了其他的功能;

5.降低中央仓库的负荷:maven中央仓库被请求的数量是巨大的,配置私服也可以大大降低中央仓库的压力。

当前主流的maven私服: 
1.Apache的Archiva 
2.JFrog的Artifactory 
3.Sonatype的Nexus



配置使用maven私服(假如本地已经搭建了maven的nexus私服):

只让当前项目使用私服,在当前项目pom.xml中添加私服配置:

<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.maven.demo</groupId>
    <artifactId>project</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <!-- 配置私服依赖地址 -->
    <repositories>
        <repository>
            <id>nexus</id>
            <name>Team Nexus Repository</name>      <url>http://localhost:8081/nexus/content/groups/public</url>
        </repository>
    </repositories>

    <!-- 配置私服插件地址 -->
    <pluginRepositories>
        <pluginRepository>
            <id>nexus</id>
            <name>Team Nexus Repository</name>                
    <url>http://localhost:8081/nexus/content/groups/public</url>
        </pluginRepository>
    </pluginRepositories>

    <dependencies>
        <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.10</version>
        </dependency>

    </dependencies>
</project>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

让所有本机的maven项目使用私服,修改settings.xml文件:

  <profiles>
    <profile>
      <repositories>
        <repository>
          <id>nexus</id>
            <name>Team Nexus Repository</name>      <url>http://localhost:8081/nexus/content/groups/public</url>
          <layout>default</layout>
          <snapshotPolicy>always</snapshotPolicy>
        </repository>
      </repositories>
    </profile>
  </profiles>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

参照文档: 
maven–私服的搭建(Nexus的使用)和注意的问题,区分项目和本机以及项目发布权限配置 
使用Nexus搭建Maven私服,私服搭建,只应用本项目 
私服搭建及服务配置



2.2)、镜像仓库: 
maven的中央仓库服务器都是位于国外的,有时因为无法访问外网,国内也有和中央仓库一样的镜像仓库。

mirror相当于一个拦截器,它会拦截maven对remote repository(远程仓库)的相关请求,把请求里的remote repository地址,重定向到mirror里配置的地址。

修改使用镜像仓库: 
打开settings.xml文件,找到<mirrors>(大概位于146行),直接复制示例并修改:

  <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>mirrorId</id>
      <mirrorOf>repositoryId</mirrorOf>
      <name>Human Readable Name for this Mirror.</name>
      <url>http://my.repository.com/repo/path</url>
    </mirror>
     -->
    <mirror>
      <!-- id为当前镜像仓库的id -->
      <id>maven.net.cn</id>
      <!-- mirrorOf指镜像(替换访问或覆盖)哪个中央仓库,central为上面超级pom的中央仓库的id -->
      <!-- 也可配置为*,表示所有外部仓库访问都转到当前镜像仓库 -->
      <mirrorOf>central</mirrorOf>
      <name>Central Mirror in China</name>
      <!-- 镜像仓库的地址 -->
      <url>http://maven.net.cn/content/groups/public</url>
    </mirror>
  </mirrors>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

补充:mirror和repository 区别

3、坐标:

3.1、坐标概念: 
任何的构件都有唯一的坐标,Maven根据这个坐标定义了构件在仓库中的唯一存储路径。 


3.2、maven坐标组成:

maven 的坐标通过 5 个元素进行定义:groupId、artifactId、version、packaging、classifier等基本坐标共同定义的,其中 groupId、artifactId、version 是必须的,packaging 是可选的(默认为jar),classifier 是不能直接定义的。 


3.3、maven坐标组成元素含义:

groupId:定义当前 Maven 项目所属的实际项目,跟 Java 包名类似,通常与域名反向一一对应。

artifactId:定义当前 Maven 项目的一个模块,默认情况下,Maven 生成的构件,其文件名会以 artifactId 开头,如 hibernate-core-3.6.5.Final.jar。

version:定义项目版本。

packaging:定义项目打包方式,如 jar,war,pom,zip ……,默认为 jar。

classifier:定义项目的附属构件,如 hibernate-core-3.6.6.Final-sources.jar,hibernate-core-3.6.6.Final-javadoc.jar,其中 sources 和 javadoc 就是这两个附属构件的 classifier。classifier 不能直接定义,通常由附加的插件帮助生成。 


3.4、maven坐标与仓库中的存储路径关系:

maven坐标如图: 
这里写图片描述

1)、基于groupId,将点分隔符替换为路径分隔符,就是将 “.” 转换成 “\” ,如: com.maven.demo —>com\maven\demo

2)、基于artifactId,将artifactId连接到后面,注意添加路径分隔符:com\maven\demo\project

3)、基于version,将version连接到后面,注意添加路径分隔符:com\maven\demo\project\0.0.1-SNAPSHOT

4)、将artifactId、version以中横线”-“作为分隔符连接到后面,注意添加路径分隔符:com\maven\demo\test-project\0.0.1-SNAPSHOT\project-0.0.1-SNAPSHOT

5)、基于packaging,将packaging以点分隔符连接到后面:com\maven\demo\test-project\0.0.1-SNAPSHOT\project-0.0.1-SNAPSHOT.jar

6)、判断构件如果有classifier,要在第4项后,classifier以中横线”-“作为分隔符连接到后面:com\maven\demo\test-project\0.0.1-SNAPSHOT\project-0.0.1-SNAPSHOT-sources.jar 


参看文档: 
Maven详解之仓库——本地仓库、远程仓库


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值