I'm using Maven 3.x. I know how to configure proxies in the .m2/settings.xml file. What I am unable to figure out is how to specify which of the proxies is used, either on the command line, or via some system property. This comment appears in the sample settings.xml provided by maven.apache.org:
This is a list of proxies which can be used on this machine to connect to the network.
Unless otherwise specified (by system property or command-line switch), the first proxy
specification in this list marked as active will be used.
But I have not found anywhere where they tell you (a) how to set up the proxies in settings.xml to support the above contention that you can choose which proxy to use, or (b) how to specify on the command line which proxy to use.
Assume the following:
<settings>
<proxies>
<proxy>
<id>work-one</id>
<protocol>http</protocol>
<host>1.2.3.4</host>
<port>80</port>
<nonProxyHosts>127.0.0.1,localhost,foo...</nonProxyHosts>
</proxy>
<proxy>
<id>work-two</id>
<protocol>http</protocol>
<host>5.6.7.8</host>
<port>80</port>
<nonProxyHosts>127.0.0.1,localhost,bar...</nonProxyHosts>
</proxy>
</proxies>
</settings>
There are actually three situations.
So, assuming that the comment from the sample settings.xml (above) is correct, my quest is for a solution that meets the following:
ALREADY TRIED UNSUCCESSFULLY
Using a profile to set properties which are then used in the proxy: Fail: Complained about regexp errors in nonProxyHosts and non-integer in port, despite the fact that the profile was stated on the command line via -P argument, and the profile declaration was before the proxy declaration. See 1608079, it didn't work for me, and if it did work, how would you specify no proxy at all?
% mvn -U -P workone
... usual stuff...
[WARNING] Some problems were encountered while building the effective settings
[WARNING] Unable to parse element 'port', must be an integer (position: END_TAG seen ...<port>${proxy-port}</port>... @243:30) caused by: java.lang.NumberFormatException: For input string: "${proxy-port}"
... more stuff ...
[ERROR] Internal error: java.util.regex.PatternSyntaxException: Illegal repetition near index 0
[ERROR] ${proxy-no_proxy}
[ERROR] ^
The settings.xml was this:
<settings>
...
<profile>
<id>workone</id>
<properties>
<proxy-protocol>http</proxy-protocol>
<proxy-port>80</proxy-port>
<proxy-host>hostnameone</proxy-host>
<proxy-no_proxy>127.0.0.1,127.0.1.1,localhost,machine,*.local,*.domain.name.com</proxy-no_proxy>
</properties>
</profile>
<profile>
<id>worktwo</id>
<properties>
<proxy-protocol>http</proxy-protocol>
<proxy-port>80</proxy-port>
<proxy-host>hostnametwo</proxy-host>
<proxy-no_proxy>127.0.0.1,127.0.1.1,localhost,machine,*.local,*.other.domain.name.com</proxy-no_proxy>
</properties>
</profile>
</profiles>
<proxies>
<proxy>
<protocol>${proxy-protocol}</protocol>
<host>${proxy-host}</host>
<port>${proxy-port}</port>
<nonProxyHosts>${proxy-no_proxy}</nonProxyHosts>
</proxy>
</proxies>
</settings>
Assuming that id was the "profile name" of the proxy: Fail: Since you don't want the proxy used by default, active had to be set false. If you leave active out, it defaults true, and the first active proxy is automatically used. If you set active=false, -P work-one does not turn the proxy on. If you leave them both active=true, then the first one is used regardless, which won't work if you're not behind that proxy, and would not work when you were at home.
Java system properties: Fail: There are examples that suggest setting the java system properties for proxies, however those examples also state that it won't work for a number of situations that are commonly found in maven. It also would result in lots of typing on the command line, a less than desirable situation.
Linux script: Fail, doesn't work for Windows: Yes, I have a Linux script that will set proxies for a variety of tools that I use, however the solution doesn't work for Windows, and is therefore not an answer to this question. It's also awkward, since if the network doesn't connect correctly before you log in, you end up with bogus proxy information. (Fix network, logout, log back in, proxies okay, ugh.)
INTERIM RESULT: The answer seems to be that there is no maven-ish way to handle this, or a bunch of other things in the settings.xml file. As a result, I'm presently withholding acceptance of an answer indefinitely.
Profiles look like the most likely place to solve this, but proxies is not one of the blocks that you can enter inside a profile block, nor is mirrors (which is something else I've run into needing to have three ways to set it).
mvn has a -s switch
-s,--settings <arg> Alternate path for the user
settings file
I was not able to dynamically switch the proxy from command prompt. So I ended up creating 3 separate settings files (one for each proxy), say work1-settings.xml, work2-settings.xml , settings.xml (for home network).
I had setup Windows environment variables like
WORK1=-s c:\Users\coderplus\.m2\work1-settings.xml
WORK2=-s c:\Users\coderplus\.m2\work2-settings.xml
When at home, I would use
mvn clean install
(this will pick the default settings.xml file)
When at work1, I would use
mvn clean install %WORK1%
When at work2, I would use
mvn clean install %WORK2%
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With