SystemPropertyVariable is not read in Eclipse IDE

Viewed 181

In ma maven project I have something like below in pom.xml

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.19.1</version>
    <configuration>
    <systemPropertyVariables>
        <environment>DEV</environment>
    </systemPropertyVariables>
    </configuration>
</plugin>

And of course I use this property in my Java code. However my problem is that if I run my tests in Eclipse then this property is not read from pom file and finally my 'environment' property stays empty.

If I run my tests from console adding ' -Denvironment=DEV ' then it's ok.

My question is how to configure Eclipse so that it sees my 'environment' property as it is while I run it from console?

Can anyone help please? I cannot find solution by myself...

Thanks

1 Answers

This is a common misunderstanding of how Maven works.

The core of Maven defines a build lifecycle, essentially a state machine that tells it how to transition to a given phase. For example, if you run:

mvn test-compile

Maven knows that it first needs to through a series of steps (states) before it can execute test-compile. Each of these states may or may not have a plugin attached to it that Maven will execute, given how the POM is written. So essentially, Maven itself doesn't do any resource copying, compiling, testing, packaging, etc. All of these tasks are delegated to plugins.

When you import a Maven project into Eclipse, it will provide its own mapping of plugins, for various reasons. Some of these (like compiling) will make sense for the IDE to do, it will use its own plugins to do the job. Other phases that is not part of what Eclipse normally does (such as packaging) will have no mapping and thus no plugin to execute.

Running JUnit tests is not what Eclipse does normally as part of the build, so that's why you run JUnit tests manually (right-click test class > Run as > JUnit test). Eclipse simply ignores the surefire plugin since it uses its own internal JUnit runner to run the tests and as such it doesn't pick up the configuration from the surefire plugin.

I have no doubt that this could be made to work in Eclipse but at the time of writing, it simply doesn't. Have you tried IntelliJ by any chance?

However, since one could argue that Eclipse always is in "DEV" mode, would a suitable workaround be that you statically set the system property on the JRE in Eclipse?

Like so:

Window > Preferences > Java > Installed JREs > select your JRE > Edit... > Default VM arguments: -DDEV

Related