Environment Variable with Maven

JavaMavenEnvironment Variables

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

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>

Solution 2 - Java

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.

Solution 3 - Java

You could wrap your maven command in a bash script:

#!/bin/bash

export YOUR_VAR=thevalue
mvn test
unset YOUR_VAR

Solution 4 - Java

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>

Solution 5 - Java

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][1], if this is what you actually want:

  • ${maven.projectBasedir}/.mvn/maven.config
  • ${maven.projectBasedir}/.mvn/jvm.config

[1]: https://maven.apache.org/docs/3.3.1/release-notes.html "Release Notes – Maven 3.3.1"

Solution 6 - Java

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.

Solution 7 - Java

Following documentation from @Kevin's answer the below one worked for me for setting environment variable with maven sure-fire plugin

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
	<environmentVariables>
		<WSNSHELL_HOME>conf</WSNSHELL_HOME>
	</environmentVariables>
</configuration>

Solution 8 - Java

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

Solution 9 - Java

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

Solution 10 - Java

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"

Solution 11 - Java

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"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGianlucaView Question on Stackoverflow
Solution 1 - JavaPéter TörökView Answer on Stackoverflow
Solution 2 - JavaFrVaBeView Answer on Stackoverflow
Solution 3 - Javamodle13View Answer on Stackoverflow
Solution 4 - JavaKevinView Answer on Stackoverflow
Solution 5 - JavaRenéView Answer on Stackoverflow
Solution 6 - JavaChristianView Answer on Stackoverflow
Solution 7 - JavaYuvaraj GView Answer on Stackoverflow
Solution 8 - JavaLuis Eduardo Blandon PuelloView Answer on Stackoverflow
Solution 9 - JavaDavis BennyView Answer on Stackoverflow
Solution 10 - JavaHassekView Answer on Stackoverflow
Solution 11 - JavaDimos KoulView Answer on Stackoverflow