Linux 部署 nexus2
创建于 2019-08-28
更新于 2019-08-28
科技
nexus
linux

前言

我之所以选择已经不在更新的 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 文件。
bash
1
tar -zxf nexus-latest-bundle.tar.gz
  • 确定存储位位置,使用 df -h 命令查看磁盘使用情况,选取存储空间较大的挂载点。
bash
1
mv nexus-2.14.14-01 targetDir
  • 创建软连接,便于版本维护。
bash
1
sudo ln -s targetDir/nexus-2.14.14-01 /usr/local/nexus
  • 配置环境变量。
bash
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 脚本。

bash
1
sudo cp $NEXUS_HOME/bin/nexus /etc/init.d/nexus
  1. 提升脚本权限。
bash
1
2
chmod 755 /etc/init.d/nexus chown root /etc/init.d/nexus
  1. 修改脚本信息。

    1. NEXUS_HOME 修改为绝对文件路径 (e.g.,NEXUS_HOME="/usr/local/nexus")。
    2. 设置 RUN_AS_USER 为当前用户,或用于运行服务的具有受限权限的任何其他非 Root 用户。
    3. 如果是通过 SDKMAN 安装的 Java,设置 JAVA_HOME=~/.sdkman/candidates/java/current
  2. 注册服务。

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

三、配置

3.1 构建 central 仓库本地索引

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

bash
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 或者使用阿里云的 https://maven.aliyun.com/repository/google

参考

本文作者: 有次元袋的 tiger
本文链接: https://www.superheaoz.top/2019/08/20417/
版权声明: 本站点所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 我的个人天地