1
我正在尝试从Java执行Ant目标,以便按条件搜索目录。如果从控制台运行searchDirectories
,则结果为OK,如果来自Java类,则找不到目录。Ant:如何从Java执行Ant
File buildFile = new File("build.xml");
Project p = new Project();
p.setUserProperty("ant.file", buildFile.getAbsolutePath());
p.init();
ProjectHelper helper = ProjectHelper.getProjectHelper();
DefaultLogger consoleLogger = new DefaultLogger();
consoleLogger.setErrorPrintStream(System.err);
consoleLogger.setOutputPrintStream(System.out);
consoleLogger.setMessageOutputLevel(Project.MSG_INFO);
p.addBuildListener(consoleLogger);
p.addReference("ant.projectHelper", helper);
helper.parse(p, buildFile);
try {
p.fireBuildStarted();
p.fireSubBuildStarted();
p.executeTarget("searchDirectories");
} catch (IOException e) {
p.fireBuildFinished(e);
}
编辑:如果我调用Ant从那里包含build.xml文件的目录,目录会被发现。否则当通过这条线执行ant时,结果是不正确的。
ant -buildfile D:\Projects\antproj searchDirectories
我不明白是什么问题?
['Runtime#exec(String)'](https://docs.oracle.com/javase/8/docs/api/java/lang/Runtime.html#exec-java.lang.String- )?您可以执行您在shell中使用的相同命令。 – beatngu13
beatngu13,反正它不起作用 –