我之所以选择已经不在更新的 Nexus2,是因为 Nexus3 目前不支持本地索引,这在内网是一个很有必要的需求.

一、 准备环境

操作系统: Centos 7
Java: 版本至少在 1.8,推荐使用 SDKMAN 配置 Java 环境
Nexus 版本: Nexus Repository Manager OSS 2.14.14-01
索引生成工具: indexer-cli-5.1.2-816025a.jar
Maven 中央仓库离线索引: nexus-maven-repository-index.gz

二、 安装

2.1 部署 nexus

  • 解压缩下载好的 nexus 文件

    1
    tar -zxf nexus-latest-bundle.tar.gz
  • 确定存储位位置,使用df -h命令查看磁盘使用情况,选取存储空间较大的挂载点.

    1
    mv nexus-2.14.14-01 targetDir
  • 创建软连接,便于版本维护

    1
    sudo ln -s targetDir/nexus-2.14.14-01 /usr/local/nexus
  • 配置环境变量

    1
    2
    3
    sudo vi /etc/profile.d/nexus.sh
    export NEXUS_HOME="/usr/local/nexus"
    source /etc/profile
  • 配置端口\仓库位置
    nexus 默认仓库在$NEXUS_HOME/sonatype-work/nexus目录下,默认端口为8081,可以通过修改$NEXUS_HOME/conf/nexus.properties文件改变配置

    1. 修改application-port可以改变运行时端口
    2. 修改nexus-work可以改变仓库存储路径,但程序缓存还是会在$NEXUS_HOME/tmp

2.2 配置 nexus 为系统服务

  1. (可选)创建具有足够访问权限的nexus用户

  2. 创建init.d脚本

    1
    sudo cp $NEXUS_HOME/bin/nexus /etc/init.d/nexus
  3. 提升脚本权限

    1
    2
    chmod 755 /etc/init.d/nexus
    chown root /etc/init.d/nexus
  4. 修改脚本信息
    a. 将NEXUS_HOME修改为绝对文件路径(e.g., NEXUS_HOME="/usr/local/nexus")
    b. 设置RUN_AS_USER为当前用户,或用于运行服务的具有受限权限的任何其他非 Root 用户
    c. 如果是通过 SDKMAN 安装的 Java,设置JAVA_HOME=~/.sdkman/candidates/java/current

  5. 注册服务

    1
    2
    3
    cd /etc/init.d
    chkconfig --add nexus
    chkconfig --levels 345 nexus on
  • 启动 nexus

    1
    service nexus start

三、 配置

3.1 构建 central 仓库本地索引

通过以下命令即可利用提前下载好的生成工具和索引压缩包构建索引

1
2
3
java -jar indexer-cli-5.1.2-816025a.jar.jar -u nexus-maven-repository-index.gz -d indexer
mv ./indexer/* nexus仓库路径/indexer/central-ctx/
service nexus restart

3.2 添加第三方仓库

nexus2 的访问地址为http://ip:port/nexus,默认管理员账号为admin,默认管理员密码为admin123.登录后从左侧菜单栏进入repositories,如下图所示进行代理的添加

nexus

可选配置文件如下:(依赖的 ID 和 Name 可以任意起)

Repository ID Repository Name Provider Repository Policy Remote Storage Location
apache-snapshots Apache Snapshots Maven2 Snapshot https://repository.apache.org/snapshots/
jcenter jcenter Maven2 Release http://jcenter.bintray.com/
mapr-releases mapr-releases Maven2 Release http://repository.mapr.com/maven/
confluent confluent Maven2 Release http://packages.confluent.io/maven/
sonatype OSS Sonatype Staging Maven2 Release https://oss.sonatype.org/content/groups/staging/
spring spring Maven2 Release http://repo.spring.io/libs-milestone/
spring-plugin spring-plugin Maven2 Release http://repo.spring.io/plugins-release/
gradle-plugin gradle-plugin Maven2 Release https://plugins.gradle.org/m2/
grails-core grails-core Maven2 Release https://repo.grails.org/grails/core/
npmjs npmjs registry npm Release http://registry.npm.taobao.org/

网上的教程常有人说代理 Google 的仓库,但是我在尝试的时候发现访问不了,提示In Service-Remote Automatically Blocked and Unavailable.在查询过StackOverflow后发现这是 Nexus 的已知Bug,在 Nexus 3.16.0 中完成了修复,所以想使用 Goolge 代理仓库的小伙伴只能移步 Nexus3 或者使用阿里云的 Goolge 仓库代理.