1

我将我们的一个自定义(当然是以前工作的)Ant 任务从<zipfileset>用作在任务之间重用资源组的唯一方法转换,<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 文件供另一个应用程序读取...

有解决方法吗?

4

1 回答 1

0

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

尝试从getInputStream()读取数据并将其写入getName().

于 2012-10-17T06:08:25.150 回答