Maven 私服是一种特殊的Maven远程仓库,它是架设在局域网内的仓库服务,用来代理位于外部的远程仓库(中央仓库、其他远程公共仓库)。
建立私服后,当局域网内的用户需要某个构件时,会按照如下顺序进行请求和下载:
此外,一些无法从外部仓库下载到的构件,也能从本地上传到私服供其他人使用。
在Nexus下载所需的安装包,并解压到按照路径下即可。
以管理员身份打开CMD,进入bin目录下,执行./nexus /run命令启动,启动成功如图所示:
访问 Nexus 首页,地址为http://localhost:8081/,8081为默认端口号。
此处的用户名为admin,密码存储在nexus安装路径下admin.password文件中,如D:\DevProgramFiles\nexus-3.61.0-02-win64\sonatype-work\nexus3\admin.password文件中。
登录后的nexus设置:
设置新密码
设置是否匿名访问(禁用匿名访问)
完成配置:
仓库名称 | 说明 |
---|---|
maven-central | Nexus 对 Maven 中央仓库的代理 |
maven-public | Nexus 默认创建,供开发人员下载使用的组仓库 |
maven-releases | Nexus 默认创建,供开发人员部署自己 jar 包的宿主仓库 要求 releases 版本 |
maven-snapshots | Nexus 默认创建,供开发人员部署自己 jar 包的宿主仓库 要求 snapshots 版本 |
初始状态下,这几个仓库都没有内容。
仓库类型 | 说明 |
---|---|
proxy | 某个远程仓库的代理 |
group | 存放:通过 Nexus 获取的第三方 jar 包 |
hosted | 存放:本团队其他开发人员部署到 Nexus 的 jar 包 |
<mirror>
<id>nexus-mineid>
<mirrorOf>centralmirrorOf>
<name>Nexus minename>
<url>http://localhost:8081/repository/maven-public/url>
mirror>
由于不支持匿名访问,因此需要在settings.xml文件中的servers标签中配置访问的用户名和密码
<server>
<id>nexus-mineid>
<username>adminusername>
<password>123456password>
server>
注意: server 标签内的 id 标签值必须和 mirror 标签中的 id 值一样。
此时mvn clean compile
项目,会发现下载jar包并上传到nexus:
Downloading from nexus-mine: http://localhost:8081/repository/maven-public/org/codehaus/plexus/plexus-compiler-api/2.8.4/plexus-compiler-api-2.8.4.jar
Downloaded from nexus-mine: http://localhost:8081/repository/maven-public/commons-io/commons-io/2.5/commons-io-2.5.jar (209 kB at 11 kB/s)
Downloading from nexus-mine: http://localhost:8081/repository/maven-public/org/codehaus/plexus/plexus-compiler-manager/2.8.4/plexus-compiler-manager-2.8.4.jar
Downloaded from nexus-mine: http://localhost:8081/repository/maven-public/org/codehaus/plexus/plexus-compiler-manager/2.8.4/plexus-compiler-manager-2.8.4.jar (4.7 kB at 227 B/s)
Downloading from nexus-mine: http://localhost:8081/repository/maven-public/org/codehaus/plexus/plexus-compiler-javac/2.8.4/plexus-compiler-javac-2.8.4.jar
Downloaded from nexus-mine: http://localhost:8081/repository/maven-public/org/ow2/asm/asm/6.2/asm-6.2.jar (111 kB at 5.3 kB/s)
Downloaded from nexus-mine: http://localhost:8081/repository/maven-public/org/codehaus/plexus/plexus-java/0.9.10/plexus-java-0.9.10.jar (39 kB at 1.8 kB/s)
Downloaded from nexus-mine: http://localhost:8081/repository/maven-public/com/thoughtworks/qdox/qdox/2.0-M9/qdox-2.0-M9.jar (317 kB at 14 kB/s)
Downloaded from nexus-mine: http://localhost:8081/repository/maven-public/org/codehaus/plexus/plexus-compiler-api/2.8.4/plexus-compiler-api-2.8.4.jar (27 kB at 1.2 kB/s)
Downloaded from nexus-mine: http://localhost:8081/repository/maven-public/org/codehaus/plexus/plexus-compiler-javac/2.8.4/plexus-compiler-javac-2.8.4.jar (21 kB at 938 B/s)
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 3 source files to D:\DevWorkspace\IntelliJIDEAWS\giser-java\giser-java-common\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 05:16 min
[INFO] Finished at: 2023-12-23T20:45:41+08:00
[INFO] ------------------------------------------------------------------------
此时再次查看Nexus服务器,对应的仓库中就有了jar包了。如下:
上述下载实际上是通过Maven中央仓库下载,然后上传到nexus,再下载到本地的。为了提高下载速度,可以修改中央仓库地址为国内的镜像地址,如阿里云镜像地址。
将④处的地址改为阿里云镜像仓库地址:http://maven.aliyun.com/nexus/content/groups/public/
原始地址为:https://repo1.maven.org/maven2/
在Maven工程中配置如下信息:
<distributionManagement>
<snapshotRepository>
<id>nexus-mineid>
<name>Nexus Snapshotname>
<url>http://localhost:8081/repository/maven-snapshots/url>
snapshotRepository>
distributionManagement>
**注意:**这里 snapshotRepository 的 id 标签必须和 settings.xml 中指定的 mirror 标签的 id 属性一致。
执行部署命令mvn deploy
即可。
Uploading to nexus-mine: http://localhost:8081/repository/maven-snapshots/com/giser/giser-java-common/1.0-SNAPSHOT/giser-java-common-1.0-20231223.130426-1.pom
Uploaded to nexus-mine: http://localhost:8081/repository/maven-snapshots/com/giser/giser-java-common/1.0-SNAPSHOT/giser-java-common-1.0-20231223.130426-1.pom (1.7 kB at 4.5 kB/s)
Downloading from nexus-mine: http://localhost:8081/repository/maven-snapshots/com/giser/giser-java-common/maven-metadata.xml
Uploading to nexus-mine: http://localhost:8081/repository/maven-snapshots/com/giser/giser-java-common/1.0-SNAPSHOT/maven-metadata.xml
Uploaded to nexus-mine: http://localhost:8081/repository/maven-snapshots/com/giser/giser-java-common/1.0-SNAPSHOT/maven-metadata.xml (770 B at 1.1 kB/s)
Uploading to nexus-mine: http://localhost:8081/repository/maven-snapshots/com/giser/giser-java-common/maven-metadata.xml
Uploaded to nexus-mine: http://localhost:8081/repository/maven-snapshots/com/giser/giser-java-common/maven-metadata.xml (284 B at 361 B/s)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:53 min
[INFO] Finished at: 2023-12-23T21:04:29+08:00
[INFO] ------------------------------------------------------------------------
Process finished with exit code 0
此时再次查看Nexus服务器,对应的仓库中就有部署的jar包了。如下:
在工程中配置仓库地址,如下:
<repositories>
<repository>
<id>nexus-mineid>
<name>nexus-minename>
<url>http://localhost:8081/repository/maven-snapshots/url>
<snapshots>
<enabled>trueenabled>
snapshots>
<releases>
<enabled>trueenabled>
releases>
repository>
repositories>
这样就可以通过坐标直接引用jar包了。