一、了解Nexus
- 搭建Maven私服nexus指南,好久没有发布博客了,最近一直忙于项目没有更新CSDN,但是并不代表我没有学习,不热爱编程,哈哈哈,有点扯淡。最近在项目中,经常要用到搭建nexus私服仓库的需求,所以这篇博客,在此介绍一下详细步骤,共同学习一下如何搭建nexus仓库,网上有很多资料了,也很详细,我就项目实际操作简单总结一下吧。
- 首先,在搭建私服之前,我们应该了解私服的工作原理是什么?它是用来干什么的?为什么要用私服?那肯定是方便依赖关系和jar包管理啦,是的,项目中的私服就像我们自己的车库,车库中有N多辆兰博基尼、法拉利和劳斯莱斯,车多,要有地方放置和统一管理,那么私服在项目中就是充当这个角色的,用来管理项目依赖和jar包,通常只服务于一个项目,不像Maven的中央仓库包含很多jar,我们需要用的时候设置maven的中央仓库地址,然后maven自动从仓库中下载依赖包到我们的项目中完成编译。
- nexus原理
从网上偷了一张图,介绍下 私服的工作原理,从上图我们可以看出,我们在本地配置私服之后,私服的配置其实是通过一个maven-public的仓库组进行找包的,
找依赖包的流程: 首先在本地仓库中找,如果没命中,那么就找远程私服;远程私服的查找规则同样是先找host属性的私有库,然后再去找proxy属性的远程仓库;可以配置多个proxy;
- Nexus仓库类型介绍
- hosted(宿主仓库库):存放本公司开发的jar包(正式版本、测试版本)
- proxy(代理仓库):代理中央仓库、Apache下测试版本的jar包
- group(组仓库):使用时连接组仓库,包含Hosted(宿主仓库)和Proxy(代理仓库)
二、搭建nexus
- 废话不多说,我们搭建一个私服玩一把
- 准备:Centos 7、xshell、docker(提前下载好,不会可以百度)
1.使用docker 下载 nexus镜像
- 执行如下命令,下载最新版本
docker search nexus
2.启动nexus之前,首先创建一个日志目录用来映射容器中的nexus日志便于查看
mkdir -p /usr/local/nexus3/nexus-data //创建一个日志目录
chown -R root/usr/local/nexus3/nexus-data //设置权限,给与root权限
docker run -d -v /usr/local/nexus3/nexus-data/:/nexus-data -d NEXUS_CONTEXT=nexus -p 8081:8081 --name nexus docker.io/sonatype/nexus3
查看容器是否启动:docker ps
3.登录nexus管理控制台进行访问,用户名:admin,密码为123456,如果密码不正确请去/nexus-data/admin.password中查看即可
查看密码:
登录之后,首先创建仓库,创建代理,操作如下:
常用的私服代理:
1. aliyun
http://maven.aliyun.com/nexus/content/groups/public
2. apache_snapshot
https://repository.apache.org/content/repositories/snapshots/
3. apache_release
https://repository.apache.org/content/repositories/releases/
4. atlassian
https://maven.atlassian.com/content/repositories/atlassian-public/
5. central.maven.org
http://central.maven.org/maven2/
6. datanucleus
http://www.datanucleus.org/downloads/maven2
7. maven-central (安装后自带,仅需设置Cache有效期即可)
https://repo1.maven.org/maven2/
8. nexus.axiomalaska.com
http://nexus.axiomalaska.com/nexus/content/repositories/public
9. oss.sonatype.org
https://oss.sonatype.org/content/repositories/snapshots
10.pentaho
https://public.nexus.pentaho.org/content/groups/omni/
按照以上创建方式,依次创建maevn proxy,URL如上,设置时间为28800,名字自定义,
4.设置仓库组,设置找包顺序,原则是最好将默认的maven库放到最底下,如下图
5.设置私用仓库可重复发布
6.maven settings.xml配置maven私服,xml如下
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository>E:\Maven\LocalRepository</localRepository>
<pluginGroups></pluginGroups>
<proxies></proxies>
<servers>
<server>
<id>releases</id>
<username>admin</username>
<password>123456</password>
</server>
<server>
<id>snapshots</id>
<username>admin</username>
<password>123456</password>
</server>
</servers>
<mirrors>
<mirror>
<id>maven-public</id>
<mirrorOf>*</mirrorOf>
<name>Yonyou Public Mirror.</name>
<url>http://ip:6868/repository/maven-public/</url>
</mirror>
</mirrors>
<profiles>
<profile>
<id>maven-public</id>
<repositories>
<repository>
<id>maven-public</id>
<name>Public Repositories</name>
<url>http://ip:6868/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
</repository>
<repository>
<id>maven-central</id>
<name>Central Repositories</name>
<url>http://ip:6868/repository/maven-central/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>maven-releases</id>
<name>Release Repositories</name>
<url>http://ip:6868/repository/maven-releases/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>maven-snapshots</id>
<name>Snapshot Repositories</name>
<url>http://ip:6868/repository/maven-snapshots/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>plugins</id>
<name>Plugin Repositories</name>
<url>http://ip:6868/repository/maven-public/</url>
</pluginRepository>
</pluginRepositories>
</profile>
<!-- jdk 1.8配置 -->
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
</profiles>
</settings>
7.创建Maven项目,如下:
8.设置pom.xml,设置私服的发行版本库和快照版本库,在xml中添加如下配置
<distributionManagement>
<repository>
<id>releases</id>
<name>Nexus Release Repository</name>
<url>http://ip:端口/nexus/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<name>Nexus Snapshot Repository</name>
<url>http://ip:端口/nexus/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
9.mvn clean 一下,然后mvn install 一下,观察是否又报错,如果没有 mvn deploy,登录nexus查看对应的仓库已经有相关的依赖包。
注意:如果部署过程中报错:
Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy
(default-deploy) on project common-nexus: Failed to deploy
artifacts: Could not transfer artifact
com.nexus:common-nexus:jar:1.0-20200921.073534-1
from/to maven-snapshots (http://ip:端口/nexus/repository/maven-snapshots/):
Failed to transfer file http://ip:端口/nexus/repository/maven-snapshots/com/nexus/common-nexus/1.0-SNAPSHOT/common-nexus-1.0-20200921.073534-1.jar
with status code 405
这种错误说明,此时maven不知道将依赖部署到那个目标仓库中,请检查pom.xml中的配置的仓库ip和settings.xml中的server节点中的id是否以之,如果不一致,请修改为一致即可,另外检查一下项目使用的是否是您修改配置的settings.xml,如果不是,请配置成修改后的settings.xml,重新部署即可。
OK,以上仅仅是我个人的理解和操作过程,有什么不对的或者误导大家的欢迎留言评论,我们共同提高共同进步,谢谢啦