You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Maven代理配置引发企业Nexus连接错误,需保留代理适配部分项目

解决Maven代理与企业Nexus访问冲突的问题

这个问题的核心原因很明确:你的代理配置把访问企业内部Nexus的请求也转发到了代理服务器,而企业Nexus出于安全策略拦截了来自代理的请求(返回URLBlocked),但直接内网访问Nexus是允许的。下面是具体的解决步骤:

1. 更新代理的nonProxyHosts配置

把企业Nexus的域名添加到代理的nonProxyHosts列表中,这样Maven访问这些内部地址时会跳过代理,直接走内网连接。

修改你settings.xml中两个代理(http和https)的nonProxyHosts字段:

<nonProxyHosts>127.0.0.1|localhost|maven|company-nexus.com|company-deploy-nexus.com</nonProxyHosts>

这样配置后,Maven访问中央仓库等外部地址时会走代理,访问企业Nexus时直接内网连接,不会触发拦截。

2. 完整的修改后settings.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">
<localRepository>C:\Dev\m2</localRepository>
<proxies>
<proxy>
<id>http-proxy</id> <!-- 建议给代理设置唯一ID,避免重复识别问题 -->
<active>true</active>
<protocol>http</protocol>
<username>user</username>
<password>pass</password>
<host>host</host>
<port>port</port>
<nonProxyHosts>127.0.0.1|localhost|maven|company-nexus.com|company-deploy-nexus.com</nonProxyHosts>
</proxy>
<proxy>
<id>https-proxy</id> <!-- 设置唯一ID -->
<active>true</active>
<protocol>https</protocol>
<username>user</username>
<password>pass</password>
<host>host</host>
<port>port</port>
<nonProxyHosts>127.0.0.1|localhost|maven|company-nexus.com|company-deploy-nexus.com</nonProxyHosts>
</proxy>
</proxies>
<profiles>
<profile>
<id>default</id>
<repositories>
<repository>
<id>nexus1</id>
<url>http://company-nexus.com</url>
</repository>
<repository>
<id>nexus2</id>
<url>http://company-deploy-nexus.com</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus1</id>
<url>http://company-nexus.com</url>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>default</activeProfile>
</activeProfiles>
<pluginGroups>
<pluginGroup>com.atlassian.maven.plugins</pluginGroup>
</pluginGroups>
</settings>

3. 可选优化:配置Nexus为中央仓库镜像

如果你的企业Nexus已经代理了Maven中央仓库,推荐在settings.xml中添加mirrors配置,让所有中央仓库的请求都通过Nexus转发,这样可以进一步减少代理的使用场景,避免后续冲突:

<mirrors>
  <mirror>
    <id>nexus-central</id>
    <mirrorOf>central</mirrorOf>
    <url>http://company-nexus.com/nexus/content/groups/public</url>
  </mirror>
</mirrors>

这样即使项目里配置了maven-central仓库,Maven也会自动把请求转到企业Nexus,不需要通过外部代理访问中央仓库。


内容的提问来源于stack exchange,提问作者tenticon

火山引擎 最新活动