Supponiamo di avere qualche attività Ant - diciamo javac o junit - se una delle attività fallisce, voglio eseguire un'attività, ma se ci riesco non lo faccio.Come si esegue un comando Ant se un'attività non riesce?
Qualche idea su come procedere?
Supponiamo di avere qualche attività Ant - diciamo javac o junit - se una delle attività fallisce, voglio eseguire un'attività, ma se ci riesco non lo faccio.Come si esegue un comando Ant se un'attività non riesce?
Qualche idea su come procedere?
Nella tua junit di destinazione, ad esempio, è possibile impostare la failureProperty
:
<target name="junit" depends="compile-tests" description="Runs JUnit tests">
<mkdir dir="${junit.report}"/>
<junit printsummary="true" failureProperty="test.failed">
<classpath refid="test.classpath"/>
<formatter type="xml"/>
<test name="${test.class}" todir="${junit.report}" if="test.class"/>
<batchtest fork="true" todir="${junit.report}" unless="test.class">
<fileset dir="${test.src.dir}">
<include name="**/*Test.java"/>
<exclude name="**/AllTests.java"/>
</fileset>
</batchtest>
</junit>
</target>
Poi, creare un obiettivo che viene eseguito solo se la proprietà test.failed
è impostata, ma non riesce alla fine:
<target name="otherStuff" if="test.failed">
<echo message="I'm here. Now what?"/>
<fail message="JUnit test or tests failed."/>
</target>
Infine, legarli insieme:
<target name="test" depends="junit,otherStuff"/>
Quindi chiamare il target test
per eseguire i test JUnit. Verrà eseguito il target junit
. Se non riesce (errore o errore) verrà impostata la proprietà test.failed
e verrà eseguito il corpo della destinazione otherStuff
.
L'attività javac supporta gli attributi failonerror
e errorProperty
, che possono essere utilizzati per ottenere un comportamento simile.
ant-contrib ha un'attività di prova.
Impostare una proprietà nell'attività per cui si desidera verificare l'errore, quindi scrivere la seconda attività in modo che venga eseguita se la proprietà non è impostata. Non ricordo le sintassi esatte per build.xml, o darei esempi.
come detto da Kai:
ant-contrib ha un compito TryCatch.
Ma è necessaria la versione recente 1.0b3. E poi usa
<trycatch>
<try>
... <!-- your executions which may fail -->
</try>
<catch>
... <!-- execute on failure -->
<throw message="xy failed" />
</catch>
</trycatch>
Il trucco è di lanciare di nuovo un errore per indicare una build rotta.
Purtroppo non funzionerà, perché voglio che la compilazione fallisca. – tomjen