170

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?

2
  • (Anyone) Mind rephrasing this into an actual question (like: How do I use Maven through a proxy?)? You could then post your question body as an answer, to be more inline with the StackOverflow format..
    – Tim
    Aug 9, 2009 at 11:30
  • I've rephrased it for you Bogdan, please post your original content as an answer if you wish Aug 9, 2009 at 11:44

18 Answers 18

181

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>
4
  • 8
    cant find setting.xml file in the m2 directory. any idea why? @Rich Seller
    – Ariox66
    Jul 18, 2015 at 16:18
  • 5
    You have to place it there. You can find a sample settings.xml from maven_home, the location where you unzipped maven binaries.
    – smellerbee
    Oct 21, 2016 at 19:42
  • 2
    And if the proxy needs no authentication, the <username> and <password> elements must be removed (and not given empty contents). This took me a while to figure out.
    – dmcontador
    Aug 28, 2019 at 5:53
  • If you have only configured the proxy settings for HTTP in your settings.xml file, then it will not be automatically applied to HTTPS connections.
    – E Epsylon
    Feb 22, 2023 at 10:59
63

How to use a socks proxy?

Set up a SSH tunnel to a server somewhere:

ssh -D $PORT $USER@$SERVER

Linux (bash):

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

Windows:

set MAVEN_OPTS="-DsocksProxyHost=127.0.0.1 -DsocksProxyPort=$PORT"
4
  • 2
    Using a socks proxy through MAVEN_OPTS is the only way I can get Maven to download over HTTPS. Using HTTP or HTTPS proxies, either through MAVEN_OPTS or settings.xml, results in org.apache.maven.wagon.providers.http.httpclient.impl.execchain.RetryExec execute INFO: I/O exception (java.net.SocketException) caught when processing request to {tls}->http://proxy.mycompany.com:911->https://repo.maven.apache.org:443: SOCKS: TTL expired Dec 8, 2016 at 3:07
  • 3
    For http proxy: export MAVEN_OPTS="-DproxyHost=127.0.0.1 -DproxyPort=$PORT"
    – Omid
    Aug 7, 2019 at 5:45
  • 1
    Excellent, thanks. If you want to use those options just once for one single command, you can type : MAVEN_OPTS="-DsocksProxyHost=127.0.0.1 -DsocksProxyPort=1080" mvn clean install The next mvn commands in the same session will be without any proxy. May 14, 2020 at 10:32
  • 2
    How do you pass username and password to that? Mar 10, 2021 at 9:31
37

I also had this problem, and I solved it by editing the settings.xml file in my .m2 folder. My settings.xml is like this now:

<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>
1
  • 3
    This is not downloading jar files in my case. I have tried everything Jun 20, 2018 at 13:25
35

Also note that some plugins (remote-resources comes to mind) use a really old library that only accepts proxy configuration through MAVEN_OPTS;

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

You might be stuck on auth for this one.

5
  • Did not work for me on Ubuntu 14.04 with maven 3.0.5 from the Ubuntu repository. Configuring the proxy in the settings.xml file worked. Sep 10, 2015 at 22:58
  • 1
    Worked for me on Ubuntu 14.04 with Maven 3.0.4
    – rmv
    Mar 30, 2016 at 14:36
  • An example of needing to set MAVEN_OPTS in this way is building Maven from source using Maven. Without MAVEN_OPTS, it fails to download LICENSE-2.0, despite downloading many JARs.
    – Permaquid
    Mar 31, 2017 at 19:42
  • @krosenvold thank you, this solution worked for me. I had already set the proxy in settings.xml but there was a cxf plugin that wanted to generate the code from wsdl which was failing. This fixed my problem. Mar 28, 2019 at 8:17
  • 3
    How do you pass username and password using the above command? Mar 10, 2021 at 9:32
29

To set Maven Proxy :

Edit the proxies session in your ~/.m2/settings.xml file. If you cant find the file, create one.

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

or

Edit the proxies session in your {M2_HOME}/conf/settings.xml

1
  • how can you use the proxy for Profiles? and is there a way to use these settings in a pom.xml later on?
    – madduci
    May 20, 2022 at 9:35
18

Just to add my own experiences with this: my company's proxy is http://webproxy.intra.companyname.com:3128. For maven to work via this proxy, the settings have to be exactly like this

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

Unlike some other proxy-configuration files, the protocol here describes how to connect to the proxy server, not which kinds of protocol should be proxied. The http part of the target has to be split off from the hostname, else it won't work.

3
  • At this time I just experimented the opposite. The protocol here is to describe which protocol should use this proxy. I just achieved to configure a https protocol proxy that connect to a http proxy by using <protocol>https</protocol> Feb 9, 2016 at 15:40
  • Thank you! This worked for me! I had "http://" pre-pended and it would fail Jan 12, 2018 at 21:02
  • + 1 for the "The http part of the target has to be split off from the hostname, else it won't work." part. It was driving me crazy and this solved it.
    – Parto
    Feb 5, 2021 at 6:11
18

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.

