13

Is it possible to set env vars in pom.xml that junit tests uses? Netbeans can get env vars and builds project correnctly. But when i use command line (mvn clean -> mvn install), build fails due to test errors.

Thanks.

johnny-b-goode
  • 3,792
  • 12
  • 45
  • 68

1 Answers1

21

There are two approaches I know.

Commandline:

You can pass it in command line like

mvn -DAM_HOME=conf install

Using pom :

If AM_HOM is the variable and the value is conf ,make entry like below.

<plugins>
    ...


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

...
<plugins>
gosua
  • 1,017
  • 4
  • 15
  • 32
amicngh
  • 7,831
  • 3
  • 35
  • 54
  • 1
    so , why can't he add these lines directly to the POM.xml rather than adding them through command line – KItis Jun 18 '12 at 09:01
  • This is the same solution that i use before. System.getEnv("AM_HOME") returns null. – johnny-b-goode Jun 18 '12 at 12:19
  • Sorry, error was found. Thank you for help. But i use instead – johnny-b-goode Jun 18 '12 at 13:17
  • BTW, dont you know, is it possible to define some env var using previously defined var? For example: conf ${AM_HOME}2 – johnny-b-goode Jun 18 '12 at 13:28
  • 1
    That does the trick. This is the right way to set env variables to be read later using `System.getEnv`, different to most answers to this same question around the internet that say the right way is setting a property. – Nicolás Arias Apr 05 '22 at 15:51