使用ant,检查文件中是否找到特定字符串

Anu*_*kar 6 ant

我的要求是,使用waitfor条件,ant应该定期检查日志文件中是否显示字符串"Build Successful".如果找到该字符串,则应执行特定操作.

mar*_*ton 6

以下是您可能执行此操作的一种方法示例:

<target name="wait-for">
    <waitfor maxwait="15" maxwaitunit="second" timeoutproperty="build.timeout">
        <resourcecontains resource="build.log" substring="Build Successful" />
    </waitfor>
    <antcall target="build-success" />
</target>

<target name="build-success" depends="build-fail" unless="build.timeout">
    <echo message="Success" />
</target>
<target name="build-fail" if="build.timeout">
    <echo message="Fail" />
</target>
Run Code Online (Sandbox Code Playgroud)

使用resourcecontains条件在命名资源中查找字符串 - 在本例中为"build.log"文件.如果在指定时间内未找到,build.timeout则设置该属性.有两个目标,一个在找到字符串时运行,另一个如果没有.在"目标"属性 if,unless以及depends用来做的if-else逻辑的需要.如果您只需要在成功失败的情况下采取行动,您可以略微简化.