java 获取ant,从Java运行ant

Is there a tutorial on how to run Ant from Java? I got some code from here: Setting JAVA_HOME when running Ant from Java

But haven't been able to make it work. I've been trying to find an example or tutorial on how to actually use it.

Here's what I have so far:

Project p = new Project();

p.setUserProperty("ant.file", buildFile.getAbsolutePath());

p.fireBuildStarted();

p.init();

p.executeTarget("default");

But I guess this error:

Exception in thread "main" Target "default" does not exist in the project "null".

at org.apache.tools.ant.Project.tsort(Project.java:1912)

at org.apache.tools.ant.Project.topoSort(Project.java:1820)

at org.apache.tools.ant.Project.topoSort(Project.java:1783)

at org.apache.tools.ant.Project.executeTarget(Project.java:1368)

at com.arthrocare.vss2svn.VSS2SVN.newProcess(VSS2SVN.java:128)

at com.arthrocare.vss2svn.VSS2SVN.main(VSS2SVN.java:52)

Java Result: 1

I tried specifying the project with:

p.setUserProperty("ant.project.name", "VSS Project");

But no luck.

The ant file specified is correct as it works perfectly from the command line.

UPDATE

It is a great tutorial.

Here's the code I got a little bit earlier than seeing the code in the answer below:

Project project = new Project();

ProjectHelper.configureProject(project, buildFile);

DefaultLogger consoleLogger = new DefaultLogger();

consoleLogger.setErrorPrintStream(System.err);

consoleLogger.setOutputPrintStream(System.out);

consoleLogger.setMessageOutputLevel(Project.MSG_INFO);

project.addBuildListener(consoleLogger);

project.init();

project.executeTarget(project.getDefaultTarget());

But for some reason the task still fails! I'm using a Visual Source Safe task that needs to read an environment value at runtime but it doesn't see it with this approach. Running the build.xml file manually and with the following code works:

ProcessBuilder pb = new ProcessBuilder();

Map env = pb.environment();

String path = env.get("ANT_HOME");

System.out.println(path);

pb.directory(new File(System.getProperty("user.home")));

pb.command(path + System.getProperty("file.separator")

+ "bin" + System.getProperty("file.separator") + "ant.bat");

try {

Process p = pb.start();

} catch (IOException ex) {

//

}

解决方案

Is there a tutorial on how to run Ant from Java?

Part of my answer to this question might help:

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());

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值