user1016403
user1016403

Reputation: 12631

How to refer environment variable in POM.xml?

I am using Maven as build tool. I have set an environment variable called env. How can I get access to this environment variable's value in the pom.xml file?

Upvotes: 227

Views: 258234

Answers (7)

Ravi
Ravi

Reputation: 379

is it possible to construct like http address in pom.xml properties? eg:

<properties>
  <app_url>https://${app_name}.${domain_name}</app_url>
</properties>

Upvotes: 0

EricGreg
EricGreg

Reputation: 1178

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

Upvotes: 48

yoAlex5
yoAlex5

Reputation: 34471

You can use <properties> tag to define a custom variable and ${variable} pattern to use it

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <!-- define -->
    <properties>
        <property.name>1.0</property.name>
    </properties>

    <!-- using -->
    <version>${property.name}</version>

</project>

Upvotes: 1

Andrew White
Andrew White

Reputation: 53536

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

Upvotes: 275

fjones
fjones

Reputation: 104

I was struggling with the same thing, running a shell script that set variables, then wanting to use the variables in the shared-pom. The goal was to have environment variables replace strings in my project files using the com.google.code.maven-replacer-plugin.

Using ${env.foo} or ${env.FOO} didn't work for me. Maven just wasn't finding the variable. What worked was passing the variable in as a command-line parameter in Maven. Here's the setup:

  1. Set the variable in the shell script. If you're launching Maven in a sub-script, make sure the variable is getting set, e.g. using source ./maven_script.sh to call it from the parent script.

  2. In shared-pom, create a command-line param that grabs the environment variable:

<plugin>
  ...
  <executions>
    <executions>
    ...
      <execution>
      ...
        <configuration>
          <param>${foo}</param> <!-- Note this is *not* ${env.foo} -->
        </configuration>
  1. In com.google.code.maven-replacer-plugin, make the replacement value ${foo}.

  2. In my shell script that calls maven, add this to the command: -Dfoo=$foo

Upvotes: 5

tmjee
tmjee

Reputation: 209

Can't we use

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

Upvotes: 17

Carlitos Way
Carlitos Way

Reputation: 3434

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

Upvotes: 28

Related Questions