請原諒,如果問題是愚蠢的,那麼我只是第二天關於Ant和Java一起攻擊一些CI解決方案,幾乎不知道Ant或Java的知識。在Ant腳本中創建Java步驟
所以我希望構建失敗,如果(我的)Java程序作爲構建內的步驟運行決定構建必須失敗。
我想在Java程序中拋出未處理的異常或使用System.exit()關閉JVM,但它們看起來很討厭。
如果一個java步驟決定它應該是否存在一個螞蟻失敗的好方法?
請原諒,如果問題是愚蠢的,那麼我只是第二天關於Ant和Java一起攻擊一些CI解決方案,幾乎不知道Ant或Java的知識。在Ant腳本中創建Java步驟
所以我希望構建失敗,如果(我的)Java程序作爲構建內的步驟運行決定構建必須失敗。
我想在Java程序中拋出未處理的異常或使用System.exit()關閉JVM,但它們看起來很討厭。
如果一個java步驟決定它應該是否存在一個螞蟻失敗的好方法?
對於<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明確處理它以及您提到的叉子,我很高興。 – jontyc 2011-04-16 16:57:42
螞蟻應該是直截了當的。如果特定步驟不成功,它確實會失敗。我認爲你想要的行爲已經內置。
至於你的自定義步驟,我的建議是在CI流程的其餘部分工作時找到一種方法在Ant之外進行操作。更好地取得這些進展,而不是陷入一個細節之中。
如果你描述你的程序正在做什麼,它可能會有所幫助。也許有更好的方法來實現你所需要的。
更新:我不認爲你應該走這條路。你用CC運行的測試應該是單元測試。如果你必須打包和部署應用程序進行測試,我會調用這些集成測試。作爲QA步驟的一部分單獨運行,而不是構建。
你正在用硒做正確的事情;我喜歡你的嚴謹和努力。但我建議僅使用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應用程序,拍攝屏幕快照並將其與已知的藍圖。如果該圖像比較步驟失敗,那麼我希望構建失敗。一切工作正常,我只是不太喜歡我用過的任何一種方法。 – jontyc 2011-04-16 14:52:42
re:你的更新:最初單個集成被放在CI中,因爲目前只有大約30%的單元測試覆蓋率,並且主流中的斷點被立即捕獲,因爲剩餘的代碼被重構以適應測試。但是,考慮到這樣的集成測試是完全自動化的,並且服務器負載不是問題,在本單元測試寫入階段之後,我沒有理由將其從CI中移除。它永遠不會取代當然測試的後期階段。 – jontyc 2011-04-16 16:06:20
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>
你可能想看看那個。
我知道
「......他們似乎很討厭...」 - 請描述你的想法,告訴用戶程序失敗了。 – duffymo 2011-04-16 13:52:56
像我的Java應用程序退出代碼到JVM的返回代碼,JVM將它傳遞給Ant和Ant作用於它失敗? – jontyc 2011-04-16 13:55:47
因此,當您調用System.exit()並繼續時返回-1。 – duffymo 2011-04-16 13:59:11