搭建一个 Nexus 的 Maven 仓库

概要

搭建一个 nexus 的 maven 仓库。

博客

博客地址:IT老兵驿站

前言

今年开始深度编写 Java 代码,这样就势必会遇到搭建自己的 Maven 仓库,但是 Maven 这个东西,似乎不是那么简单,可以一蹴而就的,所以,需要好好记录一下学习的笔记。

本文记录一下使用 Nexus 来搭建 Maven 的仓库的笔记,感觉不是太好记,内容太多,先走一步,看一步吧,记录一下,总比不记录要好,看来记录笔记,是有很大的学问的。

正文

安装和启动

参考这里,安装和启动,和其他的 Java 项目一样,都是非常简单的。

Maven Repository Format 格式

Maven 仓库的格式的规范。

Version policy 版本策略

Release
Maven 仓库可以被配置为 Release 版本策略。

Snapshot
Snapshot 版本策略。

Mixed
混合策略。

Release 和 Snapshot 是 Maven 的一个概念,参考官网

What is a SNAPSHOT version?
Notice the value of the version tag in the pom.xml file shown below has the suffix: -SNAPSHOT.

在 pom.xml 中会有带有 -SNAPSHOT后缀的标记。

1
2
3
4
5
6
7
8
<project xmlns="http://maven.apache.org/POM/4.0.0"
...
<groupId>...</groupId>
<artifactId>my-app</artifactId>
...
<version>1.0-SNAPSHOT</version>
<name>Maven Quick Start Archetype</name>
...

The SNAPSHOT value refers to the ‘latest’ code along a development branch, and provides no guarantee the code is stable or unchanging. Conversely, the code in a ‘release’ version (any version value without the suffix SNAPSHOT) is unchanging.
In other words, a SNAPSHOT version is the ‘development’ version before the final ‘release’ version. The SNAPSHOT is “older” than its release.
During the release process, a version of x.y-SNAPSHOT changes to x.y. The release process also increments the development version to x.(y+1)-SNAPSHOT. For example, version 1.0-SNAPSHOT is released as version 1.0, and the new development version is version 1.1-SNAPSHOT.

SNAPSHOT 表示开发分支的最后代码,不保证代码是固定的或者不变。而位于 release 版本里面的则表示不会被修改了。

Layout policy 层级策略

Maven 仓库定义了默认的目录结构,以下的参数表示允许不允许破坏这个默认的规则。

Permissive
允许。

Strict
严格遵守。

Proxying Maven Repositories 代理 Maven 仓库

A default installation of Nexus Repository Manager includes a proxy repository configured to access the Central Repository via HTTPS using the URL https://repo1.maven.org/maven2/. To reduce duplicate downloads and improve download speeds for your developers and CI servers, you should proxy all other remote repositories you access as proxy repositories as well.

Nexus 默认会包含一个代理仓库,可以配置去访问中心仓库 https://repo1.maven.org/maven2/。这样相当于是一级缓存,不用每次都去从远程去拉取。

Hosting Maven Repositories 自主 Maven 仓库

这个地方不知道怎么去翻译这个 hosting。

这个方式可以存放第三方的库。默认会有两个仓库 maven-releases 和 maven-snapshots, 和上面的原理一样。

Grouping Maven Repositories 对仓库分组

可以把代理的和自主的仓库通过这种方式统一暴露出去。

上述这几个过程,在这个帖子都配有图来讲解,这里就不再赘述。

配置客户端

把以下内容配置在~/.m2/settings.xml文件中,这是配置访问你的仓库的凭证,并且告诉 mvn,你的仓库是作为中心仓库的一个镜像。

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
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd">

<servers>
<server>
<id>nexus-snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>nexus-releases</id>
<username>admin</username>
<password>admin123</password>
</server>
</servers>

<mirrors>
<mirror>
<id>central</id>
<name>central</name>
<url>http://your-host:8081/repository/maven-group/</url>
<mirrorOf>*</mirrorOf>
</mirror>
</mirrors>

</settings>

配置项目

配置项目依赖:

1
2
3
4
5
6
7
8
9
10
11
<project ...>

...

<repositories>
<repository>
<id>maven-group</id>
<url>http://your-host:8081/repository/maven-group/</url>
</repository>
</repositories>
</project>

配置发布(mvn deploy):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<project ...>

...

<distributionManagement>
<snapshotRepository>
<id>nexus-snapshots</id>
<url>http://your-host:8081/repository/maven-snapshots/</url>
</snapshotRepository>
<repository>
<id>nexus-releases</id>
<url>http://your-host:8081/repository/maven-releases/</url>
</repository>
</distributionManagement>
</project>

总结

至此,大体将这个过程梳理一遍,断断续续花了不少时间,Maven 的细节比较多,还是需要时间的。

参考

https://blog.sonatype.com/using-nexus-3-as-your-repository-part-1-maven-artifacts
https://help.sonatype.com/repomanager3/formats/maven-repositories#MavenRepositories-ProxyingMavenRepositories