Maven provides a built-in method of doing this, via a file called settings.xml, and this has been covered in other answers. However, it is customary, particularly in Linux, for command-line tools to automatically use the proxy specified by the environment variable https_proxy
.
To follow the Don't repeat yourself principle (which is intended to help you avoid mistakes), it would be nice if mvn
could automatically work with that too.
Here's a shell script that makes the necessary conversions:
#! /usr/bin/env bash
function javaproxy {
## using "Shell Parameter Expansion"
request_scheme=$1 ; proxy=$2
notscheme=$(echo ${proxy#*://}) ## parse
scheme=$(echo ${proxy%${notscheme}}) ## remove
scheme=$(echo ${scheme%://}) ## strip
hostport=$(echo ${proxy#*//*}) ## parse
host=$(echo ${hostport%:*}) ## parse
port=$(echo ${hostport#${host}}) ## remove
port=$(echo ${port#:}) ## strip
scheme=$(echo ${scheme:-http}) ## default
host=$(echo ${host:-localhost}) ## default
port=$(echo ${port:-8080}) ## default
echo -n " -D${request_scheme}.proxyHost=${host}"
echo -n " -D${request_scheme}.proxyPort=${port}"
}
JTO=""
if [ $http_proxy ] ; then
JTO="${JTO}$(javaproxy http ${http_proxy})"
fi
if [ $https_proxy ] ; then
JTO="${JTO}$(javaproxy https ${https_proxy})"
fi
if [ $no_proxy ] ; then
JTO="${JTO} -Dhttp.nonProxyHosts=$(echo \"${no_proxy}\"|tr ',' '|')"
fi
export JAVA_TOOL_OPTIONS=${JTO}
echo "JAVA_TOOL_OPTIONS=${JAVA_TOOL_OPTIONS}"
mvn_friendliness_options+=--update-snapshots
mvn ${mvn_friendliness_options} $@
You might name this something like proxied_mvn
and run it as:
$ https_proxy=http://localhost:58080 ./proxied_mvn clean package
Alternatively, you could just move the environment setup into your startup scripts.
gotchas
There are many things that can go wrong when trying to configure Maven for access to a Nexus through a proxy. Hopefully, this script will help with some of the most finicky issues, but others remain:
Nexus credentials available and correct (only if required)
Check with mvn help:effective-settings
Maven caching: "resolution will not be reattempted"
mvn clean package --update-snapshots
Maven wall-of-text output -- you have to look closely at the output to make sure errors messages aren't subtly different between runs
Older versions of Java may require _JAVA_OPTIONS
instead of JAVA_TOOL_OPTIONS
.
epilogue
There is more than one kind of Proxy. Correspondingly, there is more than one way that this question has been interpreted -- contributing to the large number of disparate answers here.
I have explicitly addressed the case of a (forward HTTP/HTTPS) web proxy server, which is used to access the internet from within a company network (for some companies). This may be notably distinct from a SOCKS proxy, which has also been addressed in some answers here.
Oh by the way, since it uses JAVA_TOOL_OPTIONS
, this solutions can be applied to running your other Java applications inside a proxy too.
Parting hint... My example above uses http://localhost:58080
. This is because I've set up port-forwarding from my CLIENT_PROXY=localhost:58080
to the actual network proxy by using WSL on my remote-access client to run:
ssh $PROXY_CLIENT -R $CLIENT_PROXY:$SERVER_PROXY