How to refer environment variable in POM.xml? How to refer environment variable in POM.xml? java java

How to refer environment variable in POM.xml?


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