惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

有赞技术团队
有赞技术团队
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
P
Palo Alto Networks Blog
C
Cisco Blogs
The Hacker News
The Hacker News
T
Threatpost
S
Schneier on Security
K
Kaspersky official blog
Spread Privacy
Spread Privacy
博客园_首页
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
NISL@THU
NISL@THU
量子位
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Google DeepMind News
Google DeepMind News
Security Latest
Security Latest
博客园 - 司徒正美
云风的 BLOG
云风的 BLOG
博客园 - 叶小钗
H
Hackread – Cybersecurity News, Data Breaches, AI and More
N
News and Events Feed by Topic
爱范儿
爱范儿
P
Proofpoint News Feed
C
CERT Recently Published Vulnerability Notes
Project Zero
Project Zero
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Cisco Talos Blog
Cisco Talos Blog
GbyAI
GbyAI
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Apple Machine Learning Research
Apple Machine Learning Research
T
Tenable Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
V
Vulnerabilities – Threatpost
Forbes - Security
Forbes - Security
博客园 - 三生石上(FineUI控件)
C
Cyber Attacks, Cyber Crime and Cyber Security
N
News and Events Feed by Topic
V
V2EX
Webroot Blog
Webroot Blog
The Register - Security
The Register - Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
阮一峰的网络日志
阮一峰的网络日志
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Blog — PlanetScale
Blog — PlanetScale
M
MIT News - Artificial intelligence
Scott Helme
Scott Helme
Simon Willison's Weblog
Simon Willison's Weblog
L
LangChain Blog
W
WeLiveSecurity
Cloudbric
Cloudbric

二丫讲梵

学习周刊-总第258期-2026年第15周 学习周刊-总第257期-2026年第14周 学习周刊-总第256期-2026年第13周 学习周刊-总第255期-2026年第12周 学习周刊-总第254期-2026年第11周 学习周刊-总第253期-2026年第10周 临时插播一条羊毛,免费领取450元大模型API代金券 学习周刊-总第252期-2026年第09周 学习周刊-总第251期-2026年第08周 学习周刊-总第250期-2026年第07周 学习周刊-总第249期-2026年第06周 诚邀评论,聊聊你所知欲知的我 学习周刊-总第248期-2026年第05周 我的QQ动态之2015年 我的QQ动态之2014年 我的QQ动态之2013年 我的QQ动态之2012年 我的QQ动态-2010-2011年 我的QQ动态-创栏小叙 学习周刊-总第247期-2026年第04周 Nexus社区版权益阉割--一文告诉你有哪些版本可以选择 学习周刊-总第246期-2026年第03周 学习周刊-总第245期-2026年第02周 学习周刊-总第244期-2026年第01周 学习周刊-总第243期-2025年第52周 学习周刊-总第242期-2025年第51周 开源项目ZenOps:带你领略禅意运维 学习周刊-总第241期-2025年第50周 用京东金融,享负债人生 学习周刊-总第240期-2025年第49周 学习周刊-总第239期-2025年第48周 整理我在静态服务透明代理上的极致求索之路,最后一个你绝想不到 学习周刊-总第238期-2025年第47周 我们输了,但收获颇多 太多了,太多了 学习周刊-总第237期-2025年第46周 学习周刊-总第236期-2025年第45周 学习周刊-总第235期-2025年第44周 二五年国庆二三事 学习周刊-总第234期-2025年第43周 学习周刊-总第233期-2025年第42周 学习周刊-总第232期-2025年第41周 学习周刊-总第231期-2025年第40周 学习周刊-总第230期-2025年第39周 西湖毅行 2025年开源世界逸闻三则 学习周刊-总第229期-2025年第38周 手抄《与妻书》 CNB开发与构建基于docker-cache缓存复用的配置实践心得 学习周刊-总第228期-2025年第37周 父亲善行录 学习周刊-总第227期-2025年第36周 带你认识我之看看我的高中同学录(其二) 学习周刊-总第226期-2025年第35周 带你认识我之看看我的高中同学录(其一) 认识神级MCP工具系列--用anyquery和数据库交互 学习周刊-总第225期-2025年第34周 学习周刊-总第224期-2025年第33周 学习周刊-总第223期-2025年第32周 学习周刊-总第222期-2025年第31周 学习周刊-总第221期-2025年第30周 CNB云原生开发环境届的瑞士军刀,详解qifei项目 vuepress-vdoing主题配置自建不蒜子统计 学习周刊-总第220期-2025年第29周 写在博客发表文章1000篇的节点 从claude cli的体验聊聊最大的敌人是我们自己的成见 学习周刊-总第219期-2025年第28周 学习周刊-总第218期-2025年第27周 学习周刊-总第217期-2025年第26周 理论正确,事实错误 学习周刊-总第216期-2025年第25周 学习周刊-总第215期-2025年第24周 学习周刊-总第214期-2025年第23周 学习周刊-总第213期-2025年第22周 学习周刊-总第212期-2025年第21周 从赵心童世锦赛夺冠聊聊我的斯诺克情缘 学习周刊-总第211期-2025年第20周 记录二五年五一之短暂回归家庭 学习周刊-总第210期-2025年第19周 学习周刊-总第209期-2025年第18周 学习周刊-总第208期-2025年第17周 Go开发实践之Gin框架将前端的dist目录embed到二进制 学习周刊-总第207期-2025年第16周 近期关于cobra库的一些实践心得总结 学习周刊-总第206期-2025年第15周 我的嗜好--嗑瓜子 记大宝参加幼儿园组织的清明烈士陵园扫墓活动 学习周刊-总第205期-2025年第14周 学习周刊-总第204期-2025年第13周 前端开发小笔记--在数组中给字段添加值与删除字段的操作 学习周刊-总第203期-2025年第12周 人生实苦,何以自渡 学习周刊-总第202期-2025年第11周 近期发现的一些优秀的工具提名(一) 学习周刊-总第201期-2025年第10周 学习周刊-总第200期-2025年第09周 记一次女友喝醉使我忍术破功 爱情风波--一次分手又复合的经历 学习周刊-总第199期-2025年第08周 整理我的信息源
关于gradle中plugins依赖未走私服导致编译失败的原因探析
二丫讲梵 · 2023-06-04 · via 二丫讲梵

