标签 nexus 下的文章

背景

项目采用Jenkins+Maven实现自动化部署springboot项目。本地运行正常,但是gitlab提交后,执行自动化部署过程中,出现了如下错误:

[INFO] Downloading from central: https://repo.maven.apache.org/maven2/org/apache/poi/poi-ooxml/3.17/poi-ooxml-3.17.jar
[INFO] Downloading from central: https://repo.maven.apache.org/maven2/org/apache/xmlbeans/xmlbeans/2.6.0/xmlbeans-2.6.0.jar
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for  
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  02:14 min
[INFO] Finished at: 2022-03-14T06:17:49Z
[INFO] ------------------------------------------------------------------------
Waiting for Jenkins to finish collecting data
[ERROR] Failed to execute goal on project **-server: Could not resolve dependencies for project *****:jar:1.0-SNAPSHOT: The following artifacts could not be resolved: org.apache.poi:poi-ooxml:jar:3.17, org.apache.xmlbeans:xmlbeans:jar:2.6.0: Could not transfer artifact org.apache.poi:poi-ooxml:jar:3.17 from/to central (https://repo.maven.apache.org/maven2): Transfer failed for https://repo.maven.apache.org/maven2/org/apache/poi/poi-ooxml/3.17/poi-ooxml-3.17.jar: Connect to repo.maven.apache.org:443 [repo.maven.apache.org/151.101.40.215] failed: Connection timed out (Connection timed out) -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.

这里的描述很清楚,本地找不到poi-ooxml包,去中央仓库下载,然后国内的网络环境下,无法在https://repo.maven.apache.org下载该包。导致了构建失败。

过程及分析

一、确认Jenkins配置是否正确

首先我想到的是maven配置问题,因为我使用的是自己部署的nexus3私库,在Jenkins-管理管理-Managed files中添加了settings.xml文件并在项目中进行了配置使用。反复检查了settings.xml文件,确认没有问题。

<!-- 检查是否添加了私库镜像 -->
  <mirrors>
    <mirror>
        <id>nexus</id>
        <mirrorOf>*</mirrorOf>
        <url>http://私库Ip/nexus/repository/maven-public/</url>
    </mirror>
  </mirrors>

二、nexus配置

后来想到,可能是nexus中配置的代理仓库没有修改成国内镜像。查看后发现也没有问题。nexus中添加了阿里云镜像的代理仓库。

请输入图片描述

并把其放在了靠前的位置。
请输入图片描述

三、pom.xml配置

以上检查无误后,陷入了僵局,这个问题困扰了我一整天。期间我一度想直接在本地拷贝了这个包放到Jenkins的本地仓库中,然后问题依然存在,o(╯□╰)o。
直到后来,无意中查询到, 自定义pom.xml都是继承自super pom,所以maven项目下载一些jar包时,默认会从中央仓库下载 。知道了这点,剩下的就好办了,只要在pom.xml中进行配置,覆盖掉super pom的配置即可。

<!-- pom.xml文件中添加如下配置 -->
 <repositories>
        <repository>
            <id>nexus</id>
            <url>http://nexus.dev.dhdc.com/nexus/repository/maven-public/</url>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>nexus</id>
            <url>http://nexus.dev.dhdc.com/nexus/repository/maven-public/</url>
        </pluginRepository>
    </pluginRepositories>

说明

看标题很绕,那是因为我目前也不知道怎么处理,但是误打误撞找到了一个临时解决方案,为了防止以后复现此类问题无法解决,这里简单记录一下。

背景

springboot开发的项目、本地使用nexus搭建了maven库,使用gitlab做代码管理,同时配合jenkins进行自动部署。
整体流程就是提交代码到gitlab后,触发jenkins进行编译并部署到docker容器。开发过程中使用了一个第三方的jar包,手动上传到了nexus库中。

问题

清理了jenkins中的.m2缓存的包后,再次在jenkins中构建会提示如下错误:

Downloaded from maven-releases: http://我的nexusip/nexus/repository/maven-releases/e-iceblue/spire.presentation.free/3.9.0/spire.presentation.free-3.9.0.jar (37 MB at 64 MB/s)
[INFO] 
...
Waiting for Jenkins to finish collecting data
[ERROR] Failed to execute goal on project data-maintain: Could not resolve dependencies for project 我的项目:jar:0.0.1-SNAPSHOT: Could not find artifact e-iceblue:spire.presentation.free:jar:3.9.0 in central (https://repo.maven.apache.org/maven2) -> [Help 1]
[ERROR] 

这里的spire.presentation.free-3.9.0.jar便是我使用的第三方jar包,很明显前面已经从我的nexus库中下载了,但是后面又去repo.maven.apache.org中去查找没找的报错了。

解决方案

这个问题困扰了我好久,至今也没找到方案,不过阴差阳错间,找到了个临时方法。过程如下:

  1. pom文件中原本是这样的
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <repositories>
        <repository>
            <id>central</id>
            <url>http://我的nexus地址/nexus/repository/maven-releases</url>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>central</id>
            <url>http://我的nexus地址/nexus/repository/maven-releases</url>
        </pluginRepository>
    </pluginRepositories>

    <modelVersion>4.0.0</modelVersion>
...
  1. 提交,并自动部署。会报上述错误。
  2. 将pom修改为:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <repositories>
        <repository>
            <id>com.e-iceblue</id>
            <url>http://repo.e-iceblue.cn/repository/maven-public/</url>
        </repository>
    </repositories>
    <modelVersion>4.0.0</modelVersion>
...
  1. 提交,并触发自动部署。仍会报错。
  2. 修改pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<!-- 删除了这些配置-->
    <modelVersion>4.0.0</modelVersion>
  1. 提交,并jenkins进行构建。
  2. 竟然好了。

总结

先这样吧,后面找到了具体原因,在更新。