Search code examples
mavenenvironment-variablessystem-properties

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.


Solution

  • 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>
        ...