java - ANT 脚本中的条件任务执行

标签 java ant

我的要求非常简单,我有一个 ANT 任务,它在内部处理异常并且不抛出任何异常,而是向控制台抛出自定义消息[这些不是异常]。下面显示了一个示例,其中包含测试“指定名称的工作区不存在”。

enter image description here

我的要求是,如果除了“构建成功”之外还有任何此类消息,我应该确保我的 ANT 脚本失败,这样它就不会继续下去。但我无法这样做,因为我不知道如何读取写入控制台的自定义消息。

我尝试探索“记录”任务,但没有成功,因为该日志仅写入控制台而不写入文件(不知道为什么)。但即使它被写入文件,我也应该理想地读取文件的每一行以找出是否存在特定文本。

是否有一种简单的方法可以尝试从控制台读取之前执行过的内容?

<target name="build">
    <record name="test.txt" action="start" append="true" loglevel="verbose" />
    <echo>Welcome to Apache Ant! Building the project in Cordys Middleware</echo>
    <property environment="env"/>
    <property name="bop.install.dir" value="${env.CORDYS_HOME}"/>
    <exec executable="${bop.install.dir}/components/cws/scripts/linux/CWSPackage.sh" failonerror="true" resultproperty="output">        
        <env key="CLASSPATH" value="/opt/Cordys/Oracle_Jar/ojdbc6.jar"/>
        <arg value="${ORG_NAME}"/>
        <arg value="${WORKSPACE_NAME}"/>
        <arg value="${PROJECT_NAME}"/>      
    </exec>
    <echo>Finishing the build</echo>
    <record name="test.txt" action="stop"/>
    <echo>${output}</echo>
    <fail>Something wrong here.</fail> <!-- I want to throw this error conditionally -->
</target>

最佳答案

您要查找的是 exec 任务的 outputproperty 属性。

你可以这样做:

<exec executable="${my.executable}" outputproperty="exec.output">
    <arg value="${my.arg}" />
</exec>

<fail message="Invalid output from exec task">
    <condition>
        <contains string="${exec.output}" substring="The workspace with the specified string does not exist." />
    </condition>
</fail>

多个条件(允许 boolean 值的任何复杂程度):

<fail message="Invalid output from exec task">
    <condition>
        <and>
            <not>
                <contains string="${exec.output}" substring="SUCCESS" />
            </not>
            <or>
                <contains string="${exec.output}" substring="ERROR" />
                <contains string="${exec.output}" substring="FAILED" />
            <or>
        </and>
    </condition>
</fail>

正则表达式:

<fail message="Invalid output from exec task">
    <condition>
        <matches string="${exec.output}" pattern="The .* does not exist." />
    </condition>
</fail>

关于java - ANT 脚本中的条件任务执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47904649/

相关文章:

java - 如何在引用可变对象时使类不可变?

java - 如何将 Eclipse java 项目部署到 jar 文件中

ant - 使用 bash 命令结果设置 ant 属性

java - 在 netbeans 中构建项目时 Ivy 错误

gradle - Ant 替换 token -Gradle

java - 如何抑制 Eclipse 3.5 的死代码警告

JavaFX:使用 sum 函数对 TableView 进行分组

java - 添加模块描述符时没有名为 'entityManagerFactory' 的 bean 可用

java - 从 java 项目运行 build.xml

java - ANT 中的目标是什么