Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使Ant脚本中的Java步骤失败_Java_Ant - Fatal编程技术网

使Ant脚本中的Java步骤失败

使Ant脚本中的Java步骤失败,java,ant,Java,Ant,请原谅,如果这个问题是愚蠢的,我只是在第二天的蚂蚁和Java黑客一起一些CI解决方案几乎没有知识的蚂蚁或Java 因此,如果(我的)java程序作为构建中的一个步骤运行,决定构建必须失败,那么我希望构建失败 我曾想过在Java程序中抛出一个未经处理的异常,或者使用System.exit()关闭JVM,但它们看起来相当糟糕 如果java步骤决定ant应该失败,那么有没有一种很好的方法可以让ant失败呢?ant应该很简单。如果某个特定步骤没有成功,它确实会使构建失败。我认为你想要的行为已经是内在的了

请原谅,如果这个问题是愚蠢的,我只是在第二天的蚂蚁和Java黑客一起一些CI解决方案几乎没有知识的蚂蚁或Java

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

我曾想过在Java程序中抛出一个未经处理的异常,或者使用System.exit()关闭JVM,但它们看起来相当糟糕


如果java步骤决定ant应该失败,那么有没有一种很好的方法可以让ant失败呢?

ant应该很简单。如果某个特定步骤没有成功,它确实会使构建失败。我认为你想要的行为已经是内在的了

至于您的自定义步骤,我的建议是,现在在Ant之外找到一种方法来实现这一点,同时让其余的CI流正常工作。与其因为一个细节而陷入困境,不如取得这么大的进展

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

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

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

我还想知道使用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手册显示了一个名为的内置任务,您可以使用特定条件对其进行配置,以使构建失败

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

您可能需要研究一下这个问题。

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

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

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

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


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

“…它们看起来很糟糕…”-请描述一下你的想法,一种“好”的方法可以通知用户程序失败。比如我的java应用程序退出时会返回到JVM,JVM将其传递给Ant并由Ant执行操作失败?因此,在调用System.exit()并继续操作时返回-1。我要做的是让Ant签出web应用程序和测试应用程序的代码,编译它们,然后让测试应用程序(Java)通过Selenium 2驱动web应用程序,拍摄屏幕快照并将其与已知蓝图进行比较。如果图像比较步骤失败,那么我希望构建失败。一切都很好,我只是不太喜欢我使用的任何一种方法。re:your update:,最初单一集成是放在CI中的,因为目前只有大约30%的单元测试覆盖率,当剩余的代码被重构以适应测试时,主流程中的中断会立即被捕获。然而,考虑到这样的集成测试是完全自动化的,并且服务器负载不是问题,我认为没有理由在单元测试编写阶段之后将其从CI中删除。当然,它永远不会取代后面的测试阶段。我知道,但不知道,谢谢你的领导。仔细研究,条件测试了一个的返回代码,所以这里有承诺,谢谢。我想这正是我想要的,然后我意识到我已经使用了“failonerror”(我从一个示例中复制了它,但忘了它在那里)。我不太喜欢构建失败,因为它迫使JVM关闭Ant,但是failonerror显式地处理它和您提到的fork,我很高兴。