首页 > 解决方案 > 无法为 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

为什么端口设置不像其他代理设置那样使用环境变量?

如何使用环境变量进行代理端口设置?

标签: linuxmavenmaven-3

解决方案


这是一个 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>

推荐阅读