前言
今天跟大家分享的是maven私库的搭建,其实按道理我们这个小庙可以不用搞的,但是自从上次找兼职来赶项目后,这个问题我也思考了。没有这个私库,那来兼职的小伙伴,代码就是给他们了,没有一点秘密可言,就算签的那个保密协议有什么用呢?首先不说谈支付保密费用没有,就说泄漏了,又能知道具体是谁吗?
所以我才觉得maven私库必须得搞,能增加一道防线是一道,当然所有的防护措施都没有绝对。
一、maven私库搭建
这里选择的是Nexus方案。
下载安装
下载最新版(以3.x为例)
wget https://download.sonatype.com/nexus/3/latest-unix.tar.gz解压
tar -zxvf latest-unix.tar.gz目录结构
nexus-3.x.x/ # 包含运行程序
sonatype-work/ # 包含数据文件。
启动 Nexus
# 进入目录
cd nexus-3.x.x/bin
# 启动(Linux/Mac)
./nexus start
# Windows
nexus.exe /run
访问配置
默认访问地址:http://localhost:8081
首次登录:
用户名:admin
密码:在 sonatype-work/nexus3/admin.password 文件中
二、基本配置
1. 仓库类型配置
Nexus 支持多种仓库类型:
proxy:代理远程仓库(如 Maven Central)
hosted:本地私有仓库(存储公司内部构件)
group:仓库组(聚合多个仓库)
2. 创建必要仓库
登录后进入"Repository" → “Repositories”
创建以下仓库:
maven-central (proxy) → 代理中央仓库
maven-releases (hosted) → 公司发布版本
maven-snapshots (hosted) → 公司快照版本
maven-public (group) → 聚合上述仓库
3. 配置仓库策略
Releases 仓库:Deployment policy 设置为 Disable redeploy
Snapshots 仓库:Deployment policy 设置为 Allow redeploy
该处使用的url网络请求的数据。
三、Maven 客户端配置
settings.xml 配置
<settings>
<servers>
<server>
<id>nexus-releases</id>
<username>deployment</username>
<password>yourpassword</password>
</server>
<server>
<id>nexus-snapshots</id>
<username>deployment</username>
<password>yourpassword</password>
</server>
</servers>
<mirrors>
<mirror>
<id>nexus</id>
<name>Nexus Public</name>
<url>http://your-nexus:8081/repository/maven-public/</url>
<mirrorOf>*</mirrorOf>
</mirror>
</mirrors>
</settings>
项目 pom.xml 配置
<distributionManagement>
<repository>
<id>nexus-releases</id>
<name>Releases</name>
<url>http://your-nexus:8081/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>nexus-snapshots</id>
<name>Snapshots</name>
<url>http://your-nexus:8081/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
其实,如果不是module有要deploy的,这个也不用配置。
四、团队成员使用
团队成员使用,那就要搞权限了,用户了。我们现在的私库是搭建在内网,然后使用lanproxy映射出来的,速度我绝对其实很快了,但是lanproxy真的只是过渡方案,后面要上vpn方案。
看看效果吧。
这就是我们的私有jar,后面各种组合设置,对兼职设置保密。
总结
- 私库搭建还是挺简单的。(这次其实不是自己亲自搭建的,运维我们有运维了)
- 私库利用的好,保密还是可以得到保障的
就写到这里,希望可以帮到大家!