127

I've ported a project from Eclipse to Maven and I need to set an environment variable to make my project work.

In Eclipse, I go to "Run -> Run configurations" and, under the tab "environment", I set "WSNSHELL_HOME" to the value "conf".

How can I do this with Maven?

İsmail Y.
  • 3,579
  • 5
  • 21
  • 29
Gianluca
  • 2,379
  • 3
  • 25
  • 41
  • I would suggest to get your project running without environment variable. And then move to maven. – khmarbaise Apr 01 '11 at 08:20
  • 4
    There is no reason to skip the usage of environment variables if it is useful for your system and I am quite sure my solution works (I use it myself). See my hint refarding 'System.getenv'/'System.getProperty'. – FrVaBe Apr 04 '11 at 18:53

10 Answers10

160

You can just pass it on the command line, as

mvn -DmyVariable=someValue install

[Update] Note that the order of parameters is significant - you need to specify any options before the command(s).[/Update]

Within the POM file, you may refer to system variables (specified on the command line, or in the pom) as ${myVariable}, and environment variables as ${env.myVariable}. (Thanks to commenters for the correction.)

Update2

OK, so you want to pass your system variable to your tests. If - as I assume - you use the Surefire plugin for testing, the best is to specify the needed system variable(s) within the pom, in your plugins section, e.g.

<build>
    <plugins>
        ...
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            ...
            <configuration>
                ...
                <systemPropertyVariables>
                    <WSNSHELL_HOME>conf</WSNSHELL_HOME>
                </systemPropertyVariables>
            </configuration>
        </plugin>
        ...
    </plugins>
</build>
Community
  • 1
  • 1
