Working with Proxies and internal artifact repositories

Posted on 15-Jun-2019 16:14:13


If you working at a start-up or using a personal laptop/IaaS/PaaS account, you will have an advantage of admin privileges and no usage of a proxy or an artifact repository like Nexus. But if you are working for larger organisations, you will have trouble with admin privileges and a proxy that you need to setup. While browsers are automatically set to use the system proxy, unfortunately many tools (GUI or command line executables) do not pick them by default and will need you to explicitly set the proxy. In most of the cases it is a one-time setup that you need to do when you join the organisation or do a team movement. However, in the fast changing technology world and an ever increasing open source community, there are new tools/libraries/platforms/frameworks/services, etc that are coming up and you keep running into a need to download and try something new which may solve a problem for your team or simplify an existing solution.

In this post, I will cover the proxy and Nexus setup for few of the use cases.





Proxy on a Mac or Linux/Unix terminal (using bash shell)

export http_proxy=http://proxyhost:port

export https_proxy=http://proxyhost:port

– replace http with https if its a HTTPS based proxy.

– http_proxy is for HTTP connections and https is for HTTPS connections.

If your proxy requires a user name and password, provide those also.

export http_proxy=http://username:password@proxyhost:port

export https_proxy=http://username:password@proxyhost:port

Setup for Maven

You have 2 options for providing the proxy details and one way to set Nexus as a repository.

  1. Set proxy in the Maven settings.xml file ($HOME/.m2/settings.xml).
  <id>proxyhost</id>


  <active>true</active>


  <protocol>http</protocol>


  <host>proxy.example.com</host>


  <port>proxyport</port>


  <username>username</username>


  <password>password</password>


  <nonProxyHosts>www.xyz.com</nonProxyHosts>

Set Nexus as your artifact repository in your settings.xml file to download the dependencies in your pom file.

<mirror>


  <id>central</id>


  <name>central</name>


  <url>http://your-host:8081/repository/maven-group/</url>


  <mirrorOf>*</mirrorOf>


</mirror>

And to set the repository in your .pom file, add the below to it.

<repository>


  <id>maven-group</id>


  <url>http://your-host:8081/repository/maven-group/</url>


</repository>
  1. Setting proxy in command line

Proxy can be supplied for Maven or infact the java binary as well with the D options.

mvn -D java.net.useSystemProxies=true java -D java.net.useSystemProxies=true

mvn -Dhttp.proxyHost=[proxyhost] -Dhttp.proxyPort=[proxyport] -Dhttp.nonProxyHosts=[proxybypasshosts]

java D http.proxyHost=[proxyhost] D http.proxyPort=[proxyport] D http.nonProxyHosts=[proxybypasshosts]

  1. Setting proxy in Java code

You can use the proxy inside Java code by setting the System properties.

  System.setProperty("java.net.useSystemProxies", "true");

or

  System.setProperty("http.proxyHost", host);


  System.setProperty("http.proxyPort", port);

Setting up pip to use Nexus

Luckily pip, atleast on bash terminals uses the http_proxy and https_proxy environment variables. Put the following in your pip.conf file to use Nexus PyPI repository instead of the external pypi.org.

[global]

index = http://nexushost:8081/repository/pypi-all/pypi

index-url = http://nexushost:8081/repository/pypi-all/simple

Setting up proxy for Docker on Mac

On Mac, Docker Desktop can be located on the top panel. The proxy can be setup under Preferences->Proxies.

Setting up Proxy in STS

The proxy in STS can be setup under Preferences->General->Network Connections.

Would you like to get notifications about New Blogs?