Maven 未在 pom.xml 中为远程存储库使用代理

Maven not using proxy in pom.xml for remote repositories

我正在使用 Maven 构建我的代码并在我的项目中 pom.xml,我已将远程存储库配置为

 <repositories>
   <repository>
   <releases>
   <enabled>true</enabled>
   <updatePolicy>always</updatePolicy>
   <checksumPolicy>warn</checksumPolicy>
   </releases>
   <snapshots>
   <enabled>false</enabled>
   <updatePolicy>never</updatePolicy>
   <checksumPolicy>fail</checksumPolicy>
   </snapshots>
   <id>HDPReleases</id>
   <name>HDP Releases</name>
   <url>http://repo.hortonworks.com/content/repositories/releases/</url>
   <layout>default</layout>
   </repository>
   </repositories>

并且在 Maven 中 setting.xml 我在代理标记下添加了详细信息,以便所有 HTTPS 请求在下载时都将使用代理。然后 Maven 无法从远程 hortonworks 存储库获取所需的文件,并出现类似

的错误
[ERROR] Failed to execute goal on project flume-sources: Could not resolve dependencies for project com.hdptest:flume-sources:jar:1.0-SNAPSHOT: Failed to collect dependencies at org.apache.flume:flume-ng-core:jar:1.4.0.2.0.10.1-3: Failed to read artifact descriptor for org.apache.flume:flume-ng-core:jar:1.4.0.2.0.10.1-3: Could not transfer artifact org.apache.flume:flume-ng-core:pom:1.4.0.2.0.10.1-3 from/to hortonworks (http://repo.hortonworks.com/content/repositories/releases/): Connect to repo.hortonworks.com:80 [repo.hortonworks.com/54.225.131.199] failed: Connection timed out -> [Help 1]

我可以理解这里有什么问题。就是每当 Maven 尝试从 repo.hortonworks.com 下载时,它不会使用我在 settings.xml 中提到的代理详细信息,因此该请求不起作用。但是当它向 Maven

传递相同的请求时

通过查看来自 Maven 构建的调试消息,这一点很清楚,如下所示。从 https://repo.maven.apache.org 下载任何内容时,它使用正确的代理详细信息。但是从hortonworks的repository下载东西时,使用proxy是失败的。:

[DEBUG] =======================================================================
[DEBUG] Using transporter WagonTransporter with priority -1.0 for http://repo.hortonworks.com/content/repositories/releases/
[DEBUG] Using connector BasicRepositoryConnector with priority 0.0 for http://repo.hortonworks.com/content/repositories/releases/
Downloading: http://repo.hortonworks.com/content/repositories/releases/org/apache/flume/flume-ng-core/1.4.0.2.0.10.1-3/flume-ng-core-1.4.0.2.0.10.1-3.pom
[DEBUG] Writing tracking file /root/.m2/repository/org/apache/flume/flume-ng-core/1.4.0.2.0.10.1-3/flume-ng-core-1.4.0.2.0.10.1-3.pom.lastUpdated
[DEBUG] Using transporter WagonTransporter with priority -1.0 for https://repo.maven.apache.org/maven2
[DEBUG] Using connector BasicRepositoryConnector with priority 0.0 for https://repo.maven.apache.org/maven2 via xyzproxy.test.co.in:8080 with username=proxy_user_name, password=***
Downloading: https://repo.maven.apache.org/maven2/org/apache/flume/flume-ng-core/1.4.0.2.0.10.1-3/flume-ng-core-1.4.0.2.0.10.1-3.pom
[DEBUG] Writing tracking file /root/.m2/repository/org/apache/flume/flume-ng-core/1.4.0.2.0.10.1-3/flume-ng-core-1.4.0.2.0.10.1-3.pom.lastUpdated

那么确保 Maven 将对每个 HTTP 请求使用给定代理的解决方案是什么?该 HTTP 请求可以发送到项目 pom.xml 内设置的任何存储库。此处 HTTP URL 可从外部访问,因此存在 pom 文件。

我尝试在 setting.xml 中为我的同一个 hortonworks 存储库添加一个镜像,这样 Maven 将在查看镜像站点时使用代理详细信息。但这也行不通。

  <mirror>
      <id>hortonworks</id>
      <mirrorOf>HDPReleases</mirrorOf>
      <name>using Mirror for hortonworks.</name>
      <url>http://repo.hortonworks.com/content/repositories/releases/</url>
    </mirror>

请注意,我的代理设置是正确的,Maven 正在使用它们从 https://repo.maven.apache.org 下载其他文件。如果我从设置文件中删除代理,我会收到明确的错误消息,即 Maven 无法连接到 repo。这意味着我使用的是正确的 settings.xml 文件,我的代理设置不正确不是问题。

唯一的问题是 Maven 没有为远程存储库的 https 请求使用代理 URL。

我可以解决这个问题。我知道这是我公司代理的问题,所以最初我在 Maven settings.xml 中添加了所有需要的代理详细信息以及用户名和密码。然而,当我们在 pom.xml 中定义任何存储库时,该设置未被使用。

所以对我有用的解决方案是在我的 VM 上设置 CNTLM,然后将 settings.xml 内的代理指向 localhost:3128。之后,来自该虚拟机的任何 https 请求都会发送到 localhost:3128,我的问题就解决了。

maven中的条目settings.xml会像这样

<proxies>
   <proxy>
      <active>true</active>
      <protocol>http</protocol>
      <host>localhost</host>
      <port>3128</port>
      <nonProxyHosts>127.0.0.1</nonProxyHosts>
   </proxy>
</proxies>