开发者

testing last line of log files using ANT

I have couple of bat files which run further files to build the project. its a hectic process. I am writign a master ant build file to do all ..

T开发者_Python百科here is an BAT files, which print BUILD SUCCESSFUL on console when it runs successfully. The BUILD SUCCESSFUL is the last line of the console out put.

I have written this in my ant script so far

<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>

I wana test if the last line of test.log file is BUILD SUCCESSFUL or not. if it is then execute next task, otherwise FAIL.

I have tried to use fail task but din help. could someone guide me?


If you want the last line, use a filterchain with tailfilter lines="1", see Ant Manual FilterChains
afterwards use some Ant Addon with if/else construct like Flaka or Antcontrib to check the property =

<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>

or use the standard ant way with condition =

<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>


You can examine the buildC.log property using a nested condition of a fail task:

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

You may need to tweak the pattern a little to get it working, depends on exactly what is written by the test.bat script. The matches by default applies to the whole string, so the above will only match if the success message constitutes the last line of the file.

You might also consider capturing the output from test.bat using the exec task outputproperty attribute.

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

(Note the shell redirect has been removed in the arg line.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