您的位置:首页 > 其它

Maven 仓库 Sonatype Nexus 的私服说明

2017-02-15 16:55 176 查看
本文转载总结至 http://linjie.org/2016/04/23/Sonatype-nexus-3-build-run/             linjie

 http://www.cnblogs.com/luotaoyeah/p/3791966.html  luotao

私服是架设在局域网的一种特殊的远程仓库,目的是代理远程仓库及部署第三方构件。有了私服之后,当 Maven 需要下载构件时,直接请求私服,私服上存在则下载到本地仓库;否则,私服请求外部的远程仓库,将构件下载到私服,再提供给本地仓库下载。


                                                


  我们可以使用专门的 Maven 仓库管理软件来搭建私服,比如:Apache ArchivaArtifactory,Sonatype
Nexus。这里我们使用 Sonatype Nexus。例如阿里的私服使用的就是Nexus http://maven.aliyun.com/nexus/

不过一般个人开发也不用配置什么,直接使用Maven 中央库即可,就算有什么私人或没传到中央库的jar文件,也可以使用各种方式加入到项目中来,所以不太必要有什么自己的私有库。

不过团队开发中,有些企业内部有自己不宜公开的开发框架或基础框架等,如果上面个人的解决方案,可能是生成一个Jar文件扔到项目里面,不过如果涉及到基础框架更新或维护,这可能就不太理想了,所以有一套团队或企业内部的私有库,就显的很重要了。

好了,完毕!不罗嗦了。

PS2:英文文档很全,如果没什么压力的话,可以直接看官方文档。 :)


地址:https://books.sonatype.com/nexus-book/3.0/reference/index.html


前提

当前 sonatype neuxs 基于Java,所以需要在环境里面安装jdk,当前这个版本需要jdk1.8 ,所以请先配好java环境。

因为我本机已经安装了Java,且java安装教程很多,请参考网络上的。


下载

开始下载,地址:http://www.sonatype.com/download-oss-sonatype



图片

当前最新版本:nexus-3.0.0-03 (这个版本支持npm,Nuget等)

平台:根据环境选择(我这边选Unix)

OK ,点击下载就好了。


运行

将下载好的文件直接解压,见如图:




OK,解压好了,进入到bin里面,我是放到了/opt 目录下面,所以直接:

1
2

cd /opt/nexus-3.0.0-03/bin/
./nexus

它提示:

1

Usage: ./nexus {start|stop|run|run-redirect|status|restart|force-reload}

如果是以root身份运行,则会看到:

1
23
4
5

[root@localhost bin]# ./nexus
WARNING: ************************************************************
WARNING: Detected execution as "root" user. This is NOT recommended!
WARNING: ************************************************************
Usage: ./nexus {start|stop|run|run-redirect|status|restart|force-reload}

好了,根据提示,现在开始运行:

1
2

[linjie@localhost bin]$ ./nexus start
Starting nexus

OK,如果片没什么问题,就应该启动起来了,访问:
http://localhost:8081/

默认的端口是8081。

此时应该可以看到:




点击 右边上角的 Sign In,默认账号密码:
admin
,
admin123
.

恭喜,已经安装好了,:) , 不用设置什么数据库密码,之类的.

登陆之后,看看仓库:




Name 就不说了,其他的:

Type
proxy 是代理仓库 ,如果自己私有库没有对应的资源(jar等),会到这里去找。
hosted 是宿主仓库 ,是自己的私有库地址,这个就是自己的。这个有 releases 和snapshots 两种类型,你如果自己创建的时候,需要指定 ,一个是正式发布地址,一个是开发中地址。
group 管理组 ,组是Nexus一个强大的特性,它允许你在一个单独的URL中组合多个仓库。比如这里默认组合了:
maven-central
maven-releases
maven-snapshots
 ,一般直接引用这个地址就好了。

Status

这里本来也没什么好说的,不过却遇到一个跟 neuxs 2.x不同的坑。如果用过2.x可能就入坑,比如我。

这里目有两个状态:
Online
 和 
Online
- Remote Connection Pending...


首先说下,这是正常的,没坑,如果没和2.x版本对比过,就是这样的。

