【问题标题】:Why isn't Maven recognizing my environment variables?为什么 Maven 不能识别我的环境变量?
【发布时间】:2015-10-28 21:52:47
【问题描述】:

我在带有 Java 8 的 Mac Yosemite 上使用 Maven 3.3.3。我在 /etc/profile 文件中定义了一个变量 JBOSS_HOME……

JBOSS_HOME=/opt/wildfly-10.0.0.CR2 

在我的终端(使用 bash shell)中,我可以看到这个变量的值……

davea$ echo $JBOSS_HOME
/opt/wildfly-10.0.0.CR2

但是,当我(从同一个 shell)运行 Maven 脚本时,我无法访问该变量的值。下面

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.8</version>
    <dependencies>
        <dependency>
            <groupId>ant-contrib</groupId>
            <artifactId>ant-contrib</artifactId>
            <version>20020829</version>
        </dependency>
    </dependencies>
    <executions>
        <execution>
            <id>create-dodeploy-file</id>
            <phase>package</phase>
            <configuration>
                <target>
                    <taskdef resource="net/sf/antcontrib/antcontrib.properties" />
                    <property environment="env" />
                    <echo message="jboss home: ${env.JBOSS_HOME}" />

生产……

 [echo] jboss home: ${env.JBOSS_HOME}

什么给了?如何让 Maven 识别我的环境变量?

【问题讨论】:

标签: maven environment-variables maven-3 maven-antrun-plugin


【解决方案1】:

您应该删除&lt;property environment="env" /&gt; 行。正如Ant documentation 中定义的那样,这将读取系统环境变量(而不是用户环境变量)并将它们存储在属性中,前缀为"env"。这将隐藏 Maven 定义的 ${env.*} 属性。

因此,此配置将起作用:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.8</version>
    <dependencies>
        <dependency>
            <groupId>ant-contrib</groupId>
            <artifactId>ant-contrib</artifactId>
            <version>20020829</version>
        </dependency>
    </dependencies>
    <executions>
        <execution>
            <id>create-dodeploy-file</id>
            <phase>package</phase>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
                <target>
                    <taskdef resource="net/sf/antcontrib/antcontrib.properties" />
                    <echo message="jboss home: ${env.JBOSS_HOME}" />
                </target>
            </configuration>
        </execution>
    </executions>
</plugin>

【讨论】:

  • 嗨,我删除了 '' 这一行,但 Maven 仍然没有选择 JBOSS_HOME 环境变量。
  • @Dave 嗯,我刚刚在我的 Windows 机器上测试了它,它工作正常。你的输出是什么?
  • 在我的 Mac 上,输出和以前一样,“[echo] jboss home: ${env.JBOSS_HOME}”
  • @Dave 我使用我正在使用的完整插件配置进行了编辑。当使用mvn clean install 启动时,它会输出正确的值。
  • 谢谢。仍然没有骰子,所以这可能与 Maven 在 Windows 和 Mac 上运行方式之间的差异有关。我确实找到了一个适用于我的环境的解决方案,我将在稍后发布。
【解决方案2】:

当我在 ~/.profile 文件的顶部添加这一行时

export JBOSS_HOME=$JBOSS_HOME

然后 Maven 脚本选择了环境变量。很直观吧?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-13
    • 1970-01-01
    • 2013-02-06
    • 1970-01-01
    • 2021-12-29
    • 2021-04-20
    • 2021-09-09
    相关资源
    最近更新 更多