运行一个JAR文件中的ANT build.xml文件 [英] Running an ANT build.xml file inside a Jar file
问题描述
我需要运行使用存储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的 ProjectHelper#解析方法表明,它是可能的项目帮手,支持任何形式的来源,包括的InputStream
源 - 源的XML配置。一个辅助必须至少支持文件,为保持向后兼容性。助手可能支持URL,的InputStream 等或专门类型。
块引用>运行你modifiyed样品code
字符串buildXml =build.xml文件;
项目P =新的项目();
p.setUserProperty(ant.file,buildXml);
压住他();
ProjectHelper帮手= ProjectHelper.getProjectHelper();
p.addReference(ant.projectHelper,帮手);
的InputStream的InputStream =新的FileInputStream(buildXml);
helper.parse(P,InputStream的);
p.executeTarget(p.getDefaultTarget());给出了以下异常:
异常线程main
来源java.io.FileInputStream中不受此插件支持
在org.apache.tools.ant.helper.ProjectHelper2.parse(ProjectHelper2.java:233)
在org.apache.tools.ant.helper.ProjectHelper2.parse(ProjectHelper2.java:177)
在ant.test.App.main(App.java:28)寻找到的最新Apache Ant的版本的源代码code(1.9.4发布在时间)内置
ProjectHelper
实施只支持后,不幸的是
- 的java.io.File
- 的java.net.URL
- org.apache.tools.ant.types.Resource
即使一个
的InputStream
之后创建的几行:/Apache Ant安装手册表明,它是可能的implemt一个自己的
ProjectHelper
和配置蚂蚁使用系统属性来使用它org.apache.tools.ant.ProjectHelper
。
类org.apache.tools.ant.ProjectHelper是预期要实现的API。因此,通过扩展抽象类写自己的ProjectHelper。
块引用>但所有的需要实施的方法似乎只与
的java.io.File
或的java.net.URL $工作C $ C>。
因此,我认为最好的办法是到extrat的
的build.xml
从罐子(在我的评论中所示),并对其运行的蚂蚁。I need to run an ANT build using the build.xml file that is stored inside the jar file. This jar file is available on the classpath. Is it possible to do it, without exploding the jar file and saving the build.xml to local directory? If so how can I do it.
Update:
The jar file is a ant build project where a build.xml in classpath is run via a java program using ANT libraries. The front end is a web application, which loads this build project jar as a dependency. A user will click a Build App button to run the ANT build. So I need to run the build.xml file in a jar when user clicks Build App.
Code for reading the build.xml file
URL preBuildFileUrl = getClass().getClassLoader().getResource("build.xml"); Project p = new Project(); p.setUserProperty("ant.file", preBuildFileUrl.toURI().getPath()); p.init(); ProjectHelper helper = ProjectHelper.getProjectHelper(); p.addReference("ant.projectHelper", helper); helper.parse(p, new File(preBuildFileUrl.toURI().getPath())); p.executeTarget(p.getDefaultTarget());
解决方案I think it's not possible. The API for the ProjectHelper#parse method suggests that it is possible for a Project helper to support any kind of sources including
InputStream
source - The source for XML configuration. A helper must support at least File, for backward compatibility. Helpers may support URL, InputStream, etc or specialized types.
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 propertyorg.apache.tools.ant.ProjectHelper
.The class org.apache.tools.ant.ProjectHelper is the API expected to be implemented. So write your own ProjectHelper by extending that abstract class.
But all the methods that need to be implemented seem to work only with
java.io.File
orjava.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文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!