【问题标题】:Environment Variable with Maven使用 Maven 的环境变量
【发布时间】:2011-07-27 12:15:27
【问题描述】:

我已将一个项目从 Eclipse 移植到 Maven,我需要设置一个环境变量以使我的项目正常工作。

在 Eclipse 中,我转到“运行 -> 运行配置”,然后在“环境”选项卡下,将“WSNSHELL_HOME”设置为值“conf”。

如何使用Maven 做到这一点?

【问题讨论】:

  • 我建议让您的项目在没有环境变量的情况下运行。然后转到 maven。
  • 如果环境变量对您的系统有用并且我很确定我的解决方案有效(我自己使用它),那么没有理由跳过环境变量的使用。请参阅我的提示'System.getenv'/'System.getProperty'。

标签: java maven environment-variables


【解决方案1】:

你可以在命令行中传递它,就像

mvn -DmyVariable=someValue install

[更新]请注意,参数的顺序很重要 - 您需要在命令之前指定任何选项[/Update]

在 POM 文件中,您可以将系统变量(在命令行或 pom 中指定)称为${myVariable},将环境变量称为${env.myVariable}(感谢评论者的更正。)

更新2

好的,所以你想将你的系统变量传递给你的测试。如果 - 正如我所假设的 - 您使用 Surefire plugin 进行测试,最好在 pom 中指定所需的系统变量,在您的 plugins 部分,例如

<build>
    <plugins>
        ...
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            ...
            <configuration>
                ...
                <systemPropertyVariables>
                    <WSNSHELL_HOME>conf</WSNSHELL_HOME>
                </systemPropertyVariables>
            </configuration>
        </plugin>
        ...
    </plugins>
</build>

【讨论】:

  • 我需要启动 'mvn test',它似乎不起作用,因为如果我写 mvn test -DWSNSHELL_HOME=conf 我仍然得到异常 java.io.FileNotFoundException: null/wsnshell。 conf(没有这样的文件或目录)
  • @Janky,换个方式试试,即mvn -DWSNSHELL_HOME=conf test
  • nope... :( 我仍然得到 java.io.FileNotFoundException: null/wsnshell.conf (No such file or directory)... 在代码中我以这种方式读取变量.. System.getenv("WSNSHELL_HOME"); 对吗?
  • @Janky,等一下,您是直接从 Maven 构建运行应用程序,还是...?
  • 你们两个混淆了环境变量和系统属性。 -D 命令选项仅设置系统属性。
【解决方案2】:

-D 属性不会可靠地从 surefire-pluging 传播到您的测试(我不知道为什么它适用于 eclipse)。在命令行上使用 maven 时,使用 argLine 属性来包装您的属性。这会将它们传递给您的测试

mvn -DargLine="-DWSNSHELL_HOME=conf" test

使用System.getProperty 读取代码中的值。看看this 的帖子,了解System.getenvSytem.getProperty 的区别。

【讨论】:

  • 您应该使用 System.getProperty("WSNSHELL_HOME") 而不是使用 System.getenv 来读取属性。
  • 完美,在花了很多时间尝试其他事情之后,这个对我有用。
  • 如果你在 pom.xml 中为 surefire 插件配置了 argLine 参数,则添加 ${argLine} 占位符,否则命令行中的参数将不会被拾取。示例:&lt;argLine&gt;-Duser.timezone=UTC ${argLine}&lt;/argLine&gt;
  • 这对我有用,谢谢!为 maven 测试配置 sys-props 的好工具。乍一看还是很迷惑,为什么不能直接使用,需要额外的参数。
【解决方案3】:

有一个名为properties-maven-plugin 的maven 插件,它提供了一个目标set-system-properties 来设置系统变量。如果您有一个包含所有这些属性的文件,这将特别有用。因此,您可以读取属性文件并将其设置为系统变量。

【讨论】:

    【解决方案4】:

    另一种解决方案是在${user.home}/.mavenrc(或Windows 上的%HOME%\mavenrc_pre.bat)中设置MAVEN_OPTS(或其他环境变量)。

    从 Maven 3.3.1 开始有 new possibilities to set mvn command line parameters,如果这是你真正想要的:

    • ${maven.projectBasedir}/.mvn/maven.config
    • ${maven.projectBasedir}/.mvn/jvm.config

    【讨论】:

      【解决方案5】:

      您可以将 maven 命令包装在 bash 脚本中:

      #!/bin/bash
      
      export YOUR_VAR=thevalue
      mvn test
      unset YOUR_VAR
      

      【讨论】:

      • 令人难以置信的是,这是使用 Surefire 插件对我完全有效的唯一方法。在我在 POM 中设置的 10 个左右的环境变量中,测试运行时看不到其中的两个。即使我将它们设置在 shell 中然后mvn install,它们仍然以某种方式被遮蔽。但是,如果我从 shell 脚本运行,它工作正常!我希望我明白为什么会发生这种情况。
      • 我相信这应该是一个公认的答案,因为这是最简单的解决方案
      【解决方案6】:

      Maven中的环境变量,可以在下面设置。

      http://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html#environmentVariables http://maven.apache.org/surefire/maven-failsafe-plugin/integration-test-mojo.html#environmentVariables

      <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-failsafe-plugin</artifactId>
          ...
          <configuration>
              <includes>
                  ...
              </includes>
              <environmentVariables>
                  <WSNSHELL_HOME>conf</WSNSHELL_HOME>
              </environmentVariables>
          </configuration>
      </plugin>
      

      【讨论】:

        【解决方案7】:

        在您的代码中添加:

        System.getProperty("WSNSHELL_HOME")

        从 maven 命令修改或添加 value 属性:

        mvn clean test -DargLine=-DWSNSHELL_HOME=yourvalue
        

        如果您想在 Eclipse 中运行它,请在您的 Debug/Run 配置中添加 VM 参数

        • 转到运行 -> 运行配置
        • 选择选项卡参数
        • 在虚拟机参数部分添加

        -DWSNSHELL_HOME=你的价值

        你不需要修改 POM

        【讨论】:

          【解决方案8】:

          根据@Kevin 的回答中的文档,下面的文档对我有用,用于使用 maven sure-fire 插件设置环境变量

          <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <configuration>
              <environmentVariables>
                  <WSNSHELL_HOME>conf</WSNSHELL_HOME>
              </environmentVariables>
          </configuration>
          

          【讨论】:

            【解决方案9】:

            您可以通过_JAVA_OPTIONS 变量传递一些参数。

            例如,为 maven 代理标志定义一个变量,如下所示:

            _JAVA_OPTIONS="-Dhttp.proxyHost=$http_proxy_host -Dhttp.proxyPort=$http_proxy_port -Dhttps.proxyHost=$https_proxy_host -Dhttps.proxyPort=$http_proxy_port"
            

            然后使用mvn clean install(它会自动接_JAVA_OPTIONS)。

            【讨论】:

              【解决方案10】:

              我建议使用神奇的工具direnv。使用它,您可以在 cd 进入项目后注入环境变量。这些步骤对我有用:

              .envrc 文件

              source_up
              dotenv
              

              .env 文件

              _JAVA_OPTIONS="-DYourEnvHere=123"
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2013-05-04
                • 1970-01-01
                • 2012-01-26
                • 1970-01-01
                • 2013-12-02
                相关资源
                最近更新 更多