0

I'd like to set a system environment variable to be visible by Java code in the unit tests of a Maven project. I tried this:

mvn -Dfoo=bar clean package

But that doesn't seem to take. How do I accomplish this?

David Williams
  • 8,388
  • 23
  • 83
  • 171

1 Answers1

1

You should be able to use a Maven Property in your pom.xml

Check the Properties section of http://maven.apache.org/settings.html#Activation

or maybe this question might help How to identify and set a missing environment property in Maven?

Edit: You can also set an environment variable in the section of a plugin like the surefire plugin: Environment Variable with Maven

Community
  • 1
  • 1
jmcmahon
  • 630
  • 1
  • 7
  • 14
  • So no way to do it on the command line? The reason I ask is because I am using this to pass a config parameter to the underlying Java tests. Ah got it, thanks, the last edit got it. Cheers. – David Williams Dec 09 '13 at 20:33
  • However, it looks like from these links that the path to getting an System env variable into the test code from the command line through Maven is not implemented. – David Williams Dec 09 '13 at 20:53
  • Yep, I think that's right. I believe you can only set Maven system variables through the command line. Maven handles Environment variables completely different. I only way I know to set env vars through maven is in the pom. – jmcmahon Dec 09 '13 at 21:37
  • 1
    Cool, so I decided just to use a unix environment variable to store the data I wanted, then used `System.getenv("foo")` which is all good for the app. – David Williams Dec 09 '13 at 23:13