Configuring Proxy In Maven (Part-4)

Configuring proxy in maven (to execute maven behind a corporate network)

There will be frequent need to download dependencies (if not already available in local repository) for your project. If you are working in corporate sector, there is a chance that your company has set up a firewall and a HTTP proxy server so that every request you make should go through the proxy. In this case if you specify any dependency in your project’s pom.xml, you may get connection related issue as

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

For this you need to specify proxy server related details in maven configuration file settings.xml. In this article we are going to see how to configure proxy in maven.

There are two locations where proxies’ related configuration can be done.

  1. Global Settings ([maven install]/conf/settings.xml)
  2. User Settings (${user.home}/.m2/settings.xml)

Both of these configurations are explained below.

  1. Global Settings ([maven install]/conf/settings.xml)

This maven configuration file resides in [maven install]/conf/ folder. In my windows machine, it is here

Open this “settings.xml” file and look for <proxies> tag, you will find it something like this.

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

This is where we have to make changes. Uncomment the <proxy> tag and fill in your proxy server details (In case you don’t know the details, contact your network administrator).

 <!-- 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>codeAdda</username>
 <password>password</password>
 <host>proxy.codeadda.com</host>
 <port>8080</port>
 <nonProxyHosts>local.net|some.host.com</nonProxyHosts>
 </proxy>
 </proxies>

Just save the file and thus you have successfully configured to use proxy of your corporate office.

  1. User Settings (${user.home}/.m2/settings.xml)

This setting should be done in the local repository which is specific the current windows user. “settings.xml” file may not be already available first time as in my case here.

Create an xml file with name “settings.xml” parallel to repository folder like the following.

Copy the following proxy related configuration with your proxy server details as following.

</pre>
<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>
<id>optional</id>
<active>true</active>
<protocol>http</protocol>
<username>codeAdda</username>
<password>password</password>
<host>proxy.codeadda.com</host>
<port>8080</port>
<nonProxyHosts>local.net|some.host.com</nonProxyHosts>
</proxy>
</proxies>
</settings>
<pre>

Note: I would recommend to do the user settings to avoid storing the password in plain text in a public location. This configuration is valid both for HTTP and HTTPS protocol.

References:

https://stackoverflow.com/questions/1251192/how-do-i-use-maven-through-a-proxy

http://maven.apache.org/guides/mini/guide-proxies.html

https://stackoverflow.com/questions/31032174/how-to-configure-a-proxy-server-for-both-http-and-https-in-mavens-settings-xml

 

Share Knowledge
This entry was posted in Java, Maven, Uncategorized and tagged , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *