Maven to use SOCKS proxy for specific repo

2019-06-02 08:52发布

问题:

I am trying to configure maven to use a socks proxy when accessing a specific repository, but not to use it for the maven central repository.

I have used export MAVEN_OPTS="-DsocksProxyHost=<proxyHost> -DsocksProxyPort=<proxyPort>" to set up the proxy initially, but then get Malformed reply from SOCKS server when it tries to download dependencies from repo.maven.apache.org/maven2.

I have also tried adding the following to settings.xml, instead of the $MAVEN_OPTS:

<proxies>
    <proxy>
      <active>true</active>
      <protocol>http</protocol>
      <host><proxyHost></host>
      <port><proxyPort></port>
      <nonProxyHosts>repo.maven.apache.org</nonProxyHosts>
    </proxy>
</proxies>

And also with SOCKS as the protocol. However, this results in the request timing out as it cannot reach the specific repository. However, it does reach repo.maven.apache.org/maven2, albeit slowly as it firsts check the specified repository.

Is there a way to cobine the two and have the SOCKS proxy for the repository I am specifying in settings.xml, and no proxy for repo.maven.apache.org?

回答1:

you should set protocol to socks5,like this(my setting):

<proxy>
    <id>ss</id>
    <active>true</active>
    <protocol>socks5</protocol>
    <username></username>
    <password></password>
    <host>127.0.0.1</host>
    <port>1080</port>
    <nonProxyHosts>127.0.0.1</nonProxyHosts>
</proxy>


回答2:

For anyone struggling with this problem, I'm gonna say another way to solve it. While it seems adding <proxy> to settings.xml should be the rightest solution, it did not work for me.

First, create SSH connection to your proxy server. You can use any service you like (Shadowsocks for example) but here is the simplest solution:

ssh -D 9999 yourname@your.gateway.com

This starts an ssh session to your server and also starts Socks proxy on port 9999. If you are doing this on a server you connected to using SSH connection, you might want to run this code under screen session so you can continue working with terminal while socks is still connected in background.

Now, when you want to build your artifacts, you can use this:

-DsocksProxyHost=127.0.0.1 -DsocksProxyPort=9999

This line of code uses Socks connection on local port 9999 to your server. Example:

mvn clean install -DsocksProxyHost=127.0.0.1 -DsocksProxyPort=8085

Also, you can alternatively export this into your environment:

export MAVEN_OPTS="-DsocksProxyHost=127.0.0.1 -DsocksProxyPort=8085"