How to set up proxy for Maven ?

When I started using Apache Maven in my company I realized that, because of proxy settings, I couldn't download dependencies from the Internet. Dependencies are really powerful features of Maven, so it would be unreasonable not to use them. I found a solution to set up proxy so that Maven requests could go out my company's network.

I had to perform a few steps in order to set up proxy for Maven:
  1. I had to find Apache Maven settings.xml file.
  2. I had to modify my proxy settings.

1. Localize Apache Maven settings.xml file

 You have two settings.xml files:
  • Global settings file localized under $M2_HOME/conf/settings.xml. $M2_HOME is Apache Maven installation directory.
  • User settings file under ${user.home}/.m2/settings.xml. ${user.home} is user home directory and it depends on the operating system you are using:
    • ~/.m2/settings.xml on Unix systems (Mac OS X, Linux etc.).
    • C:\Users\your-username\.m2 on Windows.
Additional information:
  • User settings overwrite global settings,
  • settings.xml under ${user.home}/.m2/settings.xml may not exist. If it does not exist you need to create it (copy global settings to ${user.home}/.m2/settings.xml).
  • If you don't know where your Maven installation on Unix systems is (because M2_HOME variable is not set), you could execute mvn -version. It will return information about your Maven installation. settings.xml is located under conf directory under Maven home.

2. Set up Apache Maven proxy in settings.xml

I modified user settings.xml:
  1. Open ${user.home}/.m2/settings.xml
  2. Find proxies node inside this file.
  3. Set your proxy settings in proxy node:

<proxies>
    <proxy>
      <id>whatever-name-to-identifie-your-proxy</id>
      <active>true</active>
      <protocol>http</protocol>
      <host>proxy.your-company.com</host>
      <port>8080</port>
      <username>username</username>
      <password>password</password>
      <nonProxyHosts>*.exclude.me|and.me</nonProxyHosts>
    </proxy>
</proxies>
When settings are changed, you shouldn't have any further problems with downloading dependencies from the Internet.

0 comments :: How to set up proxy for Maven ?

Post a Comment