How do I use Maven through a proxy?

Viewed 305955

I want to share my experience of using maven through a proxy.

You would most likely face exceptions and messages like:

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

or

[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

How to configure Maven to use proxy server?

17 Answers

For details of setting up a proxy for Maven, see the mini guide.

Essentially you need to ensure the proxies section in either the global settings ([maven install]/conf/settings.xml), or user settings (${user.home}/.m2/settings.xml) is configured correctly. It is better to do this in your user settings to avoid storing the password in plain text in a public location.

Maven 2.1 introduced password encryption, but I've not got round to checking if the encryption applies for the proxy settings as well as repository passwords (don't see why it wouldn't though).

For info, there is a commented-out proxy configuration in your settings.xml and instructions on how to modify it.

From the mini-guide, your settings should look something like this:

<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>

Those are caused most likely by 2 issues:

  1. You need to add proxy configuration to your settings.xml. Here's a trick in your username field. Make sure it looks like domain\username. Setting domain there and putting this exact slash is important '\'. You might want to use <![CDATA[]]> tag if your password contains non xml-friendly characters.
  2. I've noticed maven 2.2.0 does not work sometimes through a proxy at all, where 2.2.1 works perfectly fine.

If some of those are omitted - maven could fail with random error messages.

Just hope I've saved somebody from googling around this issue for 6 hours, like I did.

Thanks @krosenvold.

If the settings file changes don't work, try this in the command prompt having the POM file.

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

This has helped me immediately after a password change.

I run cntlm localy, configured with NTLMv2 password hashes to authenticate with the corporate proxy, and use

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

to use that proxy from maven. Of course the proxy you use should support cntlm/NTLMv2.

I know this is not really an answer to the question, but it might be worth knowing for someone searching this post. It is also possible to install a Maven repository proxy like nexus.

Your maven would be configured to contact the local Nexus proxy, and Nexus would then retrieve (and cache) the artifacts. It can be configured through a web interface and has support for (http) proxies).

This can be an advantage, especially in a company setting, as artefacts are locally available and can be downloaded fast, and you are not that dependent on the availability of external Maven repositories anymore.

To link back to the question; with Nexus there is a nice GUI for the proxy configuration, and it needs to be done on one place only, and not for every developer.

if you are new to proxy setup for Maven In my case first go and check your Home Folder weather there is .m2 folder and in it there should be a file named settings.xml if not create it , and paste this and change host and port,then if needed change the nonProxyHosts

Home Folder - C:\Users\ {UserName}

<settings>
<proxies>
    <proxy>
        <id>httpproxy</id>
        <active>true</active>
        <protocol>http</protocol>
        <host>your-proxy-host</host>
        <port>your-proxy-port</port>
        <nonProxyHosts>localhost</nonProxyHosts>
    </proxy>
<proxy>
        <id>httpsproxy</id>
        <active>true</active>
        <protocol>https</protocol>
        <host>your-proxy-host</host>
        <port>your-proxy-port</port>
        <nonProxyHosts>localhost</nonProxyHosts>
    </proxy>

</proxies>
</settings>

If any case this does not success go and do the changes in this location of Home Folder

/conf/settings.xml

I am using Eclipse as my IDE
Hope this will help !!

Note: To remove the proxy just move settings.xml to some where else

Maven provides a built-in method of doing this, via a file called settings.xml, and this has been covered in other answers. However, it is customary, particularly in Linux, for command-line tools to automatically use the proxy specified by the environment variable https_proxy.

To follow the Don't repeat yourself principle (which is intended to help you avoid mistakes), it would be nice if mvn could automatically work with that too.

Here's a shell script that makes the necessary conversions:

#! /usr/bin/env bash

function javaproxy {
    ## using "Shell Parameter Expansion"
    request_scheme=$1 ; proxy=$2
    notscheme=$(echo ${proxy#*://}) ## parse
    scheme=$(echo ${proxy%${notscheme}}) ## remove
    scheme=$(echo ${scheme%://}) ## strip
    hostport=$(echo ${proxy#*//*}) ## parse
    host=$(echo ${hostport%:*}) ## parse
    port=$(echo ${hostport#${host}}) ## remove
    port=$(echo ${port#:}) ## strip
    scheme=$(echo ${scheme:-http}) ## default
    host=$(echo ${host:-localhost}) ## default
    port=$(echo ${port:-8080}) ## default
    echo -n " -D${request_scheme}.proxyHost=${host}"
    echo -n " -D${request_scheme}.proxyPort=${port}"
}

JTO=""

if [ $http_proxy ] ; then
    JTO="${JTO}$(javaproxy http ${http_proxy})"
fi

if [ $https_proxy ] ; then
    JTO="${JTO}$(javaproxy https ${https_proxy})"
fi

if [ $no_proxy ] ; then
    JTO="${JTO} -Dhttp.nonProxyHosts=$(echo \"${no_proxy}\"|tr ',' '|')"
fi

   export JAVA_TOOL_OPTIONS=${JTO}
   echo "JAVA_TOOL_OPTIONS=${JAVA_TOOL_OPTIONS}"

   mvn_friendliness_options+=--update-snapshots
   mvn ${mvn_friendliness_options} $@

You might name this something like proxied_mvn and run it as:

$ https_proxy=http://localhost:58080 ./proxied_mvn clean package

Alternatively, you could just move the environment setup into your startup scripts.

gotchas

There are many things that can go wrong when trying to configure Maven for access to a Nexus through a proxy. Hopefully, this script will help with some of the most finicky issues, but others remain:

  • Nexus credentials available and correct (only if required)

    Check with mvn help:effective-settings

  • Maven caching: "resolution will not be reattempted"

    mvn clean package --update-snapshots

  • Maven wall-of-text output -- you have to look closely at the output to make sure errors messages aren't subtly different between runs

  • Older versions of Java may require _JAVA_OPTIONS instead of JAVA_TOOL_OPTIONS.

epilogue

There is more than one kind of Proxy. Correspondingly, there is more than one way that this question has been interpreted -- contributing to the large number of disparate answers here.

I have explicitly addressed the case of a (forward HTTP/HTTPS) web proxy server, which is used to access the internet from within a company network (for some companies). This may be notably distinct from a SOCKS proxy, which has also been addressed in some answers here.

Oh by the way, since it uses JAVA_TOOL_OPTIONS, this solutions can be applied to running your other Java applications inside a proxy too.


Parting hint... My example above uses http://localhost:58080. This is because I've set up port-forwarding from my CLIENT_PROXY=localhost:58080 to the actual network proxy by using WSL on my remote-access client to run:

ssh $PROXY_CLIENT -R $CLIENT_PROXY:$SERVER_PROXY

Some times you need to add other <proxy></proxy> tags, and specify the https in the protocol tags: <protocol>https</protocol>

Related