如何根据环境变量的值执行 ant 目标?
How can I execute an ant target depending on the value of an environment variable?
我想在两台不同的计算机上执行一个 ant 脚本。根据计算机的名称,应执行两个目标之一。以下无效:
<project name="import" default="all">
<property environment="env"/>
<target name="staging" if="${env.COMPUTERNAME}='STG'">
<echo>executed on staging</echo>
</target>
<target name="production" if="${env.COMPUTERNAME}='PRD'">
<echo>executed on production</echo>
</target>
<target name="all" depends="staging,production" description="STG or PRD"/>
</project>
据我了解,"if" 只能与 属性 一起使用,它会检查是否设置了 属性。但是有没有办法根据 属性 的值来创建条件?
我建议编写一个 "init" 目标,为以后的构建步骤设置您需要的任何条件,如果某些必需的属性未评估为预期的结果,构建也会失败。
例如:
<target name="all" depends="staging,production,init" />
<target name="staging" if="staging.environment" depends="init">
<echo message="executed on staging" />
</target>
<target name="production" if="production.environment" depends="init">
<echo message="executed on production" />
</target>
<target name="init">
<condition property="staging.environment">
<equals arg1="${env.COMPUTERNAME}" arg2="STG" />
</condition>
<condition property="production.environment">
<equals arg1="${env.COMPUTERNAME}" arg2="PRD" />
</condition>
<fail message="Neither staging nor production environment detected">
<condition>
<not>
<or>
<isset property="staging.environment" />
<isset property="production.environment" />
</or>
</not>
</condition>
</fail>
</target>
我想在两台不同的计算机上执行一个 ant 脚本。根据计算机的名称,应执行两个目标之一。以下无效:
<project name="import" default="all">
<property environment="env"/>
<target name="staging" if="${env.COMPUTERNAME}='STG'">
<echo>executed on staging</echo>
</target>
<target name="production" if="${env.COMPUTERNAME}='PRD'">
<echo>executed on production</echo>
</target>
<target name="all" depends="staging,production" description="STG or PRD"/>
</project>
据我了解,"if" 只能与 属性 一起使用,它会检查是否设置了 属性。但是有没有办法根据 属性 的值来创建条件?
我建议编写一个 "init" 目标,为以后的构建步骤设置您需要的任何条件,如果某些必需的属性未评估为预期的结果,构建也会失败。
例如:
<target name="all" depends="staging,production,init" />
<target name="staging" if="staging.environment" depends="init">
<echo message="executed on staging" />
</target>
<target name="production" if="production.environment" depends="init">
<echo message="executed on production" />
</target>
<target name="init">
<condition property="staging.environment">
<equals arg1="${env.COMPUTERNAME}" arg2="STG" />
</condition>
<condition property="production.environment">
<equals arg1="${env.COMPUTERNAME}" arg2="PRD" />
</condition>
<fail message="Neither staging nor production environment detected">
<condition>
<not>
<or>
<isset property="staging.environment" />
<isset property="production.environment" />
</or>
</not>
</condition>
</fail>
</target>