I have written a java code to execute ant targets via Servlet. I have given the code below.
Expand|Select|Wrap|Line Numbers
- import org.apache.tools.ant.*;
- import org.apache.catalina.ant.*;
- .
- .
- .
- .
- public void antTest()
- {
- BuildLogger logger = new DefaultLogger();
- logger.setMessageOutputLevel(Project.MSG_INFO);
- logger.setOutputPrintStream(System.out);
- logger.setErrorPrintStream(System.out);
- logger.setEmacsMode(true);
- ProjectHelper ph = ProjectHelper.getProjectHelper();
- Project p = new Project();
- p.addBuildListener(logger);
- p.init();
- p.addReference("ant.projectHelper", ph);
- ph.parse(p, new File(sctx.getRealPath("/")+"bin/build.xml"));
- p.executeTarget("display");
- }
Expand|Select|Wrap|Line Numbers
- <project name="Web Application" default="display" basedir=".">
- <property file="${user.home}/build.properties"/>
- <property file="build.properties"/>
- <target name="init">
- <tstamp/>
- </target>
- <target name="display" description="Deletes the Web Application's war directory and war file">
- <echo message="This line is displaying from ant target ..."/>
- </target>
- </project>
Expand|Select|Wrap|Line Numbers
- This line is displaying from ant target ...
Expand|Select|Wrap|Line Numbers
- Buildfile: build.xml
- Trying to override old definition of datatype resources
- display:
- [echo] This line is displaying from ant target ...
- BUILD SUCCESSFUL
- Total time: 0 seconds
1) I expect the output from the java code should also display the same lines as in the output from command prompt. Please let me know the way to do so.
2) I want to log the output of the ant target to a log file when executing from java.
3) Is there any other way to run ant targets as we are running from the command prompt. That is if we have a string variable having the value "ant -f build.xml display", we could have to make this as the command to execute ant target. Is this possible? If so please let me know the way to implement.