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.
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.
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.
Note that if you want to use Maven, you need to have Java installed and an environment variable set up.
You can start Maven with extra memory using MAVEN_OPTS environment variable. In this example we're setting an initial JVM Heap of 512Mb, a maximum Heap of 1024M, a thread stack of 2M and maximum Metaspace size of 1024M.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With