You can call ant scripts from Java code.
See this article (scroll down to the "Running Ant via Java" section) and this article:
File buildFile = new File("build.xml");
Project p = new Project();
p.setUserProperty("ant.file", buildFile.getAbsolutePath());
p.init();
ProjectHelper helper = ProjectHelper.getProjectHelper();
p.addReference("ant.projectHelper", helper);
helper.parse(p, buildFile);
p.executeTarget(p.getDefaultTarget());
Update
I tried with the following ant file , it did not "tell" anything (no console output), but it worked: the file was indeed moved
<project name="testproject" default="test" basedir=".">
<target name="test">
<move file="test.txt" tofile="test2.txt" />
</target>
</project>
And when I try it again (when there is no test.txt
to move(it is already moved)), I got an java.io.FileNotFoundException
.
I think this is what you would expect when you run something from Java.
If you want the console output of the ant tasks, you might want to add a Logger as a build listener.
From @Perception's answer below.
DefaultLogger consoleLogger = new DefaultLogger();
consoleLogger.setErrorPrintStream(System.err);
consoleLogger.setOutputPrintStream(System.out);
consoleLogger.setMessageOutputLevel(Project.MSG_INFO);
p.addBuildListener(consoleLogger);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…