【问题标题】:Making Java step in Ant script fail the build在 Ant 脚本中制作 Java 步骤使构建失败
【发布时间】:2011-04-17 00:16:09
【问题描述】:

如果这个问题很愚蠢,请原谅,我只是在 Ant 和 Java 上研究一些 CI 解决方案的第二天,几乎没有 Ant 或 Java 知识。

因此,如果(我的)java 程序作为构建中的一个步骤运行,决定构建必须失败,我希望构建失败。

我想在 Java 程序中抛出一个未处理的异常或使用 System.exit() 关闭 JVM,但它们看起来很讨厌。

如果 java 步骤决定应该这样做,是否有一种很好的 ant 使构建失败的方法?

【问题讨论】:

  • "...它们看起来很讨厌..." - 请描述您对通知用户程序失败的“好”方式的想法。
  • 类似于我的 java 应用程序退出并返回给 JVM 的代码,JVM 将它传递给 Ant 和 Ant 操作失败?
  • 所以当你调用 System.exit() 时返回 -1 并继续。

标签: java ant


【解决方案1】:

对于<java> 任务,有一个属性failonerror。如果将其设置为yes(或true),则如果进程返回的不是0,则构建将失败。

问题是为了从 java 调用返回一些值,这个调用必须System.exit(value)。为了不杀死你的蚂蚁,你还需要提供fork=true 才能在新的 JVM 中运行。

因此,java 调用可能如下所示:

<java jar="..."
      fork="yes"
      failonerror="yes">
</java>

当然,您也可以让您的 Java 程序实现 Ant 任务 API 并将其作为适当的 ant 任务加载/调用。然后它可以自己决定做什么(并且也将更具可配置性)。

【讨论】:

  • 我认为这正是我所追求的,然后我意识到我已经使用了“failonerror”(我从一个示例中复制了它并忘记了它在那里)。我不太热衷于强制 JVM 关闭 Ant 导致构建失败,但是通过 failonerror 显式处理它以及您提到的分叉,我很高兴。
【解决方案2】:

Ant 手册显示了一个名为 Fail 的内置任务,您可以配置特定条件以使构建失败。

<fail message="Files are missing.">
    <condition>
        <not>
            <resourcecount count="2">
                <fileset id="fs" dir="." includes="one.txt,two.txt"/>
            </resourcecount>
        </not>
    </condition>
</fail>

你可能想看看那个。

【讨论】:

  • 我知道 ,但不知道 所以感谢您的领导。调查一下, 条件测试了 的返回码,所以那里有承诺,谢谢。
【解决方案3】:

Ant 应该是直截了当的。如果特定步骤不成功,它确实会导致构建失败。我认为你想要的行为已经是内置的。

至于您的自定义步骤,我的建议是暂时在 Ant 之外找到一种方法来执行此操作,同时让 CI 流程的其余部分正常工作。最好取得这么大的进步,而不是在一个细节上陷入困境。

如果您描述您的程序正在做什么,这可能会有所帮助。也许有更好的方法来完成您的需要。

更新:我认为你不应该走这条路。您使用 CC 运行的测试应该是单元测试。如果您必须打包和部署应用程序进行测试,我会调用这些集成测试。作为 QA 步骤的一部分单独运行它们,而不是构建。

你用 Selenium 做的事情是正确的;我喜欢你的严谨和努力。但我建议只使用 CC 运行单元测试,将应用程序打包并部署到 QA 服务器,然后将 Selenium 测试作为 JUnit 运行。它们是按脚本编写的,而且速度很快。

我还想知道使用 Selenium 检查 UI 中的小部件位置是否明智。这对我来说似乎很脆弱;最好留给人类。

这是一个我经常重复使用的通用 Ant 构建。随意将其用作参考。不断告诉自己“这应该很简单”。如果太难了,那你就做错了。

