Nexus Repository 搭建及使用
Nexus Repository 是搭建maven的镜像的工具之一,在全球范围内使用挺广的。
一、Nexus 搭建过程
Nexus 镜像的搭建还是相对简单的,将下载的文件解压到相应的目录下,然后进入./nexus-version/bin/下,启动nexus即可将Nexus Repository运行起来(其中Nexus是基于Jetty来运行的)。
启动起来后,通过浏览器访问http://ip:8081就可以访问了。界面如下所示:
其中通过右侧的Sign In可以登录用户来管理Nexus。对于未登录的用户,只能查询和浏览相关资源。用户登录后就能看到管理相关的菜单了,在管理菜单中主要是配置Blob Stores和Repositories,其中Blob Stores 是用来配置资源的保存位置的,可以将不同的远程资源保存到服务器的不同位置上(类似于Nexus 2的远程资源保存路径配置,在Nexus 3中进行了分组管理),Repositories 则用来配置远程资源和本地资源,其中支持Proxy类型资源,表示远程资源的镜像,host类型的资源表示本地资源,用于保存第三方资源或团队内部共享的资源等。group类型资源是组,可以将多个proxy或host类型的资源构建成一个组,供Maven使用等,如下图所示:
注意事项:
1. Nexus 2 能够支持资源信息的拷贝,提供xml格式,而Nexus 3 暂时还不支持该xml格式拷贝。
2. Nexus 2 能够下载远程的索引到本地,供搜索查询使用,但是Nexus 3 暂时还不支持,在Nexus 3 中查询的都是已经有请求该镜像库的资源,对于未从该镜像库下载过的资源是无法查询到的。那么在Nexus 3 搭建初期,没有使用前,在该系统是查询不到任何资源的,需要先使用程序去调用该镜像,才会有资源使用。
3. Nexus 3 暂时不支持下载远程镜像的索引。只有当Maven中设置了Nexus 3 的资源库,Nexus 3 会在具体编译项目的时候先从远程下载对应的资源到本地,然后传输给各个客户端使用。 这样也就不用跟Nexus 2 中那样再【Repair Index】来更新索引了。
二、Maven 配置Nexus 镜像
Nexus 镜像搭建起来后,那么如何在Maven中使用该镜像资源呢? 主要是通过调整maven配置来识别该镜像资源,从而将maven资源请求转发到该镜像资源库上。通常的做法为在当前用户的目录下.m2文件夹中创建settings.xml文件,其中指定mirror,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<!-- for full reference, see also http://maven.apache.org/ref/3.2.3/maven-settings/settings.html -->
<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"> <!-- 使用Mirror配置节可以强制所有包请求都会被转向内网Nexus服务器的地址 -->
<mirrors>
<mirror>
<id>mirror_name</id>
<mirrorOf>*</mirrorOf>
<url>http://nexus_ip:8081/repository/maven-public/</url>
</mirror>
</mirrors>
</settings>
上述配置信息是所有的远程资源镜像的访问都会通过该镜像来访问,这也是大部分公司采用的方案,因为这样能够保证公司对远程资源的访问次数是有限的,公司内部的开发人员访问的都是公司内部的资源。开发人员不需要链接外网去下载远程镜像服务器上的资源,只需要该公司内部镜像去访问一次即可。当然了,mirrorOf可以指定为代理部分远程资源,详情见官方说明:http://maven.apache.org/guides/mini/guide-mirror-settings.html
三、Nexus 启用上传功能
团队有了私有的镜像服务器以后,团队内部代码的依赖也就不再需要源码依赖了,大家可以通过发布不同版本的jar到nexus镜像上来供调用者直接通过Maven下载使用,这样不同研发人员直接的依赖也就没有那么强了,大家可以基于已经发布的版本进行各自的开发。
那么如何发布个人的jar资源到团队内部镜像上呢?
1. 在Nexus 中创建一个developer的角色,拥有的权利为【nx-repository-view-maven2-*-edit】和【nx-repository-view-maven2-*-add】权利,如果该角色将来可能还有nuget,npm相关上传权利,则将其权利改为【nx-repository-view-*-*-edit】和【nx-repository-view-*-*-add】权利。
2. 创建用户,用户拥有的角色为【nx-anonymous】和刚创建的【developer】角色。其中nx-anonymous角色是nexus默认自带的角色。
3. 在.m2文件夹下的settings.xml配置文件中增加<servers>的配置。
<?xml version="1.0" encoding="UTF-8"?>
<!-- for full reference, see also http://maven.apache.org/ref/3.2.3/maven-settings/settings.html -->
<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"> <!-- 配置Maven服务器的账号信息,自动化部署的时候需要用到 -->
<servers>
<server>
<id>server_id</id>
<!-- 使用公共的developer/password账号进行日常的发布管理 -->
<username>developer</username>
<password>password</password>
</server>
</servers> <!-- 使用Mirror配置节可以强制所有包请求都会被转向内网Nexus服务器的地址 -->
<mirrors>
<mirror>
<id>mirror_name</id>
<mirrorOf>*</mirrorOf>
<url>http://nexus_ip:8081/repository/maven-public/</url>
</mirror>
</mirrors>
</settings>
4. 在需要上传jar资源的项目的pom.xml中增加<distributionManagement>配置,其中<id>设置需要跟.m2文件夹下的settings.xml中<servers>下的id相同。<url>需要指定nexus中配置的hosts Repository资源的地址。
<distributionManagement>
<repository>
<id>server_id</id>
<name>Nexus Release Repository</name>
<url>http://nexus_ip:8081/repository/host-releases/</url>
</repository>
</distributionManagement>
5. 通过maven 编译项目,并通过mvn deploy 来发布jar资源到团队内部的镜像服务器即可。
四、Nexus 启用SNAPSHOTS
团队内部在开发过程中为了相互可以互不影响的开发,需要时常将未稳定版的jar发布出来供团队其他人员调用,这时候建议使用SNAPSHOT版本,那么SNAPSHOT版本怎么发布到Nexus上呢。如果以当前的配置,发布SNAPSHOT过程会失败,因为Nexus默认是不启用SNAPSHOT的。那么怎么启用SNAPSHOT及如何上传SNAPSHOT版本资源呢?
启用SNAPSHOT的方式为在.m2文件夹下的settings.xml中增加<profile>设置
<!-- 这个默认配置决定了我们的Maven服务器开启snapshot配置,否则不能下载SNAPSHOTS的相关资源 -->
<profiles>
<profile>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<repositories>
<repository>
<id>repository_name</id>
<name>Nexus Public Repository</name>
<url>http://nexus_ip:8081/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
</repositories>
</profile>
</profiles>
增加这个配置后,那么是否就可以直接上传SNAPSHOT版本的jar资源了呢?经过测试还是不行,还需要有其他配置。
1. Nexus Repository中增加一个hosted类型的Repository,Maven的资源类型为SNAPSHOT(Nexus默认已经存在一个这种类型的资源)。
2. settings.xml的<servers>增加snapshot server的配置,settings.xml的最终配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<!-- for full reference, see also http://maven.apache.org/ref/3.2.3/maven-settings/settings.html -->
<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"> <!-- 配置Maven服务器的账号信息,自动化部署的时候需要用到 -->
<servers>
<server>
<id>nexus-releases</id>
<!-- 使用公共的developer/password账号进行日常的发布管理 -->
<username>developer</username>
<password>password</password>
</server>
<server>
<id>nexus-snapshots</id>
<!-- 使用公共的developer/password账号进行日常的发布管理 -->
<username>developer</username>
<password>password</password>
</server>
</servers> <!-- 使用Mirror配置节可以强制所有包请求都会被转向内网Nexus服务器的地址 -->
<mirrors>
<mirror>
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>http://nexus_ip:8081/repository/maven-public/</url>
</mirror>
</mirrors> <!-- 这个默认配置决定了我们的Maven服务器开启snapshot配置,否则不能下载SNAPSHOTS的相关资源 -->
<profiles>
<profile>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<repositories>
<repository>
<id>repository_name</id>
<name>Nexus Public Repository</name>
<url>http://nexus_ip:8081/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
</repositories>
</profile>
</profiles>
</settings>
3. 需要发布jar资源到团队内部镜像服务器的项目的pom.xml配置<distributionManagement>增加snapshot的支持,最终的pom.xml增加的<distributionManagement>如下:
<distributionManagement>
<repository>
<id>nexus-releases</id>
<name>Nexus Release Repository</name>
<url>http://nexus_ip:8081/repository/yoyi-releases/</url>
</repository>
<snapshotRepository>
<id>nexus-snapshots</id>
<name>Nexus Snapshot Repository</name>
<url>http://nexus_ip:8081/repository/yoyi-snapshots/</url>
</snapshotRepository>
</distributionManagement>
通过上述各种配置,即可以发布jar到团队内部的镜像服务器了。 其中distributionManagement需要配置在各个项目的pom.xml文件中,如果多个项目都需要使用,是否可以将<distributionManagement>放到.m2文件夹下的settings.xml中呢?经过测试,放到settings.xml中是不起作用的。那么如果在多个项目中使用的方案为多个项目创建一个父项目,将各个项目作为模块加载到父项目中,只在父项目中配置distributionManagement即可。如果各位看客有更好的方案,还望能在留言中告知下。^-^
五、异常情况处理
*UNKNOWN com.sonatype.nexus.plugins.outreach.internal.outreach.SonatypeOutreach - Could not download page bundle
org.apache.http.conn.HttpHostConnectException: Connect to sonatype-download.global.ssl.fastly.net: [sonatype-download.global.ssl.fastly.net/69.171.245.49] failed: 连接超时
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:) [httpcore:0.0.]
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:) [httpcore:0.0.]
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:) [httpcore:0.0.]
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:) [httpcore:0.0.]
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:) [httpcore:0.0.]
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:) [httpcore:0.0.]
处理方法:登录账号,打开【System】--》【Capabilities】,将【Outreach:Management】禁用即可。
Nexus Repository 搭建及使用的更多相关文章
- linux下安装nexus repository及Intellij Idea集成私有maven
前段日子公司搞java项目,使用nexus repository搭建了私有maven库,现在把原来的私有nuget也迁到nexus repository上了,下面介绍下搭建流程: https://he ...
- [转] 使用HTTPS在Nexus Repository Manager 3.0上搭建私有Docker仓库
FROM: https://www.hifreud.com/2018/06/06/03-nexus-docker-repository-with-ssl/ 搭建方式 搭建SSL的Nexus官方提供两种 ...
- 搭建Nexus Repository包管理系统
搭建Nexus Repository包管理系统 下载安装程序 下载Nexus Repository最新版本 配置说明 将下载后的文件传输到服务器上 #修改配置文件 vi /etc/security/l ...
- 使用 Nexus Repository Manager 搭建 npm 私服
目录 环境 下载与安装 添加npm仓库 配置与验证npm仓库 发布自己的包 Nexus开启启动 脚注 环境 windows10(1803) Nexus Repository Manager OSS 3 ...
- Maven私服搭建(Nexus Repository Manager 3)
下载和安装 下载地址:https://help.sonatype.com/repomanager3/download 注意:Nexus Repository Manager 3是一个Java服务器应用 ...
- 本地 Maven项目部署到Nexus Repository
配置Nexus Repository 打开WEB管理界面:http://localhost:8081/nexus/index.html 点击右上角Log In进行登录,默认帐号:admin.密码:ad ...
- Window下Nexus私服搭建
项目组大部分人员不能访问maven的central repository,因此在局域网里找一台有外网权限的机器,搭建nexus私服,然后开发人员连到这台私服上 环境是:nexus-2.1.1.mav ...
- Nexus 3 搭建 npm 私服 (windows)
Nexus 3 搭建 npm 私服备忘 下载与安装 在官网下载Nexus Repository Manager OSS 3.x, 解压至任意位置. 管理员运行 powershell, 切换到 nexu ...
- Nexus Repository Manager 3(CVE-2019-7238) 远程代码执行漏洞分析和复现
0x00 漏洞背景 Nexus Repository Manager 3是一款软件仓库,可以用来存储和分发Maven,NuGET等软件源仓库.其3.14.0及之前版本中,存在一处基于OrientDB自 ...
随机推荐
- javaScript设计模式(一)观察者模式
哈哈..写了一个钟,一点一点加功能. 1 function Publisher(){ this.subscribers = []; //存储订阅者 this.news = []; //存储要发布的消息 ...
- [py]python的继承体系-源码目录结构
python3安装目录 pip install virtualenv pip install virtualenvwrapper pip install virtualenvwrapper-win m ...
- oj2892(字典树)
一改时间以后WA了,我就知道这题是考字典树,可惜代码怎么也不会敲了,郁闷. #include <stdio.h>#include <string.h>#include < ...
- PAT 1103 Integer Factorization[难]
1103 Integer Factorization(30 分) The K−P factorization of a positive integer N is to write N as the ...
- weka数据挖掘拾遗(一)---- 生成Arff格式文件
一.什么是arff格式文件 1.arff是Attribute-Relation File Format缩写,从英文字面也能大概看出什么意思.它是weka数据挖掘开源程序使用的一种文件模式.由于weka ...
- keras之save & reload model
import numpy as np np.random.seed(1337) # for reproducibility from keras.models import Sequential fr ...
- iOS 新浪微博-1.0框架搭建
项目搭建 1.新建一个微博的项目,去掉屏幕旋转 2.设置屏幕方向-->只有竖向 3.使用代码构建UI,不使用storyboard 4.配置图标AppIcon和LaunchImage 将微博资料的 ...
- Nhibernate入门与demo
学习和使用Nhibernate已经很久了,一直想写点东西和大家一起学习使用Nhibernate.博客园里也有很多大牛写了很多关于Nhibernate入门的文章.其中:李永京的博客http://www. ...
- javascript 判断数据类型
Object.prototype.toString.call(asddfff) //报错asddfff没被定义Object.prototype.toString.call(undefined) //& ...
- cocoapods 配置
二.CocoaPods 安装 CocoaPods可以方便地通过Mac自带的RubyGems安装. 打开Terminal(Mac电脑自带的终端): (1).设置ruby的软件源 这是因为ruby的软件源 ...