首页 文章

如何通过代理使用Maven?

提问于
浏览
96

我想分享我通过代理使用maven的经验 .

您很可能会遇到异常和消息,例如:

repository metadata for: 'org.apache.maven.plugins' could not be retrieved from 
repository: central due to an error: Error transferring file: Connection refused: connect

要么

[WARNING] Failed to retrieve plugin descriptor for org.apache.maven.plugins:maven-clean-
plugin:2.5: Plugin org.apache.maven.plugins:maven-clean-plugin:2.5 or one of its 
dependencies could not be resolved: Failed to read artifact descriptor for 
org.apache.maven.plugins:maven-clean-plugin:jar:2.5

如何配置Maven使用代理服务器?

15 回答

  • 0

    如果maven通过代理工作但没有调用它的某些插件,请尝试使用 -Dhttp*.proxy* 设置设置 JAVA_TOOL_OPTIONS .

    如果你已经 JAVA_OPTS 就做了

    export JAVA_TOOL_OPTIONS=$JAVA_OPTS
    
  • 6

    有关为Maven设置代理的详细信息,请参阅mini guide .

    基本上,您需要确保正确配置全局设置( [maven install]/conf/settings.xml )或用户设置( ${user.home}/.m2/settings.xml )中的代理部分 . 最好在用户设置中执行此操作,以避免将密码以纯文本格式存储在公共位置 .

    Maven 2.1引入了password encryption,但我知道它为什么不会这样做 .

    有关信息,settings.xml中有一个注释掉的代理配置以及如何修改它的说明 .

    从迷你指南中,您的设置应如下所示:

    <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>
          <active>true</active>
          <protocol>http</protocol>
          <host>proxy.somewhere.com</host>
          <port>8080</port>
          <username>proxyuser</username>
          <password>somepassword</password>
          <nonProxyHosts>www.google.com|*.somewhere.com</nonProxyHosts>
        </proxy>
      </proxies>
    [...]
    </settings>
    
  • 4

    How to use a socks proxy?

    在某处设置到服务器的SSH隧道:

    ssh -D $PORT $USER@$SERVER
    

    Linux(bash):

    export MAVEN_OPTS="-DsocksProxyHost=127.0.0.1 -DsocksProxyPort=$PORT"
    

    视窗:

    set MAVEN_OPTS="-DsocksProxyHost=127.0.0.1 -DsocksProxyPort=$PORT"
    
  • 1

    我也有这个问题,我通过编辑.m2文件夹中的settings.xml文件解决了这个问题 . 我的settings.xml现在是这样的:

    <settings>
      <proxies>
        <proxy>
          <id>genproxy</id>
          <active>true</active>
          <protocol>http</protocol>
          <host>proxyHost</host>
          <port>3128</port>
          <username>username</username>
          <password>password</password>
        </proxy>
     </proxies>
    </settings>
    
  • 12

    另请注意,一些插件(远程资源可以想到)使用一个非常老的库,它只接受通过MAVEN_OPTS的代理配置;

    -Dhttp.proxyHost=<host> -Dhttp.proxyPort=<port> -Dhttps.proxyHost=<host> -Dhttps.proxyPort=<port>
    

    你可能会被困在auth上 .

  • 119

    这些是由两个问题造成的:

    • 您需要将代理配置添加到settings.xml . 这里's a trick in your username field. Make sure it looks like domain\username. Setting domain there and putting this exact slash is important ' ' . 如果您的密码包含非xml友好字符,则可能需要使用<![CDATA []]>标记 .

    • 我注意到maven 2.2.0有时通过代理无法工作,其中2.2.1完全正常 .

    如果省略其中一些 - maven可能会失败并显示随机错误消息 .

    只是希望我已经拯救了一些人来搜索这个问题6个小时,就像我一样 .

  • 19

    只是为了添加我自己的经验:我公司的代理是 http://webproxy.intra.companyname.com:3128 . 要使maven通过此代理工作,设置必须与此完全相同

    <settings>
      <proxies>
        <proxy>
          <id>default</id>
          <active>true</active>
          <protocol>http</protocol>
          <host>webproxy.intra.companyname.com</host>
          <port>3128</port>
        </proxy>
      </proxies>
    </settings>
    

    与其他一些代理配置文件不同, protocol 这里描述了如何连接代理服务器,而不是代理哪种协议 . 目标的 http 部分必须从主机名中分离出来,否则它将无效 .

  • 0

    要设置 Maven Proxy

    编辑 ~/.m2/settings.xml file 中的代理会话 . 如果找不到该文件,请创建一个 .

    <settings>
    <proxies>
        <proxy>
            <id>httpproxy</id>
            <active>true</active>
            <protocol>http</protocol>
            <host>your-proxy-host</host>
            <port>your-proxy-port</port>
            <nonProxyHosts>local.net|some.host.com</nonProxyHosts>
        </proxy>
    <proxy>
            <id>httpsproxy</id>
            <active>true</active>
            <protocol>https</protocol>
            <host>your-proxy-host</host>
            <port>your-proxy-port</port>
            <nonProxyHosts>local.net|some.host.com</nonProxyHosts>
        </proxy>
    
    </proxies>
    </settings>
    

    要么

    编辑 /conf/settings.xml 中的代理会话

    希望能帮助到你.. :)

  • 14

    谢谢@krosenvold .

    如果设置文件更改不起作用,请在具有POM文件的命令提示符中尝试此操作 .

    mvn install -Dhttp.proxyHost=abcproxy -Dhttp.proxyPort=8080 -Dhttps.proxyHost=abcproxy -Dhttps.proxyPort=8080
    

    密码更改后,这对我有所帮助 .

  • 4

    并添加到这个主题,这是我的经验下面...真奇怪和耗时,所以我认为值得添加 .

    我在尝试在Windows上构建portlet-bridge时遇到了类似的问题,导致以下错误:

    Downloading: http://repo1.maven.org/maven2/org/apache/portals/bridges-pom/1.0/bridges-pom-1.0.pom
    [DEBUG] Reading resolution tracking file C:\Documents and Settings\myuser\.m2\repository\org\apache\portals\bridges-pom\1.0\bridges-pom-1.0.pom.lastUpdated
    [DEBUG] Writing resolution tracking file C:\Documents and Settings\myuser\.m2\repository\org\apache\portals\bridges-pom\1.0\bridges-pom-1.0.pom.lastUpdated
    [ERROR] The build could not read 1 project -> [Help 1]
    org.apache.maven.project.ProjectBuildingException: Some problems were encountered while processing the POMs:
    [FATAL] Non-resolvable parent POM: Could not transfer artifact
    org.apache.portals:bridges-pom:pom:1.0 from/to central (http://repo1.maven.org/maven2): Error transferring file: repo1.maven.org and 'parent.relativePath' points at wrong local
    POM @ line 23, column 11
    ...
    [ERROR]   The project org.apache.portals.bridges:portals-bridges-common:2.0 (H:\path_to_project\portals-bridges-common-2.0\pom.xml) has 1 error
    [ERROR]     Non-resolvable parent POM: Could not transfer artifact org.apache.portals:bridges-pom:pom:1.0 from/to central (http://repo1.maven.org/maven2):
    Error transferring file: repo1.maven.org and 'parent.relativePath' points at wrong local POM @ line 23, column 11: Unknown host repo1.maven.org -> [Help 2]
    ...
    [ERROR] For more information about the errors and possible solutions, please read the following articles:
    [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/ProjectBuildingException
    [ERROR] [Help 2] http://cwiki.apache.org/confluence/display/MAVEN/UnresolvableModelException
    

    我尝试了一些事情,经过一些冲浪:

    • 尝试将parent.relativePath设置为空,以便maven认为父级不是本地的 . 这是根据SO的建议Hudson build fail: Non-resolvable parent POMin this nabble forum . 这没有效果 .

    • 我也试过确保我的settings.xml中明确列出了存储库,但这也没有效果 .

    • 然后我确保mvn被迫查找存储库,而不是依赖于它自己的历史,如this blog by Sarthon中所述 . 不幸的是,这也不是问题 .

    • 在一些绝望中,我重新访问了我的MAVEN_OPTS以确保我没有违反我的代理设置 . 这些都是正确的,尽管 Value 没有引用:

    设置MAVEN_OPTS = -Dhttp.proxyHost = myproxy.mycompany.com -Dhttp.proxyPort = 8080 -Xmx256m

    • 所以,最后,我将代理配置移动到我的settings.xml中,这有效:
    <proxies>
          <proxy>
            <id>genproxy</id>
            <active>true</active>
            <protocol>http</protocol>
            <!--username>proxyuser</username-->
            <!--password>proxypass</password-->
            <host>myproxy.mycompany.com</host>
            <port>8080</port>
            <nonProxyHosts>*.mycompany.com|127.0.0.1</nonProxyHosts>
          </proxy>
        </proxies>
    

    当settings.xml配置确实有效时,真的不确定为什么我的原始MAVEN_OPTS不起作用(引用?) . 我想扭转修复并再次检查每一步,但浪费了太多时间 . 将在何时报告 .

  • 12

    我知道这不是问题的答案,但是对于搜索这篇文章的人来说可能值得一提 . 也可以安装Maven存储库代理,如nexus .

    您的maven将配置为联系本地Nexus代理,然后Nexus将检索(并缓存)工件 . 它可以通过Web界面进行配置,并且支持(http)代理 .

    这可能是一个优势,尤其是在a公司设置,因为人工制品在本地可用并且可以快速下载,并且您不再依赖于外部Maven存储库的可用性 .

    链接回到问题;使用Nexus,代理配置有一个很好的GUI,它只需要在一个地方完成,而不是每个开发人员 .

  • 7

    我运行 cntlm localy,使用 NTLMv2 密码哈希配置以使用公司代理进行身份验证,并使用

    export MAVEN_OPTS="-DproxyHost=127.0.0.1 -DproxyPort=3128"
    

    maven 使用该代理 . 当然,您使用的代理应该支持 cntlm / NTLMv2 .

  • 12

    除了上面提到的技术,通过一些努力,您可以使用jproxyloader库通过代理运行maven(页面上有示例如何执行此操作:http://jproxyloader.sourceforge.net/) . 这允许仅为下载工件设置socks代理 .

    在duanni提到的解决方案中(设置-DsocksProxyHost)存在一个问题 . 如果您有针对本地数据库运行的集成测试(或连接到url的另一个不应通过代理进行的测试) . 这些测试将停止工作,因为与数据库的连接也将定向到代理 . 借助jProxyLoader,您只能为nexus主机设置代理 . 此外,如果您需要,可以通过另一个代理将连接传递给数据库 .

  • 0

    上述帖子有助于解决我的问题 . 除了上述内容之外,我还必须进行以下更改才能使其正常工作:

    • 修改了Maven的JRE网络设置(\ jre \ lib \ net.properties)以使用系统代理设置 . https.proxyHost =代理DNS https.proxyPort =代理端口

    • settings.xml中包含的代理服务器设置 . 我没有提供用户名和密码设置来使用NTLM身份验证 .

  • 36

    有时您需要添加其他 <proxy></proxy> 标记,并在协议标记中指定https: <protocol>https</protocol>

相关问题