在 Jar 文件中运行 ANT build.xml 文件 [英] Running an ANT build.xml file inside a Jar file

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

问题描述

我需要使用存储在 jar 文件中的 build.xml 文件运行 ANT 构建.这个 jar 文件在类路径上可用.是否可以在不分解 jar 文件并将 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.

更新:

jar 文件是一个 ant 构建项目,其中 classpath 中的 build.xml 通过使用 ANT 库的 java 程序运行.前端是一个 web 应用程序,它加载这个构建项目 jar 作为依赖项.用户将单击构建应用程序"按钮来运行 ANT 构建.因此,当用户单击 Build App 时,我需要在 jar 中运行 build.xml 文件.

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.

读取 build.xml 文件的代码

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

推荐答案

我认为这是不可能的.ProjectHelper#parse 方法表明项目助手可以支持任何类型的源,包括 InputStream

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 - XML 配置的来源.为了向后兼容,助手必须至少支持 File.Helpers 可能支持 URL、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.

运行修改后的代码示例

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

给出以下异常:

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)

不幸的是,在查看了最新的 apache ant 版本(发布时为 1.9.4)的源代码后,内置的 ProjectHelper 实现仅支持

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

即使在几行之后创建了 InputStream :/

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

Apache ant 手册 建议可以实现自己的 ProjectHelper 并使用系统属性 org.apache.tools.ant.ProjectHelper 配置 ant 以使用它.

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.

类 org.apache.tools.ant.ProjectHelper 是预期要实现的 API.因此,通过扩展该抽象类来编写您自己的 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.

但是所有需要实现的方法似乎都只适用于 java.io.Filejava.net.URL.

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

所以我认为最好的办法是从 jar 中提取 build.xml(如我的评论所示)并针对它运行 ant.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