本文介绍了运行一个JAR文件中的ANT build.xml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要运行使用存储jar文件里面的build.xml文件Ant构建。这个jar文件可在类路径中。是否有可能做到这一点,没有爆炸的jar文件,并保存了build.xml,本地目录?如果是的话我怎么能做到这一点。

更新:

这个jar文件是在classpath中一个build.xml通过使用ANT库java程序运行Ant构建项目。前端是一个Web应用程序,它加载为此构建项目的水瓶中的依赖。用户会点击一个构建应用程序按钮运行Ant构建。所以,我需要运行在一个罐子里的build.xml文件,当用户点击应用程序构建

code读取build.xml文件

  URL preBuildFileUrl =的getClass()getClassLoader()的getResource(build.xml文件)。;
项目P =新的项目();
p.setUserProperty(ant.file,preBuildFileUrl.toURI()的getPath());
压住他();
ProjectHelper帮手= ProjectHelper.getProjectHelper();
p.addReference(ant.projectHelper,帮手);
helper.parse(P,新文件(preBuildFileUrl.toURI()的getPath()));
p.executeTarget(p.getDefaultTarget());


解决方案

我认为这是不可能的。对于该API的方法表明,它是可能的项目帮手,支持任何形式的来源,包括的InputStream

running a modifiyed sample of you code

String buildXml = "build.xml";
Project p = new Project();
p.setUserProperty("ant.file", buildXml);
p.init();
ProjectHelper helper = ProjectHelper.getProjectHelper();
p.addReference("ant.projectHelper", helper);
InputStream inputStream = new FileInputStream(buildXml);
helper.parse(p, inputStream);
p.executeTarget(p.getDefaultTarget());

gives the following Exception:

Exception in thread "main"
Source java.io.FileInputStream not supported by this plugin
at org.apache.tools.ant.helper.ProjectHelper2.parse(ProjectHelper2.java:233)
at org.apache.tools.ant.helper.ProjectHelper2.parse(ProjectHelper2.java:177)
at ant.test.App.main(App.java:28)

Unfortunatly after looking into the source code of the latest apache ant version (1.9.4 at posting time) the Built-In ProjectHelper implementation does only support

  • java.io.File
  • java.net.URL
  • org.apache.tools.ant.types.Resource

even though an InputStream is created a few lines later :/

The apache ant manual suggests that it is possible to implemt an own ProjectHelper and configure ant to use it using the system property org.apache.tools.ant.ProjectHelper.

But all the methods that need to be implemented seem to work only with java.io.File or java.net.URL.

So I think your best bet is to extrat the build.xml from the jar (as shown in my comment) and run ant against it.

这篇关于运行一个JAR文件中的ANT build.xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 02:55
查看更多