Ubuntu 用Nexus3安装配置Maven私服及常见错误

目录

1 Nexus3安装

1.1 手动安装

1.2 Docker镜像文件安装

2 配置策略

3 Setting.xml配置

3.1 节点

3.2 节点

3.3 节点

3.4 节点

4 POM.xml

5 问题排查

5.1 现象1:nexus.log日志中报错

5.2 现象2:无法正常下载组件

6 常见配置错误


资料

Setting.xml 官方指南   

POM.xml 官方指南

阿里云 maven 仓库

1 Nexus3安装

环境条件:nexus3需要 jre 1.8版本的支持

1.1 手动安装

1)官网上下载nexus3安装包,此处是3.35版本

2)在 /usr/local 目录下创建 nexus 子目录,并将安装包复制到该子目录

cd /usr/local
sudo mkdir nexus
sudo cp /home/one/Downloads/nexus-3.35.0-02-unix.tar.gz ./

3)通过 tar 命令解压该安装包

sudo tar zxvf nexus* ./

4)解压后出现两个目录 nexus-3.35.0-02 和 sonatype-work,其中,前者是执行程序目录,后者是

5)将 nexus-3.35.0-02 目录改为 nexus3,简化目录名

sudo mv nexus-3.35.0-02 nexus3

6)编辑 /etc 目录下的 profile

sudo gedit /etc/profile

      在文件尾部增加如下内容:

      export MAVEN_HOME=/usr/local/nexus/nexus3
       PATH=$PATH:$MAVEN_HOME/bin

7)保存并关闭编辑窗口

8)执行如下语句使配置文件生效

source /etc/profile

9)使用如下命令启动nexus

cd nexus3/bin
./nexus start

       注意:若当前帐号不是 root ,则需要用 root 帐号执行,否则会报错

su root
./nexus start

10)nexus默认端口号是8081,故可以使用localhost:8081访问nexus站点

11)设置防火墙打开8081、8082、8083端口,并重起防火墙服务

firewall-cmd --zone=public --permanent --add-port=8081/tcp
firewall-cmd --zone=public --permanent --add-port=8082/tcp
firewall-cmd --zone=public --permanent --add-port=8083/tcp
systemctl restart firewalld

11、项目加载依赖项时的处理优先级:

第一优先级项目POM文件中 <repository> 节点指定的仓库
第二优先级IDE中配置的Settings.xml中指定的仓库
第三优先级Maven默认的Settings.xml中指定的仓库

1.2 Docker镜像文件安装

1)查找 nexus3 的 镜像文件

        执行 docker search nexus 命令,查看中央库中有哪些镜像文件,并确定需要下载的文件名称

[root@serverone bin]# docker search nexus
INDEX       NAME                                                   DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/sonatype/nexus3                              Sonatype Nexus Repository Manager 3             1079
docker.io   docker.io/sonatype/nexus                               Sonatype Nexus                                  448                  [OK]
docker.io   docker.io/sonatype/nexus-iq-server                     Sonatype Nexus IQ Server                        23
docker.io   docker.io/clearent/nexus                                                                               22
docker.io   docker.io/bradbeck/nexus-https                         Dockerized version of Nexus Repo Manager 3...   16                   [OK]
……

2)拉取选定的镜像文件

        执行 docker pull sonatype/nexus3

        注:如果想知道拉下来的文件放在哪里了,可以执行 find / -name nexus3 搜索一下即可。一般存放在 /var/lib/docker/containers 或 /var/lib/docker/overlay2 目录下。

3)启动镜像

        执行下列命令

docker run -d -p 9081:8081 -p 9082:8082 -p 9083:8083 -v /etc/localtime:/etc/localtime --name nexus3   sonatype/nexus3

        其中,容器的8081端口映射到主机的9081端口,8082映射9082,8083映射9083。容器名称为 nexus3,容器的镜像名称为 sonatype/nexus3。

4)打开防火墙端口,并重新启动防火墙

firewall-cmd --zone=public --permanent --add-port=9081/tcp
firewall-cmd --zone=public --permanent --add-port=9082/tcp
firewall-cmd --zone=public --permanent --add-port=9083/tcp
systemctl restart firewalld

5)获取 admin 用户的默认密码

        依次执行下列命令

# 打开容器交互模式的终端
docker exec -it nexus3 /bin/bash

# 显示密码文件的内容
cat /nexus-data/admin.password

        注:密码文件的路径在首次登录nexus站点时有提示

6)nexus服务的web站点是 http://nexusserver:9081 

注意:默认 docker 服务和容器不会自动启动,可以使用下列语句启动:

# 启动 docker 服务
systemctl start docker

# 启动名称为 nexus3 的容器
docker container start nexus3

2 配置策略

        由于当前是希望本地的 nexus 服务能缓存所有远程库的资料,故按如下策略配置:

        1)Setting.xml

                A. 镜像 <mirror> 节点只配置一个,指向 nexus 服务器

                B. 仓库 <repository> 节点配置三个,分别是 nexus-public、nexus-release、nexus-snapshot

                C. 插件仓库 <pluginRepository> 配置一个,名为 nexus-plugin,指向上面nexus-public仓库相同的路径

        2)Nexus

                A. 创建类型为 maven 2 (Proxy) 的仓库,命名为 aliyun-public ,指向阿里云 public 库,

                        a)Version Policy:Release

                        b)Remote storage:https://maven.aliyun.com/repository/public

                B. 将 aliyun-public 加入 maven-public 仓库中,且次序放在第一个

