如何在ant build.xml中将linux机器的标准主机名设置为环境变量

问题描述 投票:1回答:1

我有一只蚂蚁build.xml

我正在尝试将计算机的标准主机名(例如:“ abc-us.xyz.com”)设置为ant中的环境变量,然后尝试在各处使用该环境变量。

但是无法这样做。

到目前为止,我尝试过的是:

    <target name="get-fqdn">
    <echo>Getting the fully qualified hostname of the machine</echo>
    <exec executable="/bin/bash">
    <env key="FQDN" value="hostname-f"/>
    </exec>
    </target>

根据上面的代码,我希望它将hostname -f的值设置为环境变量FQDN

然后我有另一个目标:

    <target name="get-database" depends="init,get-fqdn">
    <echo>Getting the database*************</echo>
    <echo>$FQDN</echo>
    </target>

在第二个目标中,我正在尝试访问环境变量FQDN,已在第一个目标中设置。

但是它不起作用。请帮助

当我FQDN时,我没有在第二个目标中获得echo的值。

shell ant environment-variables build.xml
1个回答
0
投票
首先,任何<exec>调用都将在其自己的外壳中运行,因此,您在此调用中设置的任何环境变量都将仅存在于此外壳中,而不存在于父外壳(Ant)中。您需要一种方法来存储环境变量的值,这种方法将在shell调用过程中保持不变,例如在属性文件中:

<target name="get-fqdn"> <exec executable="/bin/bash"> <env key="FQDN" value="some_host.com"/> <arg value="-c"/> <arg line="echo FQDN=$FQDN > fqdn.properties"/> </exec> </target>

然后您要做的就是加载此属性文件,并使它包含的属性可用于Ant:

<target name="get-database" depends="get-fqdn"> <property file="fqdn.properties"/> <echo message="retrieved FQDN=${FQDN}"/> </target>

© www.soinside.com 2019 - 2024. All rights reserved.