Search code examples
javamavenmaven-surefire-plugin

Maven surefire environmentVariables doesn't work on one machine


Background: we are setting current project version as system environment:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.19.1</version>
    <configuration>
        <forkMode>always</forkMode>
        <environmentVariables>
            <project.target>${project.build.outputDirectory}</project.target>
            <project.version>${project.version}</project.version>
        </environmentVariables>
    </configuration>
</plugin>

And then we get the value in the test using System.getenv("project.version"); and this works just fine on all machines… except for one.

The problematic machine is new AWS EC2 instance with Ubuntu 16.04 LTS. The setup and configuration looks ok. I checked both OpenJDK and OracleJDK, latest versions, I also checked included java and maven version:

$ java -version
openjdk version "1.8.0_151"
OpenJDK Runtime Environment (build 1.8.0_151-8u151-b12-0ubuntu0.16.04.2-b12)
OpenJDK 64-Bit Server VM (build 25.151-b12, mixed mode)

maven

$ mvn -version
Apache Maven 3.3.9
Maven home: /usr/share/maven
Java version: 1.8.0_151, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-8-openjdk-amd64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.4.0-1049-aws", arch: "amd64", family: "unix"

and the newest available for download:

$ mvn --version
Apache Maven 3.5.2 (138edd61fd100ec658bfa2d307c43b76940a5d7d; 2017-10-18T07:58:13Z)
Maven home: /usr/share/maven
Java version: 1.8.0_161, vendor: Oracle Corporation
Java home: /usr/lib/jvm/jdk1.8.0_161/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.4.0-1049-aws", arch: "amd64", family: "unix"

And the issue persists. Weird thing is that the forking is enabled (required) and log entry shows that the variables are being set:

[DEBUG] Setting environment variable [project.version]=[4.0.0-SNAPSHOT]

Then we have test execution with forking:

[DEBUG] boot(compact) classpath:  surefire-booter-2.19.1.jar  surefire-api-2.19.1.jar  test-classes  classes  junit-4.10.jar  hamcrest-core-1.1.jar  …-4.0.0-SNAPSHOT.jar  surefire-junit4-2.19.1.jar

Forking command line: /bin/sh -c cd /mnt/ebsVolume/

And yet System.getenv("project.version"); returns null


Solution

  • I think you need to use systemPropertyVariables instead. See docs here.

        <systemPropertyVariables>
            <project.target>${project.build.outputDirectory}</project.target>
            <project.version>${project.version}</project.version>
        </systemPropertyVariables>