如何在POM.xml中引用环境变量?

时间:2023-01-25 11:34:41

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?

我使用maven作为构建工具。我已经设置了一个名为env的环境变量。如何在pom.xml文件中访问此环境变量的值?

5 个解决方案

#1


145  

Check out the Maven Properties Guide...

查看Maven属性指南...

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

正如Seshagiri在评论中指出的那样,$ {env.VARIABLE_NAME}会做你想要的。

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

我将添加一个警告,并说pom.xml应该完整地描述您的项目,所以请明智地使用环境变量。如果您的构建取决于您的环境,则它们更难以重现

#2


16  

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.

将环境变量直接传递给maven系统属性可能更安全。例如,在Linux上,您想要访问环境变量MY_VARIABLE。您可以在pom文件中使用系统属性。

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

在maven命令行上设置属性值:

mvn clean package -Dmy.variable=$MY_VARIABLE

#3


12  

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

另外,请确保您的环境变量仅由大写字母组成....我不知道为什么(文档没有明确说明它,至少@Andrew White提供的链接),但是如果变量是一个小写字(例如env.dummy),变量总是空或空...

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

我正在努力解决这个问题,直到我决定尝试UPPER CASE VARIABLE,问题解决了。

OK Variables Examples:

OK变量示例:

  • DUMMY
  • DUMMY_ONE
  • DUMMY_ONE
  • JBOSS_SERVER_PATH
  • JBOSS_SERVER_PATH

(NOTE: I was using maven v3.0.5)

(注意:我使用的是maven v3.0.5)

I Hope that this can help someone....

我希望这可以帮助某人......

#4


9  

Can't we use

我们不能用

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

#5


1  

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.

我正在努力解决同样的问题,运行一个设置变量的shell脚本,然后想要使用shared-pom中的变量。目标是使用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:

使用$ {env.foo}或$ {env.FOO}对我不起作用。 Maven只是没找到变量。有效的是将变量作为Maven中的命令行参数传递。这是设置:

  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.

    在shell脚本中设置变量。如果您在子脚本中启动Maven,请确保变量已设置,例如使用source ./maven_script.sh从父脚本中调用它。

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

    在shared-pom中,创建一个抓取环境变量的命令行参数:

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

    在com.google.code.maven-replacer-plugin中,设置替换值$ {foo}。

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

    在我调用maven的shell脚本中,将其添加到命令:-Dfoo = $ foo

#1


145  

Check out the Maven Properties Guide...

查看Maven属性指南...

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

正如Seshagiri在评论中指出的那样,$ {env.VARIABLE_NAME}会做你想要的。

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

我将添加一个警告,并说pom.xml应该完整地描述您的项目,所以请明智地使用环境变量。如果您的构建取决于您的环境,则它们更难以重现

#2


16  

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.

将环境变量直接传递给maven系统属性可能更安全。例如,在Linux上,您想要访问环境变量MY_VARIABLE。您可以在pom文件中使用系统属性。

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

在maven命令行上设置属性值:

mvn clean package -Dmy.variable=$MY_VARIABLE

#3


12  

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

另外,请确保您的环境变量仅由大写字母组成....我不知道为什么(文档没有明确说明它,至少@Andrew White提供的链接),但是如果变量是一个小写字(例如env.dummy),变量总是空或空...

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

我正在努力解决这个问题,直到我决定尝试UPPER CASE VARIABLE,问题解决了。

OK Variables Examples:

OK变量示例:

  • DUMMY
  • DUMMY_ONE
  • DUMMY_ONE
  • JBOSS_SERVER_PATH
  • JBOSS_SERVER_PATH

(NOTE: I was using maven v3.0.5)

(注意:我使用的是maven v3.0.5)

I Hope that this can help someone....

我希望这可以帮助某人......

#4


9  

Can't we use

我们不能用

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

#5


1  

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.

我正在努力解决同样的问题,运行一个设置变量的shell脚本,然后想要使用shared-pom中的变量。目标是使用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:

使用$ {env.foo}或$ {env.FOO}对我不起作用。 Maven只是没找到变量。有效的是将变量作为Maven中的命令行参数传递。这是设置:

  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.

    在shell脚本中设置变量。如果您在子脚本中启动Maven,请确保变量已设置,例如使用source ./maven_script.sh从父脚本中调用它。

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

    在shared-pom中,创建一个抓取环境变量的命令行参数:

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

    在com.google.code.maven-replacer-plugin中,设置替换值$ {foo}。

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

    在我调用maven的shell脚本中,将其添加到命令:-Dfoo = $ foo