If your computer connects to the Internet through a proxy server, Apache Maven has to be configured with the proxy settings to connect to the public Maven repository. Without the proxy setting, most likely Apache Maven will throw timeout errors when attempting to resolve dependencies. This issue can also propagate into your IDEs such as Eclipse or IntelliJ IDEA. This article explains how to set proxy for Maven and update those changes in both IntelliJ IDEA and Eclipse.

Set Proxy in settings.xml

Step 1:
Open the settings.xml file from the following location. If you do not have this file already, skip this step and go to Step 2.

Operating SystemLocation
Linux~/.m2/settings.xml
Mac~/.m2/settings.xml
WindowsC:\Users\{Username}\.m2\settings.xml

Step 2:
Add the following configuration inside the settings tag. If you are creating this file for the first time, copy and paste the final output into a new editor.

<proxies>
<!-- Proxy for HTTP -->
<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</nonProxyHosts>
</proxy>

<!-- Proxy for HTTPS -->
<proxy>
<id>optional</id>
<active>true</active>
<protocol>https</protocol>
<username>proxyuser</username>
<password>proxypass</password>
<host>proxy.host.net</host>
<port>80</port>
<nonProxyHosts>local.net</nonProxyHosts>
</proxy>
</proxies>

After adding the proxy configuration, the full settings.xml should look like this:

<?xml version="1.0" encoding="UTF-8"?>
<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">
<pluginGroups/>
<proxies>
<!-- Proxy for HTTP -->
<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</nonProxyHosts>
</proxy>

<!-- Proxy for HTTPS -->
<proxy>
<id>optional</id>
<active>true</active>
<protocol>https</protocol>
<username>proxyuser</username>
<password>proxypass</password>
<host>proxy.host.net</host>
<port>80</port>
<nonProxyHosts>local.net</nonProxyHosts>
</proxy>
</proxies>

<servers/>
<mirrors/>
<profiles/>
</settings>

Step 3:
Modify the configurations according to your network proxy settings. Leave the username and password empty, if they are not applicable for you. For example, if the proxy host is cache.javahelps.com and the port number is 3128, your proxy settings should look like this:

<?xml version="1.0" encoding="UTF-8"?>
<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">
<pluginGroups/>
<proxies>
<!-- Proxy for HTTP -->
<proxy>
<id>optional</id>
<active>true</active>
<protocol>http</protocol>
<username></username>
<password></password>
<host>cache.javahelps.com</host>
<port>3128</port>
<nonProxyHosts>local.net|some.host.com</nonProxyHosts>
</proxy>

<!-- Proxy for HTTPS -->
<proxy>
<id>optional</id>
<active>true</active>
<protocol>https</protocol>
<username></username>
<password></password>
<host>cache.javahelps.com</host>
<port>3128</port>
<nonProxyHosts>local.net|some.host.com</nonProxyHosts>
</proxy>
</proxies>

<servers/>
<mirrors/>
<profiles/>
</settings>

Step 4:
After making all the changes, save the file to the following location.

Operating SystemLocation
Linux~/.m2/settings.xml
Mac~/.m2/settings.xml
WindowsC:\Users\{Username}\.m2\settings.xml

Update the Changes in Apache Maven CLI

You don’t need to make any further changes for the Apache Maven CLI to use the proxy. However, if you already tried the Maven command before making the changes and it failed due to the proxy error, Apache Maven may not retry downloading the files. To force Maven to redownload, run the following command from the project parent folder. It will remove the dependencies from the local Maven repository and redownload them again.

mvn dependency:purge-local-repository

After running the above command, if you run your usual build command, the build should succeed given there are no other errors and you configured the proxy correctly.


Update the Changes in IntelliJ IDEA

Step 1:
IntelliJ IDEA also uses Apache Maven behind the scene to build the project if it is a Maven project. Therefore, run the following command from the IntelliJ terminal or system terminal to redownload the dependencies.

mvn dependency:purge-local-repository

Step 2:
Reload the Maven project by right-clicking on the project and selecting the Maven → Reload Project menu.

Step 3 (only if Step 2 doesn’t work):
In the worst case if it doesn’t help, go to the IntelliJ IDEA settings → Build, Execution, Deployment → Build Tools → Maven and make sure the User settings file is pointing to the settings.xml file you modified in the first part of this article. Apply the changes by clicking on “Apply” and “OK”.


Update the Changes in Eclipse

Step 1:
Similar to IntelliJ IDEA, Eclipse also uses Apache Maven to build maven projects. Therefore, run the following command from the terminal to redownload the dependencies.

mvn dependency:purge-local-repository

Step 2:
Go to Window → Preferences → Maven → User Settings. Make sure the User settings file is pointing to the settings.xml file you modified in the first part of this article and click the “Update Settings” button. Apply the changes by clicking the “Apply and Close” button.

Step 3:
Reload the Maven project by right-clicking on the project and selecting the Maven → Update Project menu.

Have you found this article useful? Please let me know below in the comments. Knowing someone found my articles useful motivates me to write more. Also, comment below if you face any issues with following this article or getting it working. I will try my best to help you resolve the problem. The Java Helps community is also willing to help each other and grow together.

Share.
Exit mobile version