【问题标题】:Why can't I get the source file for a resource inside <mappedresource>?为什么我无法在 <mappedresource> 中获取资源的源文件?
【发布时间】:2012-10-07 05:07:32
【问题描述】:

我将我们的一个自定义(当然是以前工作的)Ant 任务从使用&lt;zipfileset&gt; 作为在&lt;zip&gt; 任务之间重用资源组的唯一方法,而我们的自定义任务结果是使用资源集合.所以本质上,我现在只是将&lt;fileset&gt; 包裹在&lt;mappedresources&gt; 中。

<project name="test" default="installers">
  <target name="installers">
    <union id="common">
      <fileset prefix="." dir="." includes="1"/>
      <mappedresources>
        <fileset dir="." includes="2"/>
        <globmapper from="*" to="3/*"/>
      </mappedresources>
    </union>
    <generate-wix>
      <resources refid="common"/>
    </generate-wix>
  </target>
</project>

自定义任务看起来像这样,省略了所有特定于 WiX 的废话,因为它不是问题(或者至少它不是问题现在。)

public class GenerateWixTask extends Task {
    private Resources resources;

    @Override
    public void execute() throws BuildException {
        // omitting root element setup

        Iterator<?> resourceIterator = resources.iterator();
        while (resourceIterator.hasNext()) {
            Resource resource = (Resource) resourceIterator.next();

            // This is the relative path with respect to the WiX install.
            File filePath = new File(resource.getName());
            //String fileName = filePath.getName();

            // This is the file which will be used as the actual data.
            //TODO: How to get this?

            System.out.println("resource: " + resource.getName());
            System.out.println("  which is a " + resource.getClass());
            System.out.println("  name = " + resource.getName());
            System.out.println("  location = " + resource.getLocation());

            FileProvider provider = (FileProvider) resource.as(FileProvider.class);
            if (provider != null) {
                System.out.println("  file = " + provider.getFile());
            }


        }

        // omitting code to write the XML
    }

    public void setResourcesref(Reference resourcesref) {
        createResources().setRefid(resourcesref);
    }

    public Resources createResources() {
        if (resources == null) {
            resources = new Resources();
            resources.setProject(getProject());
        }
        return resources;
    }
}

这给我的输出如下:

[generatewix] resource: 1
[generatewix]   which is a class org.apache.tools.ant.types.resources.FileResource
[generatewix]   name = 1
[generatewix]   location =
[generatewix]   file = C:\Data\test\1
[generatewix] resource: 3/2
[generatewix]   which is a class org.apache.tools.ant.types.resources.MappedResource
[generatewix]   name = 3/2
[generatewix]   location =

在阅读 MappedResource 的源代码时,对 FileProvider 进行了特殊处理以隐藏源文件(该注释听起来像是为了防止滥用......我并不是完全想滥用它。)但我确实想找出文件的路径,因为我正在编写一个 XML 文件供另一个应用程序读取...

有解决办法吗?

【问题讨论】:

    标签: java ant resources


    【解决方案1】:

    很明显,映射路径不能成为真正的File 的来源。 而MappedResource 不是FileResource

    尝试从getInputStream() 读取数据并将其写入getName() 指定的新路径。

    【讨论】:

    • 我试图避免进行不必要的文件 I/O... 否则我只会将所有资源复制到另一个目录中,然后将该目录用作单个文件集。我们的安装程序接近 200MB,复制所有安装程序会不必要地增加构建时间。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多