Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to refer environment variable in POM.xml?

People also ask

How do I pass an environment variable in Maven?

Add 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.

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. Open Google and search for maven download.

How do I set my M2 at home?

Set M2_HOME & PATHCreate an environment variable named M2_HOME which refers to directory where maven was untarred/ unzipped. and then add this variable to PATH environment variable. $>export PATH=$M2_HOME/bin:$PATH. Click OK, then Edit the 'Path' user variable to add M2_HOME\bin folder in it.


Check out the Maven Properties Guide...

As Seshagiri pointed out in the comments, ${env.VARIABLE_NAME} will do what you want.

I will add a word of warning and say that a pom.xml should completely describe your project so please use environment variables judiciously. If you make your builds dependent on your environment, they are harder to reproduce


It might be safer to directly pass environment variables to maven system properties. For example, say on Linux you want to access environment variable MY_VARIABLE. You can use a system property in your pom file.

<properties>
    ...
    <!-- Default value for my.variable can be defined here -->
    <my.variable>foo</my.variable>
    ...
</properties>
...
<!-- Use my.variable -->
... ${my.variable} ...

Set the property value on the maven command line:

mvn clean package -Dmy.variable=$MY_VARIABLE

Also, make sure that your environment variable is composed only by UPPER CASE LETTERS.... I don't know why (the documentation doesn't say nothing explicit about it, at least the link provided by @Andrew White), but if the variable is a lower case word (e.g. env.dummy), the variable always came empty or null...

i was struggling with this like an hour, until I decided to try an UPPER CASE VARIABLE, and problem solved.

OK Variables Examples:

  • DUMMY
  • DUMMY_ONE
  • JBOSS_SERVER_PATH

(NOTE: I was using maven v3.0.5)

I Hope that this can help someone....


Can't we use

<properties>
    <my.variable>${env.MY_VARIABLE}</my.variable>
</properties>