Maven 仓库管理

作者: data4 | 来源:发表于2017-02-15 14:41 被阅读291次

本节主要介绍如何配置 Maven 仓库,发布 jar 包到 Maven 仓库

Maven 仓库是什么?

Maven 仓库用来存储绝大部分的开源 Jar 包,当你在 pom.xml 文件中使用 dependency 元素引入某个依赖时,Maven 会自动从仓库内下载相关 Jar 包。

Maven 仓库有哪些

Maven 仓库

本地仓库

本地仓库指的是 ${user_home}/.m2/repository/,Maven 默认会先从本地仓库内寻找所需 Jar 包。如果本地仓库不存在,Maven 才会向远程仓库请求下载,同时缓存到本地仓库。

远程仓库

  • 私服
    为了节省资源,一般是局域网内设置的私有服务器,当本地仓库内不存在 Maven 所需 Jar 包时,会先去私服上下载 Jar 包。
  • 中央仓库
    是 Maven 自带的远程仓库,不需要特殊配置。如果私服上也不存在 Maven 所需 Jar 包,那么就去中央仓库上下载 Jar 包,同时缓存在私服和本地仓库。

配置远程 Maven 仓库

一般情况下,配置远程 Maven 仓库分为两种:配置私服,每家公司都会配置自己的 Maven 私服仓库,这样可以节省流量,提高效率;配置第三方公共库。

配置私服

私服的配置一般是适用某个用户的,那么可以把它存放到 .m2/settings.xml 文件中。

<settings>
  <profiles>
      <profile>
        <id>nexus</id>
        <repositories>
          <repository>
            <id>nexus-releases</id>
            <name>local private releases repository</name>
            <url>${maven release repository url}</url>
            <releases>
              <enabled>true</enabled>
            </releases>
            <snapshots>
              <enabled>false</enabled>
            </snapshots>
          </repository>

          <repository>
            <id>nexus-snapshots</id>
            <name>local private snapshots</name>
            <url>${maven snapshot repository url}</url>
          </repository>
        </repositories>
        
        <pluginRepositories>
          <pluginRepository>
            ......
          </pluginRepository>
        </pluginRepositories>
      </profile>
  </profiles>

  <activeProfiles>
      <activeProfile>nexus</activeProfile>
  </activeProfiles>

  <servers>
    <server>
      <id>nexus-releases</id>
      <username>${username}</username>
      <password>${password}</password>
    </server>
    <server>
      <id>nexus-snapshots</id>
      <username>${username}</username>
      <password>${password}</password>
    </server>
  </servers>
</settings>
  • profile
    是将 repository, pluginRepository, properties, activation 一组元素组合成一个整体来使用,其中 repository 指向某个私服。
  • activeProfiles
    激活 profile
  • server
    是用来配置 repository,pluginRepository 中的用户名和密码,其中 server id = profile repository id。

配置第三方公共库

如果某个 Jar 包在第三方公共库上,该如何配置呢?这种情况多数是针对某个项目的特殊需求,而不具有普遍性,因此将该仓库配置在工程的 pom.xml 文件中。

<project>
......
  <repositories>
    <repository>
      <id>jboss</id>
      <name>JBoss Repository</name>
      <layout>default</layout>
      <url>${jboss repository url}</url>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>
  </repositories>
.....
</project>

发布 jar 到远程仓库

我们知道如果想发布 Jar 包的话,需要执行的 Maven 命令是

mvn clean deploy

那么该如何配置将 jar 包发布到哪个仓库里呢?这种情况也是项目可见的,所以只需要在工程的 pom.xml 文件内配置即可。

<project>
    ......
    <distributionManagement>
        <repository>
            <id>nexus-releases</id>
            <name>Nexus Repository</name>
            <url>${release respository url}</url>
        </repository>
        <snapshotRepository>
            <id>nexus-snapshots</id>
            <name>Nexus Repository</name>
            <url>${snapshot repository url}</url>
        </snapshotRepository>
    </distributionManagement>
    ......
</project>

其中对应仓库用户认证信息不应该保存在 pom.xml 文件内,而应该保存在 .m2/setting.xml 中。其中 server id = repository id

<settings>
    ......
    <servers>
        <server>
            <id>nexus-releases</id>
            <username>${username}</username>
            <password>${password}</password>
        </server>
        <server>
            <id>nexus-snapshots</id>
            <username>${username}</username>
            <password>${password}</password>
        </server>
    </servers>
    ......
</settings>

Snapshots vs Releases

一般仓库至少会分为两个,Releases 和 Snapshots。

  • Releases 仓库
    Releases 是稳定版本的仓库,一般只有测试运行稳定的版本才会发布到 Releases 仓库上。该版本一旦发布之后,再次发布将失败,不会覆盖当前版本。
  • Snapshots 仓库
    Snapshots 是快照版本,一般适用于不稳定状态,版本号多以后缀 -SNAPSHOT 结尾。可以多次发布相同的快照版本,会覆盖之前发布的 Jar 包。例如:某个 Jar 当前稳定版本是 1.0.0,那么开发版本应该是 1.0.1-SNAPSHOT。
  • 版本号管理
    如果某 Jar 当前稳定版本是 1.0.0,那么下一个不稳定版本是 1.0.1-SNAPSHOT,待 1.0.1-SNAPSHOT 版本稳定后,再发布稳定版 1.0.1。

相关文章

  • 25: Nexus介绍及安装

    简介 Nexus是Maven仓库管理器,也可以叫Maven的私服。Nexus是一个强大的Maven仓库管理器,它极...

  • centos-Maven库搭建

    Nexus是Maven仓库管理器,也可以叫Maven的私服。Nexus是一个强大的Maven仓库管理器,它极大地简...

  • nexus的安装

    Nexus介绍 Nexus 是Maven仓库管理器,如果你使用Maven,你可以从Maven中央仓库 下载所需要的...

  • CentOS 7.2 搭建nexus私服

    Nexus 是Maven仓库管理器,如果你使用Maven,你可以从Maven中央仓库 下载所需要的构件(artif...

  • Maven私服Nexus安装与使用

    Nexus介绍 Nexus是Maven仓库管理器,如果你使用Maven,你可以从Maven中央仓库下载所需要的构件...

  • Nexus搭建自己的Maven私服

    Nexus是Maven仓库管理器,管理开发所需要的构件。如果你每次都是从Apache提供的Maven中央仓库去下载...

  • Maven 仓库管理

    本节主要介绍如何配置 Maven 仓库,发布 jar 包到 Maven 仓库 Maven 仓库是什么? Maven...

  • Maven仓库管理

    摘要:Maven,本地仓库,远程仓库,仓库配置,仓库认证,镜像 为什么需要仓库 在Maven中任何一个依赖,插件或...

  • 使用nexus搭建maven私库

    什么是nexus? nexus是一个maven仓库管理器,使用nexus可以快速便捷的搭建自己的maven私有仓库...

  • Maven(5)Nexus

    前言 Maven是Java项目管理的利器,使用私服来管理Maven仓库是Maven使用的一条最佳实践。私服是内网的...

网友评论

    本文标题:Maven 仓库管理

    本文链接:https://www.haomeiwen.com/subject/kgrxwttx.html