Had a problem with retrieving returncode programmatically...following files. (did cut out some company-specific things). I got 2 files, one general file with a java-task in it and a specific file, which defines sime properties and a ant-task.
How can i get the build.rc in my executing java-test? The echo-message at the end of the file delivers the actual return-code, but the p.getProperty("build.rc") returns null.
specific.build.xml
<project basedir="../../../.." name="run-joblauncher">
<property name="prop1" value="valProp1" />
<ant antfile="./dev/script/general.xml" dir="${basedir}" target="RUN-MYCLASS" inheritAll="true"/>
</project>
general.xml
<project basedir="../../../.." default="RUN-MYCLASS" name="run-specifictest"> <target name="RUN-MYCLASS">
<property name="returnCode" value="99"/>
<target name="RUN-MYCLASS">
<java classname="my.company.class" fork="true" resultproperty="build.rc" failonerror="false">
<arg value="${workdir}"/>
<classpath>
<pathelement path="${java.class.path}"/>
</classpath>
</java>
</target>
</project>
myJunitTest.java
public class TestAntScripts extends TestCase {
public void testMyAnt() throws IOException {
File dir = new File("pathToSpecific.xml");
Project p = null;
try {
File buildFile = new File(buildFileName);
p = new Project();
DefaultLogger consoleLogger = new DefaultLogger();
consoleLogger.setErrorPrintStream(System.err);
consoleLogger.setOutputPrintStream(System.out);
consoleLogger.setMessageOutputLevel(Project.MSG_INFO);
p.addBuildListener(consoleLogger);
p.addBuildListener(new CustomBuildListener(this));
p.fireBuildStarted();
p.init();
ProjectHelper helper = ProjectHelper.getProjectHelper();
p.addReference("ant.projectHelper", helper);
helper.parse(p, buildFile);
p.fireBuildFinished(null);
} catch (BuildException e) {
p.fireBuildFinished(e);
e.printStackTrace();
System.err.println("AntRunner for buildfile " + buildFileName
+ " failed with Exception ");
}
String rc = p.getProperty("returnCode");
assertEquals(0, Integer.parseInt(rc));
}
public void taskFinished(String buildRc) {
assertEquals(0, buildRc);
}
}
EDIT:
how's the approach to assign a value to a already defined property? defined a var returnCode outside the java-target and try to assign the value like <var name="returnCode" value="${build.rc}"/>
inside the target. not runnin'..
EDIT2: added a custom BuildListener and grabbing the build.rc over there.
@Override
public void targetFinished(BuildEvent event) {
if (event.getTarget().getName().trim().equalsIgnoreCase("RUN-MYCLASS")) {
String buildRc = event.getTarget().getProject().getProperty("build.rc");
tester.taskFinished(buildRc, jobname);
}
tester is my JUnit-Test, which will be set in Constructor.