[O]Hadoop版本适配改造(二)1.0.0+
# 背景说明
在大数据组件的自动化编译与 CI 流程中,前端链路调用 node/yarn
构建时常因偶发网络抖动、依赖仓库连接超时而导致失败,尤其在企业内部网络或半离线环境下更为突出。针对这一痛点,本次改造对 hadoop-yarn-project/hadoop-yarn/hadoop-yarn-ui/pom.xml
的 exec-maven-plugin
配置进行了增强,让构建自动具备“失败自动重试”的能力。
# 主要改造点一览
路径/文件位置 | 主要变更点 | 说明 |
---|---|---|
hadoop-yarn-project/hadoop-yarn/hadoop-yarn-ui/pom.xml | 构建命令支持自动重试 | exec-maven-plugin 由直接调用 node/yarn 改为 bash 包裹并自动重试,提升容错 |
# 1. 传统配置的问题
历史上,exec-maven-plugin
直接用 node/yarn 构建:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<!-- Ember Build -->
<execution>
<id>ember build</id>
<phase>generate-resources</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<workingDirectory>${webappDir}</workingDirectory>
<executable>${nodeExecutable}</executable>
<arguments>
<argument>${packageManagerScript}</argument>
<argument>run</argument>
<argument>build:mvn</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
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
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
注意
此方式遇到网络抖动或 npm 仓库瞬时失联,构建会直接中断,对 CI/CD 环境极为不友好。
# 2. 自动重试机制的引入
本次改造,将原配置完全注释,换成 Bash 脚本包装,支持自动重试 3 次:
提示
核心思路是用 bash 的 while 循环包裹 node/yarn 调用,最大重试次数可自定义(当前为 3),只要有一次成功就 break 出循环,否则最终 fail 并输出详细日志。
# 关键实现片段
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<!-- Ember Build -->
<execution>
<id>ember build</id>
<phase>generate-resources</phase>
<goals>
<goal>exec</goal>
</goals>
<!-- <configuration>-->
<!-- <workingDirectory>${webappDir}</workingDirectory>-->
<!-- <executable>${nodeExecutable}</executable>-->
<!-- <arguments>-->
<!-- <argument>${packageManagerScript}</argument>-->
<!-- <argument>run</argument>-->
<!-- <argument>build:mvn</argument>-->
<!-- </arguments>-->
<!-- </configuration>-->
<configuration>
<workingDirectory>${webappDir}</workingDirectory>
<executable>bash</executable>
<arguments>
<argument>-c</argument>
<argument>
<![CDATA[
max_retries=3
count=0
success=false
while [[ $count -lt $max_retries ]]; do
${nodeExecutable} ${packageManagerScript} run build:mvn
if [[ $? -eq 0 ]]; then
success=true
break
fi
count=$((count+1))
echo "Retry $count/$max_retries..."
sleep 2
done
if [[ "$success" == "false" ]]; then
echo "Build failed after $max_retries attempts."
exit 1
fi
]]>
</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# 3. 实践建议与场景总结
建议所有自动化流水线、离线/半离线内网环境都采用本方案。自动重试可极大提升构建容错率,减少因偶发依赖报错导致的人工干预,真正做到无人值守。
笔记
如需自定义重试次数,只需调整 max_retries
即可。脚本内所有输出均直观反映每轮尝试,便于问题排查。
# 参考截图
改造后 exec-maven-plugin 配置结构如下,详细注释可直观定位每个环节: