How to enable proxy setting in Maven

There is a high chance your company is set up a firewall and an HTTP proxy server to stop user connects to internet directly. If you are behind a proxy, Maven will fail to download any dependencies.

To make it work, you have to declare the proxy server setting in Maven configuration file settings.xml.

1. Maven configuration file

Find {M2_HOME}/conf/settings.xml, and put your proxies detail inside.

{M2_HOME}/conf/settings.xml

<!-- proxies
   | This is a list of proxies which can be used on this machine to connect to the network.
   | Unless otherwise specified (by system property or command-line switch), the first proxy
   | specification in this list marked as active will be used.
   |-->
  <proxies>
    <!-- proxy
     | Specification for one proxy, to be used in connecting to the network.
     |
    <proxy>
      <id>optional</id>
      <active>true</active>
      <protocol>http</protocol>
      <username>proxyuser</</username>
      <password>proxypass</password>
      <host>proxy.host.net</host>
      <port>80</port>
      <nonProxyHosts>local.net|some.host.com</nonProxyHosts>
    </proxy>
    -->
  </proxies>

Un-comment the proxy options and fill in your proxy server detail.


<!-- proxies
   | This is a list of proxies which can be used on this machine to connect to the network.
   | Unless otherwise specified (by system property or command-line switch), the first proxy
   | specification in this list marked as active will be used.
   |-->
  <proxies>
      <proxy>
      <id>optional</id>
      <active>true</active>
      <protocol>http</protocol>
      <username>mkyong</username>
      <password>password</password>
      <host>proxy.mkyong.com</host>
      <port>8888</port>
      <nonProxyHosts>local.net|some.host.com</nonProxyHosts>
    </proxy>
  </proxies>

2. Saved it.

Done, your Apache Maven should be able to connect to the Internet via your proxy server now.

Note
Restart is NOT required, how do you restart Maven? Maven is just a command, when you call it, it will read this file again.

References

  1. Configuring a proxy

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
Punit
Guest
Punit

Hi Thanks for the post, you always have great tutorials.
just wanted to know, what are the other elements, i mean:

<id>optional</id>
      <active>true</active>
      <protocol>http</protocol>
      <username>proxyuser</username>
      <password>proxypass</password>
      <host>proxy.yourcompany.com</host>
      <port>80</port>
      <nonProxyHosts>local.net|some.host.com</nonProxyHosts>

do i need to change only host, i mean i need to create proxy for “http://repo.jenkins-ci.org/public/org/jenkins-ci”.
so i need to change the host only, rest i need to leave as it is??

AnNik
Guest
AnNik

I`d like to add nonProxy hosts aka 10.0.0.0/8 – what is solution for this case ?

Pawash Ahuja
Guest
Pawash Ahuja
Configured my settings.xml file to incude the proxy. However, when I tried to use mvn archetype:generate, this was the error: Please advise. TIA maven-metadata.xml from/to central (https://repo.maven.apache.org/maven2): aurl003.us.alcatel-lucent.com [WARNING] Failure to transfer org.codehaus.mojo/maven-metadata.xml from https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced. Original error: Could not transfer metadata org.codehaus.mojo/maven-metadata.xml from/to central (https://repo.maven.apache.org/maven2): aurl003.us.alcatel-lucent.com [INFO] ———————————————————————— [INFO] BUILD FAILURE [INFO] ———————————————————————— [INFO] Total time: 40.518 s [INFO] Finished at: 2016-06-09T14:19:31+00:00 [INFO] Final Memory: 13M/102M [INFO] ———————————————————————— [ERROR] No plugin found for prefix ‘archetype’ in the… Read more »
KR
Guest
KR

Thank you, It just works

Iyshwarya
Guest
Iyshwarya

I’m new to maven and I have a query. Is it possible to configure multiple proxies in maven? If so how ?

Aditya C
Guest
Aditya C

Thanks a Lot …It helped me alot..

Elom Atsou Agboka
Guest
Elom Atsou Agboka

Nice job !!!

Manish
Guest
Manish

I don’t see here any format of setting.xml, how this file should be and what content this file may have.

rajmohan
Guest
rajmohan
C:\Git\cas> C:\Git\cas>mvn clean package -P local -Dmaven.test.skip=true [INFO] Scanning for projects… [INFO] [INFO] ———————————————————————— [INFO] Building cas 1.2.2 [INFO] ———————————————————————— Downloading: http://download.java.net/maven/2/org/apache/maven/plugins/maven-clean-plugin/2.4.1/maven-clean-plugin-2.4.1.pom Downloading: http://repository.codehaus.org/org/apache/maven/plugins/maven-clean-plugin/2.4.1/maven-clean-plugin-2.4.1.pom Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-clean-plugin/2.4.1/maven-clean-plugin-2.4.1.pom [INFO] ———————————————————————— [INFO] BUILD FAILURE [INFO] ———————————————————————— [INFO] Total time: 0.827s [INFO] Finished at: Wed Jun 27 16:33:19 IST 2012 [INFO] Final Memory: 2M/5M [INFO] ———————————————————————— [ERROR] Plugin org.apache.maven.plugins:maven-clean-plugin:2.4.1 or one of its dependencies could not be resolved: Failed to read artifact descriptor for org.apache.maven .plugins:maven-clean-plugin:jar:2.4.1: Could not transfer artifact org.apache.maven.plugins:maven-clean-plugin:pom:2.4.1 from/to maven2-repository.dev.java.net (http://do wnload.java.net/maven/2): Not authorized by proxy, ReasonPhrase:Proxy Authentication Required ( The ISA Server requires authorization to fulfill the request. Access to… Read more »
LK
Guest
LK

Does someone know the solusion for this error?

Paddy
Guest
Paddy

As Apostolos mantioned, try using wagon jar to solve this problem. See this link for further details :

http://thiranjith.com/2012/10/31/how-to-configure-maven-to-work-with-ntlm-proxies/

Apostolos
Guest
Apostolos

Try adding the wagon-http-lightweight-2.2.jar on the lib/ext of your maven setup.

Deepak Sabharwal
Guest
Deepak Sabharwal

Hi, its good to use maven for project management. nice work !!!

Bikash
Guest
Bikash

Thanks for tutorial but, I want to know is it ok to
give IP address in instead of name.

jesse
Guest
jesse

i am using automatic configuration script to connect to internet. how do i set in this scenario

M J Kulathinal
Guest
M J Kulathinal

I had the same issue – was trying to build with maven but had to go through an automatic configuration script for a proxy server. I managed to sort the issue by using the setting in the maven settings.xml file and defining the proxy server details which were defined in the configuration script which I managed to get from the network team and it worked … so instead of using the script path, use the actual proxy definitions in the settings file for maven and it will work

glenn
Guest
glenn

it works

Unblock Proxy
Guest
Unblock Proxy

Looking through that information. It’s very usefull for us…

Rohit
Guest
Rohit

Thanks this was very useful

trackback
Maven Tutorials | Tutorials

[…] Enable the proxy in Maven To use proxy server to connect internet, you have to configure the proxy setting in Maven. […]

Klaudia Saric
Guest
Klaudia Saric

Thanks for this post, answers a bunch of questions I was having.

Prasath
Guest
Prasath

Thanks for the article. It looks good.

Please kindly write an article how to make use of maven in the projects.

Thanks

alejogabriel
Guest
alejogabriel

Hi, I’m new to the computer world. I recently installed a linksys router. I connect with wireless to my computer. my question is where I get the information I write on the tag “host”. Use Windows. Currently it is giving me the following error.

Could not resolve archetype org.appfuse.archetypes:appfuse-modular-struts:RELEASE from any of the configured repositories.
Could not resolve artifact org.appfuse.archetypes:appfuse-modular-struts:jar:RELEASE
Missing org.appfuse.archetypes:appfuse-modular-struts:jar:RELEASE

thanks
Alejo

PD: Sorry for my English