2
  • 3
    I've seen that answer everywhere but how do you pass user/password using that method?
    – localhost
    Jan 11, 2023 at 1:14
  • This should have been the correct answer. Thanks! it helped solving my issue
    – gjjhhh_
    Oct 18, 2023 at 22:38
17

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.

2
  • Are you using an NTLM proxy? from the mini guide: "Please note that [c]urrently NTLM proxies are not supported as they have not been tested. You may be able to use the relevant system properties on JDK 1.4+ to make this work." Aug 9, 2009 at 11:46
  • Not sure about this. That is a corporate proxy that's actually placed 600 miles from me and nobody from system administration is in office this weekend. So can't ask anybody now. It will be interesting to find out. I know it supports http, socks and other protocols. Maybe it's NTML as well.
    – Bogdan
    Aug 9, 2009 at 11:55
15

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.

2
  • ` set MAVEN_OPTS=-DproxyHost=127.0.0.1 -DproxyPort=3128 ` for windows
    – qxo
    Dec 25, 2019 at 9:32
  • PowerShell can set a system environment variable: [Environment]::SetEnvironmentVariable("MAVEN_OPTS", "-DproxyHost=127.0.0.1 -DproxyPort=3128", "Machine")
    – Boggin
    Mar 9, 2020 at 16:01
6

And to add to this topic, here're my experiences below... Really odd and time consuming so I thought it was worth adding.

I've had a similar problem trying to built the portlet-bridge on Windows, getting the following errors:

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

I tried a couple of things, following a bit of surfing:

  • Tried to set the parent.relativePath as empty so that maven didn't think the parent was local. This is as per the suggestion on SO at Hudson build fail: Non-resolvable parent POM and in this nabble forum. This had no effect.

  • I also tried ensuring the repository was explicitly listed in my settings.xml but this had no effect either.

  • I then ensured mvn was forced to lookup the repository, rather than rely on it's own history, as discussed in this blog by Sarthon. Unfortunately, this wasn't the issue either.

  • In some desperation, I then revisited my MAVEN_OPTS to ensure I wasn't falling foul of my proxy settings. These were correct, albeit with the value unquoted:

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

  • So, finally, I moved the proxy config into my settings.xml and this worked:

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

Really not sure why my original MAVEN_OPTS wasn't working (quotes?) while the settings.xml config did work. I'd like to reverse the fix and check each step again but have wasted too much time. Will report back as and when.

2
  • Did you actually specify a username and password in the MAVEN_OPTS as well?
    – bcmoney
    Dec 4, 2012 at 17:48
  • 1
    I don't think I did but then I don't need to for my proxy server. As you can see though, it is possible. Apart from the security issues, you could give it a try. I gather you're still having problems having got this far then? Dec 5, 2012 at 9:47
6

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

1
  • To remove the proxy just move settings.xml to some where else
    – Akitha_MJ
    Apr 2, 2020 at 7:41
5

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.

1

If maven works through proxy but not some of the plugins it is invoking, try setting JAVA_TOOL_OPTIONS as well with -Dhttp*.proxy* settings.

If you have already JAVA_OPTS just do

export JAVA_TOOL_OPTIONS=$JAVA_OPTS
0

Except for techniques mentioned above, with some effort, you can run maven through proxy using jproxyloader library (there is example on page how to do this: http://jproxyloader.sourceforge.net/). This allows set up socks proxy only for downloading artifacts.

In solution mentioned by duanni (setting -DsocksProxyHost) there is one problem. If you have integration tests running against local database (or another tests connecting to url which should not go via proxy). These tests will stop working because connections to database will also be directed to proxy. With help of jProxyLoader you can set up proxy only for nexus host. Additionally if you want you can pass connections to database through another proxy.

0

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

0

The above postings helped in resolving my problem. In addition to the above I had to make the following changes to make it work :

  • Modified Maven's JRE net settings(\jre\lib\net.properties) to use system proxy setting.

    https.proxyHost=proxy DNS
    https.proxyPort=proxy port
    
  • Included proxy server settings in settings.xml. I did not provide username and password settings as to use NTLM authentication.

1
  • Thank you for the pointer to the JDK net.properties. This setup was needed for mvnw.cmd maven-wrapper despite having a working mvn settings.xml proxy configuration. I haven't had a Maven install with a JRE, so I believe you mean the JAVA_HOME JDK configured for your invocation of maven. Setting proxy stuff in net.properties seemed better than in every $project/.mvn/jvm.config. (My dev system uses Cisco Anyconnect proxy, not Internet Options, so I didn't try java.net.useSystemProxies). JDK9+ location is conf\net.properties
    – jla
    Jan 12 at 2:20
0

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

Initially even after adding proxy into IntelliJ GUI in File->settings->http_proxy it did not work.

After adding nexus repository url into ./m2/settings.xml it finally worked.

2
  • 3
    Please clarify is your answer is just a summary of existing answers (then please explain the added value of yours), or just a way to thank existing answer(s) (in such case, please delete)
    – mozway
    Dec 22, 2023 at 7:54
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Dec 30, 2023 at 6:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.