Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set env variables for maven to run test correctly?

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.

like image 408
johnny-b-goode Avatar asked Jun 18 '12 07:06

johnny-b-goode


People also ask

How do I set environment variables in Maven?

Step 5 - Set Maven Environment VariablesAdd M2_HOME, M2, MAVEN_OPTS to environment variables. Set the environment variables using system properties. Open command terminal and set environment variables. Open command terminal and set environment variables.

How do I run the Maven test clean?

We can run our unit tests with Maven by using the command: mvn clean test. When we run this command at command prompt, we should see that the Maven Surefire Plugin runs our unit tests. We can now create a Maven project that compiles and runs unit tests which use JUnit 5.

How do you pass an environment variable in POM XML?

To refer to environment variables from the pom. xml, we can use the ${env. VARIABLE_NAME} syntax. We should remember to pass the Java version information via environment variables.


1 Answers

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>
like image 167
amicngh Avatar answered Oct 19 '22 19:10

amicngh