Sonatype Nexus 私服的安装(Windows 篇)

本文是针对 Sonatype Nexus Repository OSS Windows 绿色版的安装指引。

注:Nexus Repository OSS 只支持 64 位的 Windows 操作系统,32 位的不支持。

官方最新版下载地址为:https://www.sonatype.com/nexus-repository-oss
历史版本下载地址为:https://help.sonatype.com/repomanager3/download/download-archives---repository-manager-3

当前(2020-02-02)最新版为 3.20.1-01,Windows 版的直接下载地址为:https://sonatype-download.global.ssl.fastly.net/repository/repositoryManager/3/nexus-3.20.1-01-win64.zip

官网下载速度在国内非常的慢,已下载缓存到网盘: https://pan.baidu.com/s/1QeW-RH5YtV8MV3gkDqYKVg 提取码: v2mi

一)nexus 安装步骤

  1. 解压下载的 nexus-3.20.1-01-win64.zip 到目录 D:\path\to\nexus\,得到如下目录结构:

    D:\path\to\nexus\
    |--nexus-3.20.1-01
    |  |--bin\
    |  |  |--contrib\...
    |  |  |--nexus.exe
    |  |  |--nexus.vmoptions
    |  |--deploy\...
    |  |--etc\...
    |  |--jre\...
    |  |--lib\...
    |  |--public\...
    |  |--system\...
    |  |--...
    |--sonatype-work\
    |  |--nexus3\
    |  |  |--log\
    |  |  |--orient\
    |  |  |  |--plugins\
    |  |  |  |  |--studio.zip
    |  |  |--tmp\
    |  |  |--clean_cache
    

    目录 D:\path\to\nexus\ 需根据自身需要设置为实际的磁盘目录,下同。

  2. 在与 nexus-3.20.1-01 同级目录下创建软链接 nexus-latest 并链接到此目录以方便日后的升级

    > cd D:\path\to\nexus\
    > mklink /D nexus-latest nexus-3.20.1-01
    为 nexus-latest <<===>> nexus-3.20.1-01 创建的符号链接
    

    mklink 命令需要在以管理员身份打开的 CMD 中才支持。

  3. D:\path\to\nexus\nexus-latest\bin 添加到系统环境变量 Path

    这样才能保证在命令行中直接使用 nexus 命令。

  4. 执行 nexus /start 启动 nexus 服务

    > nexus /start
    Starting nexus
    

    执行 nexus /? 命令可查看可用的命令清单:

    > nexus /?
    Usage: D:\path\to\nexus\nexus-latest\bin\nexus {start|stop|run|run-redirect|status|restart|force-reload}
    

    启动后访问 http://localhost:8081 打开 nexus 主页:

    nexus-main-page.png

    点击右上角的 Sign in 可以 admin 账号登陆进行相关配置,首次点击 Sign in,会提示管理员 admin 账号的初始密码已自动生成在文件 sonatype-work/nexus3/admin.password 内,以此密码登陆后会继续弹出对话框要求修改为新的密码:

    nexus-change-password.png

    之后会再弹出一个对话框设置是否允许匿名浏览、下载仓库中的包:(建议勾选)

    nexus-enable-anonymous-access.png
  5. 添加第三方 maven 仓库的代理配置到 nexus
    以管理员账号 admin 登陆 nexus,打开 Repositories 配置界面,
    默认情况下 nexus 仅配置了 maven 中心仓库的代理 maven-central。
    maven-public 为所有代理仓库的自定义汇集,用于控制 nexus 按特定顺序分别从配置的第三方仓库中下载依赖包。
    按照个人习惯,我一共配置了如下几个第三方 maven 仓库的代理配置:

    nexus-repositories.png
    nexus-maven-public.png

    详细可参考官方配置文档 《Repository Management》

  6. 【可选配置】如果要安装为系统服务,以管理员身份执行 nexus /install 即可。

    卸载服务则执行 nexus /uninstall

  7. 【可选配置】自定义 nexus 相关配置参数(特殊情况才需要修改,一般不建议修改):
    创建文件 D:\path\to\nexus\sonatype-work\nexus3\etc\nexus.properties,添加如下配置:

    # 自定义 web 访问端口
    application-port=8081
    
    # 自定义 Web 上下文路径 (以 / 开头,不要以 / 结尾)
    nexus-context-path=/
    

    参考 D:\path\to\nexus\nexus-latest\etc\nexus-default.properties 文件内的配置参数进行配置即可。

    如果要迁移数据目录 sonatype-work\nexus3 到其它地方,需要修改文件 nexus-latest\bin\nexus.vmoptions,这个不建议修改。

    注:配置文件修改后必须重新启动 nexus 服务才能生效:

    > nexus /restart
    
  8. 【可选配置】升级 nexus

    1. 执行 nexus /stop 命令停止当前已启动的 nexus 服务:
      > nexus /stop
      Shutting down nexus
      Stopped.
      
    2. 下载最新版的 nexus 包,解压后仅保留 nexus-3.{new-version} 目录,并移动到 D:\path\to\nexus\ 目录下,然后重新创建软链接 nexus-latest,指向新版本的 nexus 目录即可:
      > cd D:\path\to\nexus\
      > md nexus-latest
      > mklink /D nexus-latest nexus-3.{new-version}
      
    3. 执行 nexus /start 重新启动 nexus 服务。

    以上升级方法适用于从 nexus-3.1.0+ 升级到 nexus3 的最新版本,官方文档为《Upgrading Nexus Repository Manager 3.1.0 and Newer

