macrodef 的 <jar> 任务的行为不像隐式文件集?

macrodef'd <jar> task not behaving like an implicit fileset?

使用标准的Ant 1.9.7 来assemble 一堆jar 文件。我们制作了一个宏来帮助减少 XML:

的冗长
<!-- All 'description' bits snipped for SO posting. -->
<macrodef name="buildjar">
  ... bunch of <attribute> ...
  <element name="also"/>
  <sequential>
    <jar ....>
      <manifest>  ...  </manifest>
      <fileset what="stuff in every jar file" />
      <fileset what="and this stuff too" />
      <mappedresources if:true="beauty">
        <fileset for when truth is beauty/>
        <globmapper from="ugly" to="beauty"/>
      </mappedresoruces>

      <!-- Anything else for this specific jar. -->
      <also/>

    </jar>
  </sequential>
</macrodef>

这个有效:

<buildjar .........>
  <also>
    <fileset file="/some/path/somewhere/a_single_file"/>
  </also>
</buildjar>

但这不是:

<buildjar .........>
  <also>
    <include name="/some/path/somewhere/a_single_file"/>
  </also>
</buildjar>

没有错误。查看 ant -d 输出,根本没有提及附加条目,在第一个示例中有一行 fileset: Setup scanner in dir /some/path/somewhere with patternSet{ includes: [a_single_file] excludes: [] }

多个文件同上。这有效:

<buildjar .........>
  <also>
    <fileset dir="/some/path/somewhere">
      <include name="one_file" />
      <include name="foo**" />
    </fileset>
  </also>
</buildjar>

但这不是:

<buildjar .........>
  <also>
    <include name="/some/path/somewhere/one_file"/>
    <include name="/some/path/somewhere/foo**"/>
  </also>
</buildjar>

根据 <jar> 的 Ant 手册页,

This task forms an implicit FileSet and supports most attributes of <fileset> (dir becomes basedir) as well as the nested <include>, <exclude> and <patternset> elements.

所以理论上,一个 <include> 就足够了,并成为宏 <jar> 的嵌套元素吗?

显然,在实践中这不是问题(我们在构建文件中添加了一个 bigass 注释,告诉人们不要遗漏显式的 <fileset>)。而且我们不能像这样把<fileset>放到宏定义中:

<macrodef name="buildjar">
  <element name="also"/>
  <sequential>
    <jar ....>
      .....
      <!-- Anything else for this specific jar. -->
>>    <fileset dir="some_generic_base_path">
        <also/>
>>    </fileset>
    </jar>
  </sequential>
</macrodef>

因为当调用代码执行 buildjar 而没有 任何 also 块时,不受限制的文件集将包括整个 some_generic_base_path 树.

这仅仅是让我们大吃一惊的宏定义和文件集之间的一些交互吗?

简短的回答是否定的——这不是宏定义问题。要使用隐式文件集,您必须为 <jar> 任务指定 basedir 属性。这是一个说明这一点的例子:

<jar destfile="my.jar">
    <include name="a/b" />
</jar>

<zip destfile="my.zip" >
    <include name="a/b" />
</zip>

在示例中,<jar> 任务将成功并创建仅包含清单的 jar 文件。但是 <zip> 任务会失败说:

BUILD FAILED
build.xml:8: basedir attribute must be set, or at least one resource collection must be given!

jar 任务基于zip 任务并继承其对资源的检查。因为 jar 任务有一些 - 清单 - 没有抛出错误。

如果要使用隐式文件集,请指定 basedir