【问题标题】:Can Apache ant target detect whether property was overridden on command line?Apache ant 目标能否检测到属性是否在命令行上被覆盖?
【发布时间】:2017-10-17 11:10:14
【问题描述】:

我想根据属性的来源更改我的一个 ant 目标中的逻辑。我们的 ant 目标的初始化会导入一个属性文件,但是当我们想要覆盖一个属性时,我们会在命令行中指定它。 ant 目标是否有可能知道属性是来自初始属性文件还是来自命令行?

如果属性文件有属性“my.property”然后命令行显示:

ant -buildfile buildthis.xml my.target.to.call  -Dmy.property=overridesfilevalue

“my.target.to.call”是否能够根据“my.property”是否从命令行传入来检测和定义逻辑?

【问题讨论】:

    标签: ant


    【解决方案1】:

    Ant 有一个名为isset 的条件,它将检查是否设置了属性。您可以在加载属性文件之前简单地运行此条件,然后将构建逻辑基于结果。

    例子:

    <condition property="property.override.detected">
        <isset property="property.to.override" />
    </condition>
    
    <property file="build.properties" />
    
    <target name="do-this-if-the-property-was-overridden" if="property.override.detected">
        ...
    </target>
    
    <target name="do-this-if-the-property-was-not-overridden" unless="property.override.detected">
        ...
    </target>
    
    <target
        name="default"
        depends="
            do-this-if-the-property-was-overridden,
            do-this-if-the-property-was-not-overridden"
    />
    

    【讨论】:

    • 太棒了!我明天试试。感谢您的帮助!
    • 按照您的建议工作。再次感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-29
    • 1970-01-01
    • 1970-01-01
    • 2017-06-24
    • 2022-11-23
    相关资源
    最近更新 更多