Maven私服Nexus的搭建

来源:互联网 发布:js去除字符串中的符号 编辑:程序博客网 时间:2024/06/07 04:08

本文主要介绍Maven私服Nexus的搭建,搭建的初衷是因为某个开发室不能保证连接外网(万维网),所以打算搭建一个Maven私服,提前将需要的jar包部署到私服中。

软件版本


  • 操作系统:centOS 3.6
  • JDK: JDK 1.8
  • Nexus OSS: Nexus 2.8.1

3.x版本不带有离线搜索dependency功能,所以选择2.8.1

私服简介

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

这里写图片描述

安装JDK

1.解压jdk

[root@localhost ~]# tar -zxvf jdk1.8.0_151.tar.gz -C /home/manver/nexus/ 

2.配置环境变量
- 用vim打开/etc/profile,在末尾追加

export JAVA_HOME=/home/manver/nexus/jdk1.8.0_151export PATH=$JAVA_HOME/bin:$PATHexport CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

-编译profile,使追加的环境变量生效

source /etc/profile

安装nexus

[root@localhost~]# tar -zxvf nexus-2.8.1-bundle-unix.tar.gz -C /home/manver/nexus/ [root@localhost~]# cd /home/manver/nexus/bin[root@localhostbin]# nexus start****************************************WARNING - NOT RECOMMENDED TO RUN AS ROOT****************************************Starting Nexus OSS...Started Nexus OSS.

访问验证

打开浏览器,访问:http://ip:port/nexus/

这里写图片描述

Nexus预置的仓库

这里写图片描述

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

这里写图片描述

添加代理仓库

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

这里写图片描述

这里写图片描述


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

这里写图片描述

搜索构件

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

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

有了索引,我们就可以搜索了:
这里写图片描述

配置客户端maven使用私服

  私服搭建成功,我们就可以配置 Maven 使用私服,以后下载构件、部署构件,都通过私服来管理。
  在 settings.xml 文件中,为所有仓库配置一个镜像仓库,镜像仓库的地址即私服的地址(这儿我们使用私服公共仓库组 Public Repositories 的地址):
  

 <profiles>        <profile>           <id>dev</id>           <repositories>                <repository>                    <id>nexus</id>                                    <url>http://192.168.21.128:8081/nexus/content/groups/public/</url>                    <releases>                        <enabled>true</enabled>                    </releases>                    <snapshots>                        <enabled>true</enabled>                    </snapshots>                 </repository>            </repositories>                       <pluginRepositories>                  <pluginRepository>                      <id>nexus</id>                      <url>http://192.168.21.128:8081/nexus/content/groups/public/</url>                      <releases>                          <enabled>true</enabled>                      </releases>                      <snapshots>                          <enabled>true</enabled>                       </snapshots>                   </pluginRepository>             </pluginRepositories>         </profile>      </profiles>      <activeProfiles>        <activeProfile>dev</activeProfile>      </activeProfiles>

配置之后,客户端的需要依赖的jar,直接从私服下载。

部分内容参考 http://www.jianshu.com/p/e4a3ab0298df