为什么 Maven 不识别我的环境变量?

Why isn't Maven recognizing my environment variables?

我在 Mac Yosemite 和 Java 上使用 Maven 3.3.3 8. 我在 /etc/profile 中定义了一个变量 JBOSS_HOME ] 文件 …

JBOSS_HOME=/opt/wildfly-10.0.0.CR2 

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

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

然而,当我 运行 我的 Maven 脚本(来自同一个 shell)时,我无法访问这个变量的值。下面

<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 识别我的环境变量?

您应该删除行 <property environment="env" />。正如 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>

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

export JBOSS_HOME=$JBOSS_HOME

然后 Maven 脚本获取了环境变量。直观,嗯?