首页 > 解决方案 > 在 pom.xml 中为 Maven 设置代理(不在 settings.xml 中)

问题描述

我需要为 Maven 设置一个 HTTP 代理来访问存储库。但是,由于给定的环境,我无法在 Maven settings.xml 文件中设置代理。

所以我想知道是否可以在项目 pom.xml 文件(或项目或 mvn 命令中的任何其他位置)中设置代理。

这可能吗?如果可以,怎么做?谢谢!

标签: mavenproxyrepositorypom.xml

解决方案


好,我知道了:

可以使用自定义 settings.xml调用 Maven :mvn -s settings.xml ...并拥有这样一个自定义 settings.xml(例如 pom.xml 旁边),其中包含以下代理内容:

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
  <proxies>
   <proxy>
      <id>proxy_http</id>
      <active>true</active>
      <protocol>http</protocol>
      <host>proxy.example.com</host>
      <port>8080</port>
      <nonProxyHosts>example.com</nonProxyHosts>
    </proxy>
   <proxy>
      <id>proxy_https</id>
      <active>true</active>
      <protocol>https</protocol>
      <host>proxy.example.com</host>
      <port>8080</port>
      <nonProxyHosts>example.com</nonProxyHosts>
    </proxy>
  </proxies>
</settings>

推荐阅读