【问题标题】:testing last line of log files using ANT使用 ANT 测试日志文件的最后一行
【发布时间】:2011-07-07 20:50:27
【问题描述】:

我有几个 bat 文件,它们运行更多文件来构建项目。这是一个忙碌的过程。我正在编写一个主 ant 构建文件来完成所有工作..

有一个BAT文件,成功运行后会在控制台打印BUILD SUCCESSFUL。 BUILD SUCCESSFUL 是控制台输出的最后一行。

到目前为止,我已经在我的 ant 脚本中写了这个

<project name="MyProject" basedir=".">
    <property name="buildC" value="${basedir}/build-C" />
    <exec dir="${buildC}" executable="cmd" os="Windows XP">
        <arg line="/c test.bat > test.log"/>
    </exec>

    <loadfile property="buildC.log" srcFile="${buildC}/test.log">
    </loadfile>

</project>

我想测试test.log 文件的最后一行是否BUILD SUCCESSFUL。如果是则执行下一个任务,否则失败。

我曾尝试使用 fail 任务,但需要帮助。有人可以指导我吗?

【问题讨论】:

    标签: ant automation batch-file


    【解决方案1】:

    如果您想要最后一行,请使用带有 tailfilter lines="1" 的过滤器链,请参阅Ant Manual FilterChains
    然后使用带有 if/else 构造的一些 Ant 插件,例如 FlakaAntcontrib 来检查属性=

    <project xmlns:fl="antlib:it.haefelinger.flaka">
     <loadfile srcfile="your.log" property="buildsuccess">
      <filterchain>
       <tailfilter lines="1" />
        <!-- also possible if needed -->
        <trim/>
        <striplinebreaks/>
        <!-- also possible if needed // -->
      </filterchain>
     </loadfile>
    
     <fl:choose>
      <fl:when test="'${buildsuccess}' eq 'BUILD SUCCESSFUL'">
       <echo>execute new task..</echo>
      </fl:when>
      <fl:otherwise>
       <fail message="Houston we have a problem.."/>
      </fl:otherwise>
    </fl:choose>
    </project>
    

    或使用带有条件的标准蚂蚁方式 =

    <project default="main">
    
     <target name="checklog">
      <loadfile srcfile="props.txt" property="buildsuccess">
       <filterchain>
        <tailfilter lines="1" />
        <!-- // also possible if needed -->
        <trim/>
        <striplinebreaks/>
        <!-- also possible if needed // -->
       </filterchain>
      </loadfile>
    
      <condition property="buildOK">
       <equals arg1="${buildsuccess}" arg2="BUILD SUCCESSFUL"/>
      </condition>    
     </target>
    
     <target name="whatever">
      <fail message="Houston we have a problem.." unless="buildOK"/>   
      <!-- if not failed then you'll go on with your other tasks here .. -->    
     </target>
    
     <target name="main" depends="checklog,whatever"/>
    </project>
    

    【讨论】:

      【解决方案2】:

      您可以使用fail 任务的嵌套条件检查buildC.log 属性:

      <fail message="test.bat failed">
         <condition>
             <not>
                 <matches pattern="${line.separator}BUILD SUCCESSFUL$" string="${buildC.log}" />
             </not>
         </condition>
      </fail>
      

      您可能需要稍微调整模式以使其正常工作,具体取决于 test.bat 脚本所写的内容。 matches 默认情况下适用于整个字符串,因此仅当成功消息构成文件的最后一行时,上述内容才会匹配。

      您还可以考虑使用exec 任务outputproperty 属性捕获来自test.bat 的输出。

      <exec dir="." executable="sh" outputproperty="buildC.log">
          <arg line="/c test.bat" />
      </exec>
      

      (请注意,arg 行中的 shell 重定向已被删除。)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-20
        相关资源
        最近更新 更多