<?xml version="1.0" encoding="UTF-8"?>
<project name="xslt-converter" basedir="." default="package">

    <property name="version" value="1.6"/>
    <property name="haltonfailure" value="no"/>

    <property name="out" value="out"/>

    <property name="production.src" value="src"/>
    <property name="production.lib" value="lib"/>
    <property name="production.resources" value="config"/>
    <property name="production.classes" value="${out}/production/${ant.project.name}"/>

    <property name="test.src" value="test"/>
    <property name="test.lib" value="lib"/>
    <property name="test.resources" value="config"/>
    <property name="test.classes" value="${out}/test/${ant.project.name}"/>

    <property name="exploded" value="out/exploded/${ant.project.name}"/>
    <property name="exploded.classes" value="${exploded}/WEB-INF/classes"/>
    <property name="exploded.lib" value="${exploded}/WEB-INF/lib"/>

    <property name="reports.out" value="${out}/reports"/>
    <property name="junit.out" value="${reports.out}/junit"/>
    <property name="testng.out" value="${reports.out}/testng"/>

    <path id="production.class.path">
        <pathelement location="${production.classes}"/>
        <pathelement location="${production.resources}"/>
        <fileset dir="${production.lib}">
            <include name="**/*.jar"/>
            <exclude name="**/junit*.jar"/>
            <exclude name="**/*test*.jar"/>
        </fileset>
    </path>

    <path id="test.class.path">                            
        <path refid="production.class.path"/>
        <pathelement location="${test.classes}"/>
        <pathelement location="${test.resources}"/>
        <fileset dir="${test.lib}">
            <include name="**/junit*.jar"/>
            <include name="**/*test*.jar"/>
        </fileset>
    </path>

    <path id="testng.class.path">
        <fileset dir="${test.lib}">
            <include name="**/testng*.jar"/>
        </fileset>
    </path>

    <available file="${out}" property="outputExists"/>

    <target name="clean" description="remove all generated artifacts" if="outputExists">
        <delete dir="${out}" includeEmptyDirs="true"/>
        <delete dir="${reports.out}" includeEmptyDirs="true"/>
    </target>

    <target name="create" description="create the output directories" unless="outputExists">
        <mkdir dir="${production.classes}"/>
        <mkdir dir="${test.classes}"/>
        <mkdir dir="${reports.out}"/>
        <mkdir dir="${junit.out}"/>
        <mkdir dir="${testng.out}"/>
        <mkdir dir="${exploded.classes}"/>
        <mkdir dir="${exploded.lib}"/>
    </target>

    <target name="compile" description="compile all .java source files" depends="create">
        <!-- Debug output
                <property name="production.class.path" refid="production.class.path"/>
                <echo message="${production.class.path}"/>
        -->
        <javac srcdir="src" destdir="${out}/production/${ant.project.name}" debug="on" source="${version}">
            <classpath refid="production.class.path"/>
            <include name="**/*.java"/>
            <exclude name="**/*Test.java"/>
        </javac>
        <javac srcdir="${test.src}" destdir="${out}/test/${ant.project.name}" debug="on" source="${version}">
            <classpath refid="test.class.path"/>
            <include name="**/*Test.java"/>
        </javac>
    </target>

    <target name="junit-test" description="run all junit tests" depends="compile">
        <!-- Debug output
                <property name="test.class.path" refid="test.class.path"/>
                <echo message="${test.class.path}"/>
        -->
        <junit printsummary="yes" haltonfailure="${haltonfailure}">
            <classpath refid="test.class.path"/>
            <formatter type="xml"/>
            <batchtest fork="yes" todir="${junit.out}">
                <fileset dir="${test.src}">
                    <include name="**/*Test.java"/>
                </fileset>
            </batchtest>
        </junit>
        <junitreport todir="${junit.out}">
            <fileset dir="${junit.out}">
                <include name="TEST-*.xml"/>
            </fileset>
            <report todir="${junit.out}" format="frames"/>
        </junitreport>
    </target>

    <taskdef resource="testngtasks" classpathref="testng.class.path"/>
    <target name="testng-test" description="run all testng tests" depends="compile">
        <!-- Debug output
                <property name="test.class.path" refid="test.class.path"/>
                <echo message="${test.class.path}"/>
        -->
        <testng classpathref="test.class.path" outputDir="${testng.out}" haltOnFailure="${haltonfailure}" verbose="2" parallel="methods" threadcount="50">
            <classfileset dir="${out}/test/${ant.project.name}" includes="**/*.class"/>
        </testng>
    </target>

    <target name="exploded" description="create exploded deployment" depends="testng-test">
        <copy todir="${exploded.classes}">
            <fileset dir="${production.classes}"/>
        </copy>
        <copy todir="${exploded.lib}">
            <fileset dir="${production.lib}"/>
        </copy>
    </target>

    <target name="package" description="create package file" depends="exploded">
        <jar destfile="${out}/${ant.project.name}.jar" basedir="${production.classes}" includes="**/*.class"/>
    </target>

</project>

【讨论】:

  • 我正在做的是让 Ant 检查 web 应用程序和测试应用程序的代码,编译两者,然后让测试应用程序 (Java) 通过 Selenium 2 驱动 web 应用程序,采取屏幕快照并将其与已知蓝图进行比较。如果该图像比较步骤失败,那么我希望构建失败。一切正常,我只是不太喜欢我用过的任何一种方法。
  • 回复:您的更新:最初将单个集成放在 CI 中,因为目前只有大约 30% 的单元测试覆盖率,并且在重构剩余代码时会立即捕获主流中的中断以适应测试。然而,鉴于这样的集成测试是完全自动化的并且服务器负载不是问题,我认为没有理由在这个单元测试编写阶段之后将其从 CI 中删除。当然,它永远不会取代后期测试阶段。
猜你喜欢
  • 2017-05-10
  • 2012-05-28
  • 2015-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-17
  • 1970-01-01
  • 2022-01-17
相关资源
最近更新 更多