nexus安装与使用

一、Nexus介绍

Nexus是Maven仓库管理器,也可以叫Maven的私服。Nexus是一个强大的Maven仓库管理器,它极大地简化了自己内部仓库的维护和外部仓库的访问。利用Nexus你可以只在一个地方就能够完全控制访问和部署在你所维护仓库中的每个Artifact。Nexus是一套“开箱即用”的系统不需要数据库,它使用文件系统加Lucene来组织数据。

Nexus不是Maven的核心概念,它仅仅是一种衍生出来的特殊的Maven仓库。对于Maven来说,仓库只有两种:本地仓库和远程仓库。

Nexus Repository 是以 Java 和 JavaScript 为主,实现的一个包含前端与后台的 Web 服务。 后台方面,它采用 Jetty 作为应用服务器、Karaf 作为 OSGi 容器、OrientDB 作为数据库。 前端方面,它使用Swagger UI 作为框架,是一个单页面 Web App。

另外,它也通过 Resteasy 支持 REST API,可以通过网络进行访问控制。 并且,自行实现了一个插件系统,用插件的方式支持了更多复杂的功能。 比如,Maven、PyPI、Docker 这些支持,都是由插件实现的。 如果希望支持其它方式的代理、缓存、发布,比如 APT,也可以通过插件定制。

二、Nexus下载

官网地址:https://www.sonatype.com/

下载地址:https://help.sonatype.com/repomanager3/download

2 . 1 . 下载Nexus

  Nexus 专业版是需要付费的,这里我们下载开源版 Nexus OSS。Nexus 提供两种安装包,一种是包含 Jetty 容器的 bundle 包,另一种是不包含容器的 war 包。下载地址:Nexus Repository OSS - Software Component Management | Sonatype

2 . 2 . 使用bundle安装包安装Nexus

解压安装包nexus-2.8.1-bundle.zip,打开命令提示符,进入/nexus-2.8.1-01目录,键入nexus命令(为方便启动和退出Nexus,可将bin目录添加到环境变量):

执行 nexus install 将Nexus安装为Windows服务。可将服务启动方式设为手动,以后通过 nexus start 即可启动Nexus ,通过 nexus stop 退出Nexus:

打开浏览器,访问: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>
  • 上传私服

在~/maven/conf/settings.xml中的servers标签中添加:

<!-- 配置正式仓库账号密码、测试仓库账号密码 -->
 <server>
      <id>releases</id>
      <username>admin</username>
      <password>admin123</password>
    </server>

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


需要上传的项目中的pom.xml文件:

<!-- 上传到私服 -->
<distributionManagement>
<!-- 私服正式库 id与setting文件server中id相同 -->
 <repository>
  <id>releases</id>
  <!-- http://localhost:8081/nexus/#view-repositories;public~configuration中你的正式库url -->
  <url>http://127.0.0.1:8081/nexus/content/repositories/releases/</url>
 </repository>
 <!-- 私服测试库 -->
 <snapshotRepository>
  <id>snapshots</id>
  <url>http://127.0.0.1:8081/nexus/content/repositories/snapshots/</url>
 </snapshotRepository>
</distributionManagement>

mvn deploy 命令进行上传私服

  • 下载操作
    ~/maven/conf/settings.xml中的profiles标签中添加:
<!-- 下载jar包配置 -->
<profile> 
 <!--profile的id -->
 <id>dev</id>
 <repositories>
  <repository> <!--仓库id,repositories可以配置多个仓库,保证id不重复 -->
   <id>nexus</id> <!--仓库地址,即nexus仓库组的地址 -->
   <url>http://localhost:8081/nexus/content/groups/public/</url> <!--是否下载releases构件 -->
   <releases>
    <enabled>true</enabled>
   </releases> <!--是否下载snapshots构件 -->
   <snapshots>
    <enabled>true</enabled>
   </snapshots>
  </repository>
 </repositories>
 <pluginRepositories> <!-- 插件仓库,maven的运行依赖插件,也需要从私服下载插件 -->
  <pluginRepository> <!-- 插件仓库的id不允许重复,如果重复后边配置会覆盖前边 -->
   <id>public</id>
   <name>Public Repositories</name>
   <url>http://localhost:8081/nexus/content/groups/public/</url>
  </pluginRepository>
 </pluginRepositories>
</profile>

激活在settings标签内:

<!-- dev与上面的下载的id对应上 -->  
<activeProfiles>
  <activeProfile>dev</activeProfile>
 </activeProfiles>

测试
通过上传后然后故意在本地仓库删掉对应依赖,从私服开始下载。

  •  安装第三方jar包到本地仓库和私服
    • 指令:以阿里巴巴的fastjson.jar为例,dos命令直接执行即可上传本地
----进入jar包所在目录运行
mvn install:install-file -DgroupId=com.alibaba -DartifactId=fastjson -Dversion=1.1.37 -Dfile=fastjson-1.1.37.jar -Dpackaging=jar
----打开cmd直接运行
mvn install:install-file -DgroupId=com.alibaba -DartifactId=fastjson -Dversion=1.1.37 -Dpackaging=jar -Dfile=jar包绝对路径

  • 私服上传第三方jar包
    • 指令:
--安装第三方jar包到私服
--在settings配置文件中添加登录私服第三方登录信息
<server>
<id>thirdparty</id>
<username>admin</username>
<password>admin123</password>
</server>
----进入jar包所在目录运行
mvn deploy:deploy-file -DgroupId=com.alibaba -DartifactId=fastjson -Dversion=1.1.37 -Dpackaging=jar -Dfile=fastjson-1.1.37.jar -Durl=http://localhost:8081/nexus/content/repositories/thirdparty/ -DrepositoryId=thirdparty
----打开cmd直接运行
mvn deploy:deploy-file -DgroupId=com.alibaba -DartifactId=fastjson -Dversion=1.1.37 -Dpackaging=jar -Dfile=jar包绝对路径 -Durl=http://localhost:8081/nexus/content/repositories/thirdparty/ -DrepositoryId=thirdparty

这里一定要注意DrepositoryId的值一定是和maven中配置的id对应上的

 

 大功告成!

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值