3 Setting.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
  <!--localRepository>/home/one/.m2/nexusrepository</localRepository-->
  <interactiveMode>true</interactiveMode>
  <offline>false</offline>
  <pluginGroups/>
  <servers>
    <server>
      <id>nexus-public</id>
      <username>admin</username>
      <password>123456</password>
    </server>
    <server>
      <id>nexus-releases</id>
      <username>admin</username>
      <password>123456</password>
    </server>
    <server>
      <id>nexus-snapshots</id>
      <username>admin</username>
      <password>123456</password>
    </server>
    <server>
      <id>nexus-plugin</id>
      <username>admin</username>
      <password>123456</password>
    </server>
    <server>
      <id>mirror-public</id>
      <username>admin</username>
      <password>123456</password>
    </server>
  </servers>
  <mirrors>
    <mirror>
      <id>mirror-public</id>
      <name>private-maven-server</name>
      <url>http://nexusserver:9081/repository/maven-public</url>
      <mirrorOf>*</mirrorOf>
    </mirror>
  </mirrors>
  <proxies/>
  <profiles>
    <profile>
      <id>nexus</id>
      <repositories>
        <repository>
          <id>nexus-releases</id>
          <url>http://nexusserver:9081/repository/maven-releases</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>false</enabled></snapshots>
        </repository>
        <repository>
          <id>nexus-snapshots</id>
          <url>http://nexusserver:9081/repository/maven-snapshots</url>
          <releases><enabled>false</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </repository>
        <repository>
          <id>nexus-public</id>
          <url>http://nexusserver:9081/repository/maven-public</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </repository>
      </repositories>
      <pluginRepositories>
        <pluginRepository>
          <id>nexus-plugin</id>
          <url>http://nexusserver:9081/repository/maven-public</url>
          <releases>
            <enabled>true</enabled>
          </releases>
          <snapshots>
            <enabled>false</enabled>
          </snapshots>
        </pluginRepository>
    </pluginRepositories>
    </profile>
  </profiles>
  <activeProfiles>
    <activeProfile>nexus</activeProfile>
  </activeProfiles>
</settings>

3.1 <Server>节点

        用于配置镜像和仓库的访问账户和密码

  • id:必须与 <mirror>和<repository> 节点中的 id 保持一致
  • username 和 password:是对应节点访问的账户名和密码

3.2 <repository>节点

        用于配置开发用程序包的仓库路径及特性

  • id:仓库唯一编号
  • url:仓库的访问地址
  • releases:是否包含 Release 版本的包
  • snapshot:是否包含 Snapshot 版本的包

3.3 <pluginRepository>节点

        用于配置 Maven 使用的插件下载的仓库路径及特性

  • id:仓库唯一编号
  • url:仓库的访问地址
  • releases:是否包含 Release 版本的包
  • snapshot:是否包含 Snapshot 版本的包

3.4 <activeProfiles>节点

        用于配置当前使用哪个配置项

  • activeProfile:此处的值必须与 <profile>节点中的 id 节点的值一致

4 POM.xml

<distributionManagement>
    <repository>
        <id>nexus-releases</id>
        <url>http://nexusserver:9081/repository/maven-releases/</url>
    </repository>
    <snapshotRepository>
        <id>nexus-snapshots</id>
        <url>http://nexusserver:9081/repository/maven-snapshots/</url>
    </snapshotRepository>
</distributionManagement>

        上文中的 id 必须与 Setting.xml 中的 <repository> 节点的 id 一致

5 问题排查

5.1 现象1:nexus.log日志中报错

nexus.log日志中报错内容如下:

dmin com.sonatype.nexus.plugins.outreach.internal.outreach.SonatypeOutreach - Could not download page bundle org.apache.http.conn.ConnectTimeoutException: Connect to sonatype-download.global.ssl.fastly.net:443 [sonatype-download.global.ssl.fastly.net/108.160.172.1] failed: connect timed out

解决方法:

a) 访问Nexus管理站点,我这里是 http://localhost:8081,进入后,用管理员帐号登录。

b)点击 下图中上面的按钮,然后选择左侧 Capabilities,然后 点击右侧的 Outreach:Management

c)点击【disable】按钮,关闭Outreach服务

或者给Outreach设定新的URL,可以在标签页 Settings 中的 Override Outreach Content URL项中设定。Nexus支持的URL有如下三个:

  • http://links.sonatype.com and https://links.sonatype.com
  • http://download.sonatype.com and https://download.sonatype.com
  • http://sonatype-download.global.ssl.fastly.net and https://sonatype-download.global.ssl.fastly.net

5.2 现象2:无法正常下载组件

配置好IDE用的Settings.xml后一直无法正常下载,IDEA里执行maven clear 时,始终报操作中止,也不输出任何有效信息。

解决:用控制台,执行 mvn install,其会输出实际错误原因,如下:

错误中提示一节点未正确关闭,修改后,再次执行 mvn install,依旧报错,如下:

第一条ERROR信息中提供了明确的原因: /etc/maven 目录下没有POM文件。为此,从项目中拷贝一个现成的POM.xml文件过来,再次运行,即可通过。

6 常见配置错误

1、在<Mirrors>节点配置了除了自己私服外,还配置了其他仓库。若指向私服的<mirrorOf>节点为*且该镜像作为第一项,则排在其后的其他镜像理论上无效,但实测有效(当 * 代表的仓库无法提供指定包时,后面的镜像就有效了)。若其他镜像有效时,则通过这些镜像下载的组件库不会在私服中出现,这样就起不了私服减少外网网络访问量及组件包备份的效果。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值