# 背景

我们的项目标配使用gradle进行编译,我们也配置了nexus作为私服,理论上来说,不应该再会出现因包依赖的原因导致编译失败的情况,但是这个问题仍旧会偶尔发生,我解决过一两次,只看到通常失败的编译都是在开头的时候去 plugins.gradle.org 拉包的情况,当时将该问题存疑,今天特来探析一下导致这个问题的根因。

# 摸索

简单分析: 理论上来说,当我们的gradle通过build.gradlerepositories指向私服地址之后,就不应该再出现任何一个第三方的私服地址才对,但 gradle 在这里就是异常头铁,偏偏会在开头请求一下官方地址。这是彼时遇到问题之后的一个原始的疑问!

缘起: 今天一位开发同学反馈说某项目在gray环境打包的时候出现编译失败的情况。我看到构建日志如下:

+ gradle --no-daemon bootJar

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring root project 'example'.
> Could not resolve all artifacts for configuration ':classpath'.
   > Could not resolve org.springframework.boot:spring-boot-gradle-plugin:2.1.5.RELEASE.
     Required by:
         project : > org.springframework.boot:org.springframework.boot.gradle.plugin:2.1.5.RELEASE
      > Could not resolve org.springframework.boot:spring-boot-gradle-plugin:2.1.5.RELEASE.
         > Could not parse POM https://plugins.gradle.org/m2/org/springframework/boot/spring-boot-gradle-plugin/2.1.5.RELEASE/spring-boot-gradle-plugin-2.1.5.RELEASE.pom
            > Could not resolve org.springframework.boot:spring-boot-tools:2.1.5.RELEASE.
               > Could not resolve org.springframework.boot:spring-boot-tools:2.1.5.RELEASE.
                  > Could not get resource 'https://plugins.gradle.org/m2/org/springframework/boot/spring-boot-tools/2.1.5.RELEASE/spring-boot-tools-2.1.5.RELEASE.pom'.
                     > Could not HEAD 'https://plugins.gradle.org/m2/org/springframework/boot/spring-boot-tools/2.1.5.RELEASE/spring-boot-tools-2.1.5.RELEASE.pom'.
                        > Remote host closed connection during handshake

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