为什么说坑,因为当我成功安装了之后,马上迫不及待的就开始搜索,比如:springframework ,这是搜索出来一片空白,内心一慌,我靠!怎么回事,如果找到这里,点了进去,如图:

狂按 Rebuild Index, 提示我成功之后,立即搜索,还是一片空白!各种方式,依旧无用!:(

之后我google 这里:
http://stackoverflow.com/questions/34782859/sonatype-nexus-3-remote-connection-pending

The “connection pending” message is normal in 3.0m6. It just means nothing has been downloaded through the proxy repository yet. Try running a build that retrieves artifacts through the proxy, the status will change once the first file of the artifact is downloaded.

就放下了。


运行其他

左边的控制栏,分为三类,仓库,安全(用户),支持,系统,慢慢体验。


项目使用


先试一试能不能代理中央库,实现下载

pom.xml 文件:

1
23
4
5
6
7
8
9
10
1112
13
14
15
16
17
18
19
20
2122
23
24
25
26

<repositories>
<repository>
<id>harme-maven-public</id>
<name>maven-public</name>
<url>http://localhost:8081/repository/maven-public/</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>harme-maven-public</id>
<name>maven-public</name>
<url>http://localhost:8081/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>


再试一试能不能部属构件到nexus

setting.xml 文件,在servers节点里面:

1
23
4
5

<server>
<id>oss</id>
<username>admin</username>
<password>admin123</password>
</server>

pom.xml 文件再加:

1
23
4
5
6
7
8
9
10

<distributionManagement>
<repository>
<id>oss</id>
<url>http://localhost:8081/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>oss</id>
<url>http://localhost:8081/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>

好了。先这样。

PS3: 如果想要以 service 这种服务运行的话,也很简单,看文档:
https://books.sonatype.com/nexus-book/3.0/reference/install.html#service-linux

打开浏览器,访问:http://localhost:8081/nexus/:



点击右上角 Log In,使用用户名:admin ,密码:admin123 登录,可使用更多功能:



3 . Nexus预置的仓库

点击左侧 Repositories 链接,查看 Nexus 内置的仓库:



 

Nexus 的仓库分为这么几类:
hosted 宿主仓库:主要用于部署无法从公共仓库获取的构件(如 oracle 的 JDBC 驱动)以及自己或第三方的项目构件;
proxy 代理仓库:代理公共的远程仓库;
virtual 虚拟仓库:用于适配 Maven 1;
group 仓库组:Nexus 通过仓库组的概念统一管理多个仓库,这样我们在项目中直接请求仓库组即可请求到仓库组管理的多个仓库。



4 . 添加代理仓库

以 Sonatype 为例,添加一个代理仓库,用于代理 Sonatype 的公共远程仓库。点击菜单 Add - Proxy Repository :



填写Repository ID - sonatype;Repository Name - Sonatype Repository;

Remote Storage Location - http://repository.sonatype.org/content/groups/public/ ,save 保存:



将添加的 Sonatype 代理仓库加入 Public Repositories 仓库组。选中 Public Repositories,在 Configuration 选项卡中,将 Sonatype Repository 从右侧 Available Repositories 移到左侧 Ordered Group Repositories,save 保存:



5 . 搜索构件

为了更好的使用 Nexus 的搜索,我们可以设置所有 proxy 仓库的 Download Remote Indexes 为 true,即允许下载远程仓库索引。



索引下载成功之后,在 Browse Index 选项卡下,可以浏览到所有已被索引的构件信息,包括坐标、格式、Maven 依赖的 xml 代码:



有了索引,我们就可以搜索了:



6 . 配置Maven使用私服

私服搭建成功,我们就可以配置 Maven 使用私服,以后下载构件、部署构件,都通过私服来管理。

在 settings.xml 文件中,为所有仓库配置一个镜像仓库,镜像仓库的地址即私服的地址(这儿我们使用私服公共仓库组 Public Repositories 的地址):



<mirrors>
<mirror>
<id>central</id>
<mirrorOf>*</mirrorOf> <!-- * 表示让所有仓库使用该镜像-->
<name>central-mirror</name>
<url>http://localhost:8081/nexus/content/groups/public/</url>
</mirror>
</mirrors>


 

 

更多高级特性不再介绍(我也还不会)。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: