Reputation: 12631
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
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
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
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
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
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:
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.
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>
In com.google.code.maven-replacer-plugin, make the replacement value ${foo}
.
In my shell script that calls maven, add this to the command: -Dfoo=$foo
Upvotes: 5
Reputation: 209
Can't we use
<properties>
<my.variable>${env.MY_VARIABLE}</my.variable>
</properties>
Upvotes: 17
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:
(NOTE: I was using maven v3.0.5)
I Hope that this can help someone....
Upvotes: 28