Péter Török
  • 114,404
  • 31
  • 268
  • 329
  • I need to launch 'mvn test', it doesn't seems to work, cause if I write mvn test -DWSNSHELL_HOME=conf I still get the exception java.io.FileNotFoundException: null/wsnshell.conf (No such file or directory) – Gianluca Apr 01 '11 at 08:17
  • 3
    @Janky, try it the other way around, i.e. `mvn -DWSNSHELL_HOME=conf test`. – Péter Török Apr 01 '11 at 08:21
  • nope... :( I still get java.io.FileNotFoundException: null/wsnshell.conf (No such file or directory)... in the code I read the variable in this way.. `System.getenv("WSNSHELL_HOME");` is it correct? – Gianluca Apr 01 '11 at 08:27
  • @Janky, wait a minute, do you run the app directly from the Maven build, or ...? – Péter Török Apr 01 '11 at 08:40
  • @Péter Török I admit that I'm a total noob with maven... here is what I've done so far... I've converted an eclipse project to maven with IAM plugin.. now I'm tring to execute the tests I've written with `mvn test` but i've got this problem... should I launch some command before mvn test? should I edit the pom? Thanks a million! – Gianluca Apr 01 '11 at 08:54
  • I think I will remove the environment variable... cause it still doesn't work. Thank you very much for your help indeed!!! – Gianluca Apr 03 '11 at 09:36
  • 65
    You two are confusing Environment Variables with System Properties. The -D command option sets System Properties only. – Wouter Lievens Mar 19 '14 at 08:45
  • ${env.myVariable} does not seem to work. I think it is not setting an environment variable. – Jose Martinez Sep 18 '14 at 15:41
  • @JoseMartinez, of course it is not setting it, who said it did? Maven is only reading the variable/property's current value, which you must set yourself (or pass on the command line). – Péter Török Sep 19 '14 at 07:28
  • @PéterTörök Dude -DmyVariable=someValue this is not setting the env variable. I can tell because ${env.myVariable} does not work. But -DmyVariable=someValue does set the "system property", and that I can tell because ${myVariable} works. – Jose Martinez Sep 21 '14 at 17:56
  • @JoseMartinez, I see, you are right with that. My fault, thanks for the clarification. I corrected my answer accordingly. – Péter Török Sep 22 '14 at 07:49
  • If the options should be specified before the command, why does `mvn clean install -DskipTests` work ? Shouldn't it be `mvn -DskipTests clean install` – Olivier Boissé Jan 27 '18 at 16:03
  • I realize if variable is not in certain format it throws lifecycle error. example: `mvn clean test -Denable.it=true` – old-monk Apr 21 '22 at 12:42
  • I had to set up file encdoing for unit tests. Solution of *Update 2* works for me: `UTF-8`. – andy Jan 27 '23 at 11:06
  • @PéterTörök Hi. Is it possible to setUp Environment Variables or System Properties via maven not only for test but for Runtime? – Dred Feb 27 '23 at 10:02
33

The -D properties will not be reliable propagated from the surefire-pluging to your test (I do not know why it works with eclipse). When using maven on the command line use the argLine property to wrap your property. This will pass them to your test

mvn -DargLine="-DWSNSHELL_HOME=conf" test

Use System.getProperty to read the value in your code. Have a look to this post about the difference of System.getenv and Sytem.getProperty.

Community
  • 1
  • 1
FrVaBe
  • 47,963
  • 16
  • 124
  • 157
  • 2
    You should read the property with System.getProperty("WSNSHELL_HOME") instead of using System.getenv. – FrVaBe Apr 03 '11 at 15:06
  • Perfect, after spending A LOT of time trying other things, this one worked for me. – Bhushan Feb 07 '14 at 19:06
  • If you have configured argLine parameters for the surefire plugin in your pom.xml, then add the `${argLine}` placeholder, otherwise the parameters from the command line will not be picked up. Example: `-Duser.timezone=UTC ${argLine}` – sorrymissjackson Apr 09 '18 at 13:37
  • This worked for me, thanks! What a neat tool to configure sys-props for maven tests. Still at the first glance, it is very confusing, why it cannot use them directly, but requires an extra argument. – phi Jun 08 '20 at 14:27
23

You could wrap your maven command in a bash script:

#!/bin/bash

export YOUR_VAR=thevalue
mvn test
unset YOUR_VAR
modle13
  • 1,242
  • 2
  • 16
  • 16
  • Unbelievably, this is the only method that completely worked for me using the Surefire plugin. Out of the 10 or so env vars I set in the POM, two of them are not seen when the test run. Even if I set them in the shell and then `mvn install`, they are still somehow obscured. However, if I run from a shell script, it works fine! I wish I understood why this happens. – Pete Aug 06 '18 at 02:28
  • I believe this should be an accepted answer since this is the simplest solution – wtsiamruk Jul 26 '20 at 18:23
12

For environment variable in Maven, you can set below.

http://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html#environmentVariables http://maven.apache.org/surefire/maven-failsafe-plugin/integration-test-mojo.html#environmentVariables

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    ...
    <configuration>
        <includes>
            ...
        </includes>
        <environmentVariables>
            <WSNSHELL_HOME>conf</WSNSHELL_HOME>
        </environmentVariables>
    </configuration>
</plugin>
zx485
  • 28,498
  • 28
  • 50
  • 59
Kevin
  • 129
  • 1
  • 2
5

Another solution would be to set MAVEN_OPTS (or other environment variables) in ${user.home}/.mavenrc (or %HOME%\mavenrc_pre.bat on windows).

Since Maven 3.3.1 there are new possibilities to set mvn command line parameters, if this is what you actually want:

  • ${maven.projectBasedir}/.mvn/maven.config
  • ${maven.projectBasedir}/.mvn/jvm.config
René
  • 904
  • 13
  • 26
4

There is a maven plugin called properties-maven-plugin this one provides a goal set-system-properties to set system variables. This is especially useful if you have a file containing all these properties. So you're able to read a property file and set them as system variable.

nanosoft
  • 2,913
  • 4
  • 41
  • 61
Christian
  • 1,664
  • 1
  • 23
  • 43
0

in your code add:

System.getProperty("WSNSHELL_HOME")

Modify or add value property from maven command:

mvn clean test -DargLine=-DWSNSHELL_HOME=yourvalue

If you want to run it in Eclipse, add VM arguments in your Debug/Run configurations

  • Go to Run -> Run configurations
  • Select Tab Arguments
  • Add in section VM Arguments

-DWSNSHELL_HOME=yourvalue

See image example

you don't need to modify the POM

shizhen
  • 12,251
  • 9
  • 52
  • 88
0

You can pass some of the arguments through the _JAVA_OPTIONS variable.

For example, define a variable for maven proxy flags like this:

_JAVA_OPTIONS="-Dhttp.proxyHost=$http_proxy_host -Dhttp.proxyPort=$http_proxy_port -Dhttps.proxyHost=$https_proxy_host -Dhttps.proxyPort=$http_proxy_port"

And then use mvn clean install (it will automatically pick up _JAVA_OPTIONS).

Rohde Fischer
  • 1,248
  • 2
  • 10
  • 32
Davis Benny
  • 99
  • 1
  • 3
-1

I suggest using the amazing tool direnv. With it you can inject environment variables once you cd into the project. These steps worked for me:

.envrc file

source_up
dotenv

.env file

_JAVA_OPTIONS="-DYourEnvHere=123"
Hassek
  • 8,715
  • 6
  • 47
  • 59
-1

As someone might end up here changing his global Java options, I want to say defining _JAVA_OPTIONS is a bad idea. Instead define MAVEN_OPTS environment variable which will still be picked up automatically by Maven but it won't override everything like _JAVA_OPTS will do (e.g. IDE vm options).

MAVEN_OPTS="-DmyVariable=someValue"
Dimos Koul
  • 31
  • 3