Apache Maven – Proxy setting explanation

In this article, we will explore and understand on how to configure/enable proxy setting in Maven

1. Proxy setting explanation:

  • Sometimes, Maven users sit behind the firewall especially when developers work in a client network. In these scenarios, we need to tweak proxy setting or enable proxy setting in Maven’s settings.xml located at {MAVEN_HOME}\conf\settings.xml
  • If this proxy setting is not enabled while working behind the firewall, then maven will fail to resolves dependencies (i.e.; connection refused to download any dependencies from internet)
  • In these circumstances, it’s a wise advice to enable proxies by configuring required attributes
  • Let’s understand how to configure mandatory values inside <proxy> element in settings.xml

1.1 Understand settings.xml file and find <proxy> element

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

1.2 Un-comment the <proxy> element –> configure required attributes –> save file

  • Protocol, username/password, host, port are self explanatory –> get these required values and configure it
  • Note: Make sure to mark <active> element as “true”, if you are intended to work via this proxy
<!-- 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>

1.3 That’s all !! Done !!

  • Now while executing any build commands, then Maven will download required dependencies (or artifacts) from internet via this proxy
  • If we got more than one <proxy> elements here, then first active (i.e.; marked true) takes the effects while executing any maven commands

Useful Eclipse IDE shortcuts :

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Apache Maven - pom.xml explanation
Apache Maven - Settings.xml explanation