Step2-源码包触发下载
# 1. 核心代码原文
download 任务代码片段
Task t = task "${target}-download"(dependsOn: "${target}_vardefines",
description: "Download $target artifacts",
group: PACKAGES_GROUP) doLast {
def final TARBALL_SRC = config.bigtop.components[target].tarball.source
def final TARBALL_DST = config.bigtop.components[target].tarball.destination
def final DOWNLOAD_DST = config.bigtop.components[target].downloaddst
def final DOWNLOAD_URL = config.bigtop.components[target].downloadurl
def final GIT_REPO = project.hasProperty('git_repo') ? project.property('git_repo') : config.bigtop.components[target].git.repo
def final GIT_REF = project.hasProperty('git_ref') ? project.property('git_ref') : config.bigtop.components[target].git.ref
def final GIT_DIR = project.hasProperty('git_dir') ? project.property('git_dir') : config.bigtop.components[target].git.dir
def final GIT_COMMIT_HASH = project.hasProperty('git_commit_hash') ? project.property('git_commit_hash') : config.bigtop.components[target].git.commit_hash
def final GIT_USER_NAME = config.bigtop.components[target].git.user ?: config.bigtop.git.user
def final GIT_ACCESS_TOKEN = config.bigtop.components[target].git.token ?: config.bigtop.git.token
if (!DOWNLOAD_URL && !(GIT_REPO && GIT_REF))
return
mkdir(DL_DIR)
if (TARBALL_DST?.isEmpty() || new File(DOWNLOAD_DST)?.exists() || new File(config.bigtop.components[target].targetdl)?.exists()) {
println "\tFile $DOWNLOAD_DST appears to be already downloaded. Exiting..."
return
}
if (GIT_REPO && GIT_REF) {
// ... 见下文 ...
} else {
download {
src DOWNLOAD_URL
dest DOWNLOAD_DST
retries 3
}
}
touchTargetFile(config.bigtop.components[target].targetdl)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 2. 逐段详细解读
# 2.1 变量准备
def final TARBALL_SRC = config.bigtop.components[target].tarball.source
def final TARBALL_DST = config.bigtop.components[target].tarball.destination
def final DOWNLOAD_DST = config.bigtop.components[target].downloaddst
def final DOWNLOAD_URL = config.bigtop.components[target].downloadurl
1
2
3
4
2
3
4
- 用途:这些变量都从 BOM 配置自动解析出来,控制本次要拉取的源码包名字、落盘位置和下载 URL。
# 2.2 GIT/鉴权参数判定
def final GIT_REPO = project.hasProperty('git_repo') ? project.property('git_repo') : config.bigtop.components[target].git.repo
def final GIT_REF = project.hasProperty('git_ref') ? project.property('git_ref') : config.bigtop.components[target].git.ref
def final GIT_DIR = project.hasProperty('git_dir') ? project.property('git_dir') : config.bigtop.components[target].git.dir
def final GIT_COMMIT_HASH = project.hasProperty('git_commit_hash') ? project.property('git_commit_hash') : config.bigtop.components[target].git.commit_hash
def final GIT_USER_NAME = config.bigtop.components[target].git.user ?: config.bigtop.git.user
def final GIT_ACCESS_TOKEN = config.bigtop.components[target].git.token ?: config.bigtop.git.token
1
2
3
4
5
6
2
3
4
5
6
- 用途:支持通过 gradle -P 覆盖配置,也可从 BOM 读取默认值。这样既支持批量自动化,也能在 CI 场景临时指定分支/仓库/commit/token。
# 2.3 下载地址校验 & 幂等性检查
if (!DOWNLOAD_URL && !(GIT_REPO && GIT_REF))
return
1
2
2
- 说明:只要没配置 http 源码包链接、又没配 git 仓库+分支,则本次无需下载,直接 return。
mkdir(DL_DIR)
if (TARBALL_DST?.isEmpty() || new File(DOWNLOAD_DST)?.exists() || new File(config.bigtop.components[target].targetdl)?.exists()) {
println "\tFile $DOWNLOAD_DST appears to be already downloaded. Exiting..."
return
}
1
2
3
4
5
2
3
4
5
- 说明:只要源码包已存在(无论 tarball、download 标记文件),任务直接跳过,保证幂等性,可多次重复构建不卡壳。
# 2.4 核心分支:GIT拉取 or URL下载
# 2.4.1 GIT拉取源码(优先)
if (GIT_REPO && GIT_REF) {
def dir = GIT_DIR
if (dir == null || dir.isEmpty()) {
dir = TARBALL_SRC.substring(0, TARBALL_SRC.lastIndexOf(".t"))
}
delete("${DL_DIR}/${dir}")
def depth = "--depth 1"
if (GIT_COMMIT_HASH) {
depth = ""
}
// 私有仓库鉴权分支
if (GIT_USER_NAME && GIT_ACCESS_TOKEN) {
// ... 拼接 http(s) 认证 URL ...
exec {
workingDir DL_DIR
commandLine "git clone ${depth} --branch $GIT_REF ${uri} ${dir}".split()
}
} else {
exec {
workingDir DL_DIR
commandLine "git clone ${depth} --branch $GIT_REF ${GIT_REPO} ${dir}".split()
}
}
exec {
workingDir "$DL_DIR/${dir}"
commandLine "git submodule update --init".split()
}
if (GIT_COMMIT_HASH) {
exec {
workingDir "$DL_DIR/${dir}"
commandLine "git reset $GIT_COMMIT_HASH --hard".split()
}
}
delete("${DL_DIR}/${dir}/.git")
exec {
workingDir DL_DIR
commandLine "tar -czf ${TARBALL_DST} ${dir}".split()
}
delete("${DL_DIR}/${dir}")
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
要点:
- 支持 clone 指定分支或 commit,拉到后清理
.git
,保证打包 tar.gz 干净无多余元数据。 - 支持 submodule 递归拉取,防止依赖遗漏。
- token 拼接只支持 http(s) 协议,SSH 不支持自动注入 token。
- 拉取结束后源码目录即被删除,节省空间、保证可重入。
- 支持 clone 指定分支或 commit,拉到后清理
# 2.4.2 URL 直链下载
else {
download {
src DOWNLOAD_URL
dest DOWNLOAD_DST
retries 3
}
}
1
2
3
4
5
6
7
2
3
4
5
6
7
- 要点:简单直接拉包,遇到网络抖动支持最多三次自动重试。
# 2.5 状态标记,便于后续链路
touchTargetFile(config.bigtop.components[target].targetdl)
1
- 要点:任务收尾时写入 .download 文件,后续所有构建、解压、打包都能自动检测当前组件源码状态,无需重复下载。
# 3. 我的做法
在日常大数据环境集成和自动化打包过程中,我优先推荐使用 URL 直链下载源码包,这种方式稳定、简单、高效,是绝大多数主流组件集成的首选做法。
为什么优先用 URL 下载?
- 配置简单:只需填写 downloadurl 字段,无需关心分支、commit 或仓库权限。
- 下载高效:支持自动断点续传与多次重试,大幅提升批量构建效率。
- 结果可控:下载后的 tarball 文件结构清晰,便于后续自动化解包与打包。
常见配置与用法举例:
download {
src DOWNLOAD_URL
dest DOWNLOAD_DST
retries 3
}
1
2
3
4
5
2
3
4
5
注意只要按下述填好即可
tarball {
destination = "${version.base}.tar.gz"
source = destination
}
url {
site = bigtop.gh_proxy + "https://github.com/redis/redis/archive/refs/tags/"
archive = site
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
- 01
- bigtop-select 打包缺 compat 报错修复 deb07-16
- 02
- bigtop-select 打包缺 control 文件报错修复 deb07-16
- 03
- 首次编译-环境初始化 必装07-16