【问题标题】:Ant warning/failure when file / build.properties is missing?文件/ build.properties 丢失时出现Ant 警告/失败?
【发布时间】:2013-07-11 18:05:53
【问题描述】:

我的团队 ant 任务通过使用属性文件标签从 build.properties 文件中提取开发人员特定信息:

<property file="${user.home}/build.properties.txt" />

但是,当这个文件丢失时,ant 会继续运行。稍后在构建过程中,它会尝试访问尚未定义的属性并尝试以${user.name} 和其他类似错误登录到svn 服务器。这些错误很难调试,因为我使用的一些 ant 任务没有给出有用的错误消息。

我的主要问题是:如果 ant 找不到属性文件,有没有办法让它快速失败?

【问题讨论】:

    标签: ant build-process


    【解决方案1】:

    我认为你可以结合可用和失败:

    如果存在 File,则设置属性

    <available file="${user.home}/build.properties.txt" property="build.properties.present"/>
    

    如果属性未设置则失败

    <fail unless="build.properties.present"/>
    

    【讨论】:

    • 嘎。你也打过我吧。不过,就我个人而言,我不希望最终设置额外的属性,除非我要经常检查它们。
    • @developmentalinsanity 我也更喜欢你的解决方案 :)
    • 我想当 SO 允许时我会接受这个答案,但是,我没有设置 build.properties.present 属性,而是选择测试一个 应该 总是存在于文件中(老实说,它是该文件所需的主要道具)。我觉得这更干净,因为它允许我在您不想创建道具文件的情况下从命令行-Drequired.prop=val(无论在哪里)。
    【解决方案2】:

    您可以先添加显式检查。比如:

    <fail message="Missing build.properties">
      <condition>
        <not>
          <available file="${user.home}/build.properties.txt" />
        </not>
      </condition>
    </fail>
    

    可能会成功。

    【讨论】:

    • 虽然这是对我最初提出的问题(测试文件是否存在)的更正确答案,但在我看来,@oers 测试文件中设置的属性的解决方案是一个更干净的解决方案。谢谢。
    【解决方案3】:

    我建议,不要测试特定属性文件的存在,而是测试属性定义。这样,可以以不同的方式提供此属性(例如 -Duser.name=myname )。

    您可以在失败消息中给出建议的文件名。

    例如

    <fail message="user.name property is not set.
        It is usually defined in ${user.home}/build.properties.txt">
      <condition>
        <not><isset property="user.name"/></not>
      </condition>
    </fail>
    

    【讨论】:

    • 谢谢,我已经使用@oers 建议的语法来实现这个想法。我在 cmets 对他的回答中注意到了这一点,-D 语法对于 cronjobs 等非常有用,所以我赞成这种方法。
    【解决方案4】:

    使用目标“loadproperties”ant 任务而不是“property file” ="...">,因为如果文件丢失,后一个任务不会抱怨,这是您的情况。 在缺少属性的情况下,loadproperties 任务总是会导致构建失败。

    【讨论】:

      【解决方案5】:

      Ant Plugin Flaka 甚至更短=

      <project xmlns:fl="antlib:it.haefelinger.flaka">
          ...
       <fl:fail message="Houston we have a problem" test="!'${user.home}/build.properties.txt'.tofile.exists"/>
          ...
      </project>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-31
        • 1970-01-01
        • 1970-01-01
        • 2021-09-12
        • 2018-08-24
        相关资源
        最近更新 更多