通常遇到这种问题,我都会到对应构建的 Jenkins 服务器上,手动跑一次编译,然后 ci 机器再次打包就能过去了。这次使用同样手法将问题解决之后,我打算探究一下导致该问题的根因是什么。

后来了解到运维侧在打包的机器上有一个定时清理本地缓存的操作,是为了避免一些包依赖不更新的问题。所以当缓存清空之后,这些依赖就会重新下载,就可能因为网络原因而失败。

首先看到了报错提示的包基本都是 plugins,于是看了一眼 build.gradle 文件,可以看到在文件中有这样的配置声明:

plugins {
    id 'org.springframework.boot' version '2.1.5.RELEASE'
    id 'java'
}

1
2
3
4

报错的提示也正是在拉取该插件的时候报错的。

所以,这里得到一个问题是:即便已经配置指向了私服,但是此处的 plugins 中的包拉取的时候仍旧绕过了私服。

这是为什么呢?

通过 Google,找到了官方文档 (opens new window) 对 plugins 功能的介绍,并且里边也介绍了plugins 的仓库指向 (opens new window),需要通过单独的配置文件进行配置:

$ cat settings.gradle # 注意需要把配置文件放到这个文件的开头,而非build.gradle中
pluginManagement {
    repositories {
        maven {
            url './maven-repo'
        }
        gradlePluginPortal()
        ivy {
            url './ivy-repo'
        }
    }
}

1
2
3
4
5
6
7
8
9
10
11
12

这里我做了下测试,将如下配置添加到了项目对应的配置中:

$ cat settings.gradle
pluginManagement {
    repositories {
        //本地资源库
        mavenLocal()
        //自定义maven仓库
        maven {
            name 'aifocus-repository'
            url 'http://nexus.eryajf.net/nexus/content/groups/public/'
        }
    }
}

......

1
2
3
4
5
6
7
8
9
10
11
12
13
14

接着执行构建,发现如下报错:

FAILURE: Build failed with an exception.

* Where:
Build file '/data/gradle/example/build.gradle' line: 2

* What went wrong:
Plugin [id: 'org.springframework.boot', version: '2.1.5.RELEASE'] was not found in any of the following sources:

- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (could not resolve plugin artifact 'org.springframework.boot:org.springframework.boot.gradle.plugin:2.1.5.RELEASE')
  Searched in the following repositories:
    MavenLocal(file:/root/.m2/repository)
    aifocus-repository(http://nexus.eryajf.net/nexus/content/groups/public/)

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 3s

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

在私服仓库中,找不到这个包,需要注意的是,虽然通过org.springframework.boot 能够搜索到包,但是这个包还有gradle.plugin的 path,所以如下检索到的包并不符合条件。

那么就需要将官方地址代理进来,此处要注意一个坑点在于,官方代理的正确地址是 https://plugins.gradle.org/m2/ 而非 https://plugins.gradle.org ,此处踩坑可见该帖子 (opens new window)

国内一些镜像库也有 plugin 的镜像,所以这里直接配置了阿里的镜像仓库:https://maven.aliyun.com/repository/gradle-plugin/

当我把官方地址加入到私服之后,再次回来进行构建,可以看到这个插件通过我们的私服进行拉取了,而正确拉取之后,私服当中也缓存了该依赖,符合预期,喜大普奔!

申明

原创文章eryajf,未经授权,严禁转载,侵权必究!此乃文中随机水印,敬请读者谅解。

# 结论

这个问题以后就变得简单了,所有使用 gradle 打包的项目,只要使用到了 plugins,那么就应该在 settings.gradle 文件的开头,加上如下配置:

pluginManagement {
    repositories {
        //本地资源库
        mavenLocal()
        //自定义maven仓库
        maven {
            name 'aifocus-repository'
            url 'http://nexus.eryajf.net/nexus/content/groups/public/'
        }
    }
}

1
2
3
4
5
6
7
8
9
10
11

从此不会再有与 plugins.gradle.org 这个域名交互的情况出现,自然也与编译失败说拜拜了!