Checking an environment variable using the Maven AntRun plugin

若如初见. 提交于 2019-12-23 20:28:50

问题


I'm trying to check and see if the MULE_HOME environment variable is set within the maven-antrun-plugin without success. Here's what I have so far:

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.7</version>
            <executions>
                <execution>
                    <id>mule-deploy</id>
                    <phase>install</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <target>
                            <taskdef resource="net/sf/antcontrib/antcontrib.properties"
                                     classpath="${settings.localRepository}/ant-contrib/ant-contrib/1.0b3/ant-contrib-1.0b3.jar"/>
                            <echo message="MULE_HOME is ${env.MULE_HOME}"/>
                            <if>
                                <isset property="env.MULE_HOME"/>
                                <then>
                                    <echo message="MULE_HOME is set"/>
                                </then>
                                <else>
                                    <echo message="MULE_HOME is not set"/>
                                </else>
                            </if>
                        </target>
                    </configuration>
                </execution>
            </executions>
</plugin>

The output is:

 [echo] MULE_HOME is /<my development path>/mule
 [echo] MULE_HOME is not set

What am I missing to check an environment variable?


回答1:


Java stores environment variables differently from system properties; System.getenv() vs. System.getProperties(). My guess is that maven isn't mapping environment variables into system properties which is what Ant is expecting with isset. Try creating a property in your POM:

<properties>
    <mulehome>${env.MULE_HOME}</mulehome>
<properties>

then use

<isset property="mulehome"/>



回答2:


After the taskdef line, define a:

<property environment="env"/>

My Ant memories are a bit rusty, but as far as I recall, you needed to define that first before being able to use ${env.FOO_BAR} variables. I hope this helps. :)



来源:https://stackoverflow.com/questions/8610980/checking-an-environment-variable-using-the-maven-antrun-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!