场景

Ubuntu Server 上使用Docker Compose 部署Nexus(图文教程):

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/101111611

在上面已经实现部署Nexus后的效果是

Nexus-在项目中使用Maven私服,Deploy到私服、上传第三方jar包、在项目中使用私服jar包-LMLPHP

为什么要搭建私服

有时合作开发时,为了不泄露源码但是还能允许你调用,或者公司内部自己的依赖jar包,只能在本公司内用,并且再官方中央仓库中没有。类似情况下都需要搭建Maven私服。

注:

博客:
https://blog.csdn.net/badao_liumang_qizhi
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。

实现

Deploy依赖到私服

配置认证信息

找到Maven的安装目录

Nexus-在项目中使用Maven私服,Deploy到私服、上传第三方jar包、在项目中使用私服jar包-LMLPHP

conf下的setting.xml中找到server节点。

Nexus-在项目中使用Maven私服,Deploy到私服、上传第三方jar包、在项目中使用私服jar包-LMLPHP

配置认证节点,因为私服不是谁都能使用,所以需要配置用户名和密码,这里的密码是上面搭建Nexus服务时所设置的密码。

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

修改之后,保存。

Nexus-在项目中使用Maven私服,Deploy到私服、上传第三方jar包、在项目中使用私服jar包-LMLPHP

注:

nexues-releases:用于发布Release版本

nexus-snapshots:用于发布Snapshot版本(快照版),快照版会自动加一个时间作为标识。

配置自动化部署

在项目的pom.xml中加入如下代码:

<distributionManagement>
<repository>
<id>nexus-releases</id>
<name>Nexus Release Repository</name>
<url>http://192.168.208.134:8081/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>nexus-snapshots</id>
<name>Nexus Snapshot Repository</name>
<url>http://192.168.208.134:8081/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>

这里是使用IDEA新建的maven项目

注:

1.ID名称要与settings.xml中Servers配置的ID保持一致。

2.项目版本号中有SNAPSHOT标识的,会发布到Nexus Snapshots
Respository,否则发布到Nexus Release Repository,并根据ID去匹配授权账号。

3.这里的url是Nexus服务上的url。

Nexus-在项目中使用Maven私服,Deploy到私服、上传第三方jar包、在项目中使用私服jar包-LMLPHP

部署

打开IDEA下的Ternial,输入:

mvn deploy

Nexus-在项目中使用Maven私服,Deploy到私服、上传第三方jar包、在项目中使用私服jar包-LMLPHP

可以看到其部署效果

Nexus-在项目中使用Maven私服,Deploy到私服、上传第三方jar包、在项目中使用私服jar包-LMLPHP

此时刷新Nexus服务的url,找到Browse下的maven-snapshots

Nexus-在项目中使用Maven私服,Deploy到私服、上传第三方jar包、在项目中使用私服jar包-LMLPHP

部署成功。

然后打开IDEA--settings-maven,然后勾选上总是更新快照。

Nexus-在项目中使用Maven私服,Deploy到私服、上传第三方jar包、在项目中使用私服jar包-LMLPHP

这样就能用到最新的快照版本。

上传第三方jar包

有时在官方仓库没有的jar包,需要上传到私服上,供大家使用。

mvn deploy:deploy-file -DgroupId=com.google.code.kaptcha -DartifactId=kaptcha -Dversion=2.3. -Dpackaging=jar -Dfile=C:\Users\Administrator\Desktop\kaptcha-2.3..jar -Durl=http://192.168.208.134:8081/repository/maven-releases/ -DrepositoryId=nexus-releases

命令解释:

-DgroupId=                          自定义
-DartifactId=                        自定义
-Dversion=                          自定义 
三个自定义,构成pom.xml文件中的坐标
-Dpackaging=jar                       上传的类型是jar类型
-Dfile=                                  jar的本地磁盘位置
-Durl=                                             
                             hosted资源库的地址
-DrepositoryId=nexus-releases               
setting.xml文件中配置的ID

上传成功效果

Nexus-在项目中使用Maven私服,Deploy到私服、上传第三方jar包、在项目中使用私服jar包-LMLPHP

此时再回到浏览器,刷新。

Nexus-在项目中使用Maven私服,Deploy到私服、上传第三方jar包、在项目中使用私服jar包-LMLPHP

在项目中使用私服jar包

配置代理仓库

在需要从私服中下载jar包的项目的pom.xml中加入如下配置:

<repositories>
<repository>
<id>nexus</id>
<name>Nexus Repository</name>
<url>http://192.168.208.134:8081/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<!-- 私服仓库配置:从私服下载-->
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<name>Nexus Plugin Repository</name>
<url>http://192.168.208.134:8081/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>

为什么是从public进行下载,

因为公共仓库是发行仓库和快照仓库的映射,把两个仓库结合起来。

下面这段代码

<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>

作用是配置是否依赖发行版和是否依赖快照版。

怎样使用私服jar包。

找到要下载的jar包的坐标配置,加入到pom中,那么就会先从私服去找对应的jar包,然后再去官服去找jar包。

Nexus-在项目中使用Maven私服,Deploy到私服、上传第三方jar包、在项目中使用私服jar包-LMLPHP

04-27 23:29