6

I have one property file shown below

testdb.username=AA
testdb.password=AA
testdb.port=1521
testdb.host=localhost

now, I want to pass all these 4 key value pairs as env variable in maven command.

One way is to place each key value pair in maven command shown below

mvn clean package -Dtestdb.username=A -Dtestdb.password=AA ....

I want to know is there any way in Maven to pass whole properties file to Maven and Maven read property file and set all key value pairs dynamically as env variable in maven command.

Matthias
  • 4,481
  • 12
  • 45
  • 84
Abhay Agarwal
  • 328
  • 1
  • 4
  • 7

1 Answers1

9

Enviroment variables are referenced in maven like this:

<properties>
    <testdb.username>${env.ENV_USERNAME}</testdb.username>
    <testdb.password>${env.ENV_PASSWORD}</testdb.password>
    <testdb.port>${env.ENV_PORT}</testdb.port>
    <testdb.host>${env.ENV_HOST}</testdb.host>
</properties>

However, I think that you want to make is something like that:

<properties>
    <!-- Default values -->
    <testdb.username>foo</testdb.username>
    <testdb.password>AA</testdb.password>
    <testdb.port>1521</testdb.port>
    <testdb.host>localhost</testdb.host>
</properties>

...

${testdb.username}

mvn clean package -Dtestdb.username=$USERNAME -Dtestdb.password=$PASSWORD -Dtestdb.port=$PORT -dtestdb.host=$HOST

I hope you find this information useful!

Mar Millán
  • 188
  • 7