Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing env variables in maven command [duplicate]

Tags:

java

maven

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.

like image 456
Abhay Agarwal Avatar asked Aug 09 '18 18:08

Abhay Agarwal


People also ask

How do I pass an environment variable in Maven?

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.

How do you pass an environment variable in POM xml?

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.

Do we need to set environment variables for Maven?

Note that if you want to use Maven, you need to have Java installed and an environment variable set up.

How do I increase Maven available memory by setting environment variable?

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.


1 Answers

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!

like image 143
Mar Millán Avatar answered Sep 23 '22 14:09

Mar Millán