linux - 无法为 Maven 代理端口使用环境变量
问题描述
我可以使用settings.xml配置代理以与 Maven 一起成功工作。
我决定将配置更进一步,并为每个代理的参数使用环境变量,如下所示:
<proxies>
<proxy>
<active>true</active>
<protocol>https</protocol>
<host>${env.PROXY_HOST}</host>
<port>${env.PROXY_PORT}</port>
<username>${env.PROXY_USER}</username>
<password>${env.PROXY_PASSWORD}</password>
</proxy>
</proxies>
这种配置——特别是使用端口的环境变量——导致我的 Maven 执行中断。首先它通过了这个警告:
[WARNING]
[WARNING] Some problems were encountered while building the effective settings
[WARNING] Unable to parse element 'port', must be an integer (position: END_TAG seen ...<port>${env.PROXY_PORT}</port>... @29:40) caused by: java.lang.NumberFormatException: For input string: "${env.PROXY_PORT}" @conf/settings.xml, line 29, column 40
[WARNING]
并且由于端口不正确,执行很快就会失败:
org.eclipse.aether.transfer.MetadataTransferException:
Connect to localhost:0 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused (Connection refused)
Maven 能够在代理设置中解析主机、用户和密码的环境变量,但不能解析端口。
这是一个如何在 bash 脚本中设置环境变量的示例,然后是 Maven 命令
PROXY_PORT=8888
export PROXY_PORT
我也试过:
PROXY_PORT="8888"
export PROXY_PORT
为什么端口设置不像其他代理设置那样使用环境变量?
如何使用环境变量进行代理端口设置?
解决方案
这是一个 Maven 报告的错误:https ://issues.apache.org/jira/browse/MNG-6401 (或这里:https ://github.com/apache/maven/pull/163#issuecomment-390888715 )
替换为主机完成:
<proxies>
<proxy>
<active>true</active>
<protocol>https</protocol>
<host>${PROXY_HOST}</host>
<port>${PROXY_PORT}</port>
<username>${PROXY_USER}</username>
<password>${PROXY_PASSWORD}</password>
</proxy>
</proxies>
像这样替换:
$ PROXY_PORT=100 PROXY_HOST=http://thehost PROXY_USER=TheUser PROXY_PASSWORD=ThePassword mvn help:effective-settings | grep -FA8 '<proxies>'
<proxies>
<proxy>
<protocol>https</protocol>
<username>TheUser</username>
<password>***</password>
<port>0</port>
<host>http://thehost</host>
</proxy>
</proxies>
推荐阅读
- angular - 存储使用 renderer2 创建的 nativeElement 的引用
- reactjs - 必须使用 Typescript 和 Material UI 为布尔属性设置值
- haskell - 在二叉搜索树中插入(仅存储在叶节点的数据)
- vue.js - 在 axios 承诺失败的情况下,如何有效地编写回退以从本地存储中获取对象?
- sql - SQL Server 查询选择具有其他相关行的随机行
- python - 如何用字典替换熊猫数据框中的值?
- swift - Swift:依赖注入,工厂模式只创建一次实例
- vba - 需要修复这个错误:'Can't find DLL entry point GetSystemInfo in kernel32'
- php - codeigniter - 对 csrf_exclude_uris 中的方法使用正则表达式
- c++ - 使用推力比我自己的内核慢?