为什么我不能在<mappedresource>?</mappedresource>中获取资源的源文件

时间:2012-10-17 01:36:52

标签: java ant resources

我使用<zipfileset>转换了我们的自定义(当前正在使用的)Ant任务,因为在<zip>任务和我们的自定义任务之间重用资源组的唯一方法是使用资源集合。基本上,我现在只需<fileset>包裹<mappedresources>

<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文件来阅读...

有解决方法吗?

1 个答案:

答案 0 :(得分:0)

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

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