Gabriel Petrovay
Gabriel Petrovay

Reputation: 21864

Can one replace the maven -D system properties with environment variables?

In a certain environment I have to run Maven using system properties to pass over a proxy:

mvn -DproxySet=true -DproxyHost=... -DproxyPort=... clean install

Are there some Maven envinronment variables that I can use to achieve the same thing?

I would imagine something like this:

PROXY_SET=true PROXY_HOST=... PROXY_PORT=... mvn clean install

What I want to achieve is to run only:

mvn clean install

regardless where I am. (I can set the environment variables that tell me if I am behind a proxy or not)

UPDATE: This question is not a duplicate of How do I use Maven through a proxy? because I also want to work seamlessly without the proxy. I want to work from both behind and from the internet without having to type the -Dproxy... properties.

Upvotes: 4

Views: 1313

Answers (2)

OrangeDog
OrangeDog

Reputation: 38777

If you're invoking Maven via a script that uses MAVEN_OPTS, e.g. mvn.bat:

MAVEN_OPTS="-DproxySet=true -DproxyHost=..."

If not then you could always write your own wrapper script.

Alternatively, you could configure your proxy settings to read values from the environment:

<proxies>
  <proxy>
    <id>example-proxy</id>
    <active>${env.PROXY_SET}</active>
    <host>${env.PROXY_HOST}</host>
    <port>${env.PROXY_PORT}</port>
    ...

Upvotes: 4

J Fabian Meier
J Fabian Meier

Reputation: 35795

I would use the settings.xml with something like

<proxies>
  <proxy>
     <id>example-proxy</id>
     <active>true</active>
     <protocol>http</protocol>
     <host>proxy.example.com</host>
     <port>8080</port>
     <username>proxyuser</username>
     <password>somepassword</password>
     <nonProxyHosts>www.google.com|*.example.com</nonProxyHosts>
   </proxy>
 </proxies>

https://maven.apache.org/guides/mini/guide-proxies.html

Upvotes: 1

Related Questions