无法修改settings.xml时Maven+Jib构建Docker镜像的命令行代理配置方法
问题场景
- 日常本地环境可正常通过
mvn compile jib:dockerBuild -Djib.to.image="$IMAGE_NAME"命令,使用Maven结合Jib构建Docker镜像,构建过程可正常拉取公网依赖资源。 - 将同套构建流程迁移到AWS CodeBuild流水线(运行在需配置代理的企业内网环境)后,命令执行失败。
已尝试的无效操作
- 配置全局Maven运行参数:
export MAVEN_OPTS="-DsocksProxyHost=<host> -DsocksProxyPort=<port>",配置未生效。 - 受权限限制,无法访问/修改Maven全局
settings.xml文件,仅能通过构建命令行直接传入参数调整配置。 - 曾尝试在构建命令中拼接HTTP代理参数,执行命令为:
cd backend && mvn compile jib:dockerBuild -Dhttp.proxyHost=http://path/to/proxy -Dhttp.proxyPort=8080 -Djib.to.image="$JAVA_SERVICE_IMAGE_NAME" && cd ..
原始执行报错日志
[INFO] Scanning for projects... Downloading from central: https://repo.maven.apache.org/maven2/kr/motd/maven/os-maven-plugin/1.6.2/os-maven-plugin-1.6.2.pom [ERROR] [ERROR] Some problems were encountered while processing the POMs: [ERROR] Unresolveable build extension: Plugin kr.motd.maven:os-maven-plugin:1.6.2 or one of its dependencies could not be resolved: Failed to read artifact descriptor for kr.motd.maven:os-maven-plugin:jar:1.6.2 @ @ [ERROR] The build could not read 1 project -> [Help 1] [ERROR] [ERROR] The project com.bla:backend:1.0-SNAPSHOT (/codebuild/output/src794035415/src/backend/pom.xml) has 1 error [ERROR] Unresolveable build extension: Plugin kr.motd.maven:os-maven-plugin:1.6.2 or one of its dependencies could not be resolved: Failed to read artifact descriptor for kr.motd.maven:os-maven-plugin:jar:1.6.2: Could not transfer artifact kr.motd.maven:os-maven-plugin:pom:1.6.2 from/to central (https://repo.maven.apache.org/maven2): Transfer failed for https://repo.maven.apache.org/maven2/kr/motd/maven/os-maven-plugin/1.6.2/os-maven-plugin-1.6.2.pom: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target -> [Help 2] [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. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/ProjectBuildingException [ERROR] [Help 2] http://cwiki.apache.org/confluence/display/MAVEN/PluginManagerException [Container] 2021/02/26 11:15:48 Command did not exit successfully cd backend && mvn compile jib:dockerBuild -Dhttp.proxyHost=http://path/to/proxy -Dhttp.proxyPort=8080 -Djib.to.image="$JAVA_SERVICE_IMAGE_NAME" && cd .. exit status 1 [Container] 2021/02/26 11:15:48 Phase complete: BUILD State: FAILED_WITH_ABORT [Container] 2021/02/26 11:15:48 Phase context status code: COMMAND_EXECUTION_ERROR Message: Error while executing command: cd backend && mvn compile jib:dockerBuild -Dhttp.proxyHost=http://path/to/proxy -Dhttp.proxyPort=8080 -Djib.to.image="$JAVA_SERVICE_IMAGE_NAME" && cd ... Reason: exit status 1
报错根因
- 代理配置不兼容Maven运行机制:之前配置的SOCKS代理参数不生效,是因为Maven内置的HTTP传输组件对SOCKS代理的支持存在兼容问题;后续尝试拼接的HTTP代理参数也存在格式错误——传入的
http.proxyHost值错误携带http://协议前缀,JVM识别代理主机时会自动处理协议,带前缀会导致地址解析失败;同时Maven中央仓库为HTTPS服务,仅配置HTTP代理参数无法覆盖HTTPS流量,必须单独配置对应的HTTPS代理参数。 - 证书校验不兼容企业网络:企业代理通常会对HTTPS流量做中间人解密,替换为自签证书,JVM默认信任库未导入对应根证书,因此触发PKIX证书路径构建失败的报错。
适配权限限制的解决方案
所有配置均可通过命令行参数传入,无需修改settings.xml文件,执行步骤如下:
- 先通过环境变量注入全局JVM参数,覆盖代理与证书校验规则:
export JAVA_TOOL_OPTIONS="-Dhttp.proxyHost=<代理主机IP/域名,不带http前缀> -Dhttp.proxyPort=<代理端口> -Dhttps.proxyHost=<代理主机IP/域名> -Dhttps.proxyPort=<代理端口> -Dmaven.wagon.http.ssl.insecure=true -Dmaven.wagon.http.ssl.allowall=true -Dmaven.wagon.http.ssl.ignore.validity.dates=true" - 执行原有构建命令即可:
cd backend && mvn compile jib:dockerBuild -Djib.to.image="$JAVA_SERVICE_IMAGE_NAME" && cd ..
内容的提问来源于stack exchange,提问作者SlimenTN
相关产品推荐
相关产品推荐