较低版本 nexus3 的默认管理员账号是 admin,密码是 admin123(如 nexus-3.14),如果从较低版本升级到当前最新版(如 nexus-3.20),使用 admin 账号首次登陆后,系统会提示你更新密码一次及设置是否允许匿名浏览、下载仓库中的包。类似于上面步骤 4 的相关截图。

二)maven 相关配置

  1. 配置 maven 使用 nexus 代理所有依赖的下载:
    修改 maven 的用户配置文件 ~/.m2/settings.xml
    <settings>
      <mirrors>
        <mirror>
          <!--This sends everything else to maven-public -->
          <id>nexus</id>
          <mirrorOf>*</mirrorOf>
          <!-- http://[ip]:[port][/nexus-context-path]/repository/[public-repository-name] -->
          <url>http://localhost:8081/repository/maven-public</url>
        </mirror>
      </mirrors>
      <profiles>
        <profile>
          <id>nexus</id>
          <!-- 
            Enable snapshots for the built in central repo to direct
            all requests to nexus via the mirror
          -->
          <repositories>
            <repository>
              <id>central</id>
              <url>http://central</url>
              <releases><enabled>true</enabled></releases>
              <snapshots><enabled>true</enabled></snapshots>
            </repository>
          </repositories>
          <pluginRepositories>
            <pluginRepository>
              <id>central</id>
              <url>http://central</url>
              <releases><enabled>true</enabled></releases>
              <snapshots><enabled>true</enabled></snapshots>
            </pluginRepository>
          </pluginRepositories>
        </profile>
      </profiles>
      <activeProfiles>
        <activeProfile>nexus</activeProfile>
      </activeProfiles>
    </settings>
    

    详细可参考官方指引文档 《Proxying Maven and npm Quick Start》

  2. 【可选配置】配置将 maven 项目打包发布到此 nexus 私服:
    1. 修改 ~/.m2/settings.xml 文件,添加如下配置:
      <settings>
        ...
        <servers>
          <!-- account and password for deploy to nexus -->
          <server>
            <id>lan</id>
            <username>admin</username>
            <password>admin123</password>
          </server>
        </servers>
        <profiles>
          <profile>
            <id>lan</id>
            <properties>
              <lan-release-url>http://localhost:8081/repository/maven-releases</lan-release-url>
              <lan-snapshot-url>http://localhost:8081/repository/maven-snapshots</lan-snapshot-url>
            </properties>
          </profile>
        </profiles>
      </settings>
      
    2. 修改 maven 项目的 pom.xml 文件,添加如下配置:
      <project>
        <profiles>
          <!-- configuration for deploy to nexus repository -->
          <profile>
            <id>lan</id>
            <distributionManagement>
              <repository>
                <id>lan</id>
                <url>${lan-release-url}</url>
              </repository>
              <snapshotRepository>
                <id>lan</id>
                <url>${lan-snapshot-url}</url>
              </snapshotRepository>
            </distributionManagement>
            <build>
              <plugins>
                <plugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-source-plugin</artifactId>
                </plugin>
                <plugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-javadoc-plugin</artifactId>
                </plugin>
              </plugins>
            </build>
          </profile>
        </profiles>
      </project>
      

      命令行执行 mvn clean deploy -P lan 即可将项目打包发布到 nexus 私服。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,233评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,013评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,030评论 0 241
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,827评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,221评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,542评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,814评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,513评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,225评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,497评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,998评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,342评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,986评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,055评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,812评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,560评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,461评论 2 266

推荐阅读更多精彩内容