使用 Docker 搭建 Maven 私服(sonatype/nexus3)
nexus3
安装 nx3 & 创建仓库
官方文档:https://help.sonatype.com/docs
docker run -d -p 8880:8081 --name nexus \
-e INSTALL4J_ADD_VM_PARAMS="-Xms2703m -Xmx2703m -XX:MaxDirectMemorySize=2703m" \
-e NEXUS_CONTEXT=nx \
sonatype/nexus3:3.37.3
# 更多参数请查看:https://hub.docker.com/r/sonatype/nexus3
- 访问
http://<your-ip>:8880/<NEXUS_CONTEXT>/
- 修改密码;禁用匿名访问
- 创建本地 blob 文件存储(类似命名空间?)
- 创建
mytools
仓库(maven2 格式 hostes 模式),指定存放位置为上一步创建的文件存储
settings.xml
配置账号密码
<server>
<id>mytools</id>
<username>admin</username>
<password>sSKBX8cxaFhZ@Zt</password>
</server>
查看创建的仓库:
- 仓库地址
- 版本策略(默认为 RELEASE,版本号中不能包含 SNAPSHOT。可在创建仓库时修改为 mixed)
发布依赖(depoly)
The deploy is the last phase of the maven lifecycle. In this phase, the build is completed and the current project is being copied to the remote repository. This makes the built project available for other projects to add as dependency or developers.
配置 pom.xml
(添加 deploy 时需要的配置)
<!--指定仓库地址 -->
<distributionManagement>
<repository>
<!-- server.id -->
<id>mytools</id>
<!--上传的位置。此处容易出错! -->
<url>http://localhost:8880/nx/repository/mytools/</url>
</repository>
</distributionManagement>
<build>
<plugins>
<!-- 发布 jar 的插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.7</version>
</plugin>
<!-- 发布源码插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
修改项目坐标(仓库默认策略是 RELEASE,所以 version 中不能有 snapshot)
<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>im.eg</groupId>
<artifactId>dms</artifactId>
<version>1.0.0-test</version>
...
# 上传(经过构建、测试、打包等步骤后上传)
mvn deploy
在其他项目中使用 nexus 仓库中的依赖
- 指定 nexus 认证信息
- 配置 pom.xml。指定仓库地址、server.id、gav 坐标
<repositories>
<repository>
<id>mynx</id>
<url>http://localhost:8880/nx/repository/mytools/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>im.eg</groupId>
<artifactId>dms</artifactId>
<version>1.0.0-abc</version>
</dependency>
</dependencies>
问题:上边 pom.xml 中指定的 nuxes 仓库地址可能要在别的地方二次指定,可否进行全局配置?
答:使用 profiles 标签进行全局配置(settings.xml 中)
<profiles>
<profile>
<id>mynxprofile</id>
<!-- 依赖仓库 -->
<repositories>
<repository>
<id>mynx</id>
<url>http://localhost:8880/nx/repository/mytools/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<!-- 插件仓库 -->
<pluginRepositories>
<pluginRepository>
<id>aliyun-nexus</id>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
...
配置完后可在 idea 的 maven 工具中指定使用的 profile
参考:https://blog.csdn.net/china_coding/article/details/128480154
沉舟侧畔千帆过,病树前头万木春。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义