引言
最近突然想自己搭建个maven私服nexus,然后就动手搭了一遍。这个过程中自己对maven的一些配置有新的理解。学习嘛,有个输出会更加深印象吧,所以想把自己搭建nexus和一些新的理解记录下来,也可以供自己以后复习。
一 nexus搭建
参考文章:https://www.jianshu.com/p/1cfbc1518fce
1环境准备,这里不详细说了,会java的这些都是基础了。
centos7,jdk1.8,nexus-3.9.0-01-unix.tar.gz
Nexus官网下载压缩包巨慢,经常下载不了
这里提供一个网上的网盘下载地址:https://pan.baidu.com/s/1LjwzEax71H6emkUL3uZRcw
2 解压缩
tar -zxvf nexus-3.9.0-01-unix.tar.gz
3 进入bin目录

4 运行

注意:如果你的系统可用内存不够会报错,此时可用修改nexus启动内存
默认是1200M,我这里修改成了512M,但不能过小,过小的话启动时还是会内存不够
我最开始修改成128M,就启动不起来。

5 检查是否成功
访问Ip:8081,注意系统的8081端口开放。8081端口是默认端口,也可以修改

进行登录,有个默认用户:admin/admin123

二 nexus简单看看

Nexus默认就有这些maven仓库了
首先说些仓库类型
Proxy:代理库,这就是个远程参考,例如:maven-central这个仓库就是代理的maven的中央仓库。
Hosted:本地库,就是nexus机器上的本地仓库。我们自己的jar可以上传到这里。
Group:组,好几个仓库可以归为一组,能够包含 proxy,hosted仓库。

Jar下载流程: 先本地maven库,再私服group仓库,group仓库规则:先hosted本地库,再proxy远程库。
三 nexus仓库新增
我们可以新增一个proxy库,连个hosted库(一个release,一个snapshot),一个group仓库把这些新增的库都包含进去。
1 proxy库,我们新增一个aliyun的maven代理仓库
http://maven.aliyun.com/nexus/content/groups/public/



然后点击创建就可以了
2 创建hosted库,顺序一样,记得选择 maven2(hosted)


3 创建group仓库,选择maven2(group)

我们maven链接group仓库时,group仓库 会先从 hosted类型的仓库逐一查找,没找到 在从proxy类型的仓库中逐一查找。
四 使用私服
Settings.xml文件中配置 私服镜像

Url我们一般配置成 group仓库的地址, 在nexus上可以找到

五 项目直接deploy到私服上去
1 项目中的pom进行配置
<distributionManagement> <repository> <id>my-release</id> <name>Nexus Release Repository</name> <url>http://xx.xx.xx.xx:8081/repository/mys-release/</url> </repository> <snapshotRepository> <id>my-snapshot</id> <name>Nexus Snapshot Repository</name> <url>http://xx.xx.xx.xx:8081/repository/my-snapshot/</url> </snapshotRepository></distributionManagement>

2 hosted仓库的 deployment policy 要改成allow redeploy

3 settings.xml中配置用户名密码
注意:pom中配置的repository的id 和这里的id要对应一样
<servers>
<server>
<id>my-release</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>my-snapshot</id>
<username>admin</username>
<password>admin123</password>
</server>
</servers>
上面三步配置完成后,可以在idea中双击maven的生命周期中的deploy

成功后再nexus的 相关仓库中就可以看到相关的jar包了

六 deploy后 进行maven依赖
直接在pom中进行dependency依赖就可以了。把项目的坐标依赖过去

注意了,这时你可能还是依赖不成功。
为什么呢?因为默认不能从从snapshot仓库中下载依赖的。此时你要开启这个功能
可以在settings.xml中进行配置,项目中也可以,但xml中配置了 所有的项目都可以用了。

<profiles>
<profile>
<id>mycof</id>
<repositories>
<repository>
<id>nexus</id>
<url>http://ip:8081/repository/my-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<url>http://ip:8081/repository/my-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>mycof</activeProfile>
</activeProfiles>
此时就可以正常从snapshot库中下载依赖了
七 第三方jar包直接通过nexus页面上传


上传后去相应仓库中查看

这样就成功了
在项目中就可以正常依赖了
到此nexus的搭建和使用告一段落了,日常的使用就足够了。
接下来讨论下我在搭建使用过程中的一个疑问吧。
有没有发现settings.xml中 mirro和repository同时配置了
我当时就疑问了,这两者有啥区别呢?
参考文章:https://my.oschina.net/sunchp/blog/100634
1 mirro相当于是个拦截器
比如我们配置访问a仓库 ,但是经过它的拦截 会访问到它指向的仓库
2 repository就是配置我们maven要访问的仓库。
如果没有配置默认是中央仓库
所以如果我们在repository中直接配置我们的nexus仓库,那mirro就可以不用配置了。
今天的学习就到这了,下次见!
网友评论