Ant 从(绝对)属性创建文件集、目录集 [英] Ant create filesets, dirsets from (absolute) properties

查看:31
本文介绍了Ant 从(绝对)属性创建文件集、目录集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 ant 中,我定义了一些属性,这些属性定义了我的构建系统所需的一些路径(绝对).

In ant, I have defined some properties that define some paths (absolute) needed in my build system.

大多数 ant 任务都需要文件集,因此要从属性构建文件集,我必须这样做:

Most of ant tasks need filesets, so to build a fileset from a property I would have to do:

<fileset dir="" includes="${myprop}"/>

问题是 myprop 是绝对的,我应该省略 dir 属性(这是不可能的),所以 有没有办法定义(绝对) 属性并有效地使用它们来创建文件集、目录集等. 或在 ant 这是一个不好的做法(只应使用相对路径)?

The problem is that myprop is absolute and I should omit the dir attribute (which is not possible), so is there a way to define (absolute) properties and efficiently use them to create filesets, dirsets, etc.. or in ant is this a bad practice (only relative paths should be used)?

谢谢.

推荐答案

对于 Ant 的绝对与相对没有特定的规则.如果可能,您应该避免硬编码绝对路径 - 使您的构建更具便携性.如果您必须对构建的某些部分使用绝对路径,请尝试构建它们 - 例如从项目的 ${basedir} 中构建 - 以便更容易进行更改.

There's no particular rules about absolute versus relative with Ant. You should avoid hard-coding absolute paths if possible - to make your build more portable. If you have to use absolute paths for some part of the build, try to construct them - say from the ${basedir} of the project - so that changes are easier to make.

对于将绝对路径转换为相对路径以用于文件集等可能有用的两件事:

Two things that may be useful for converting absolute paths to relative paths for use in filesets and the like:

  • property 任务 支持一个属性relative= 可用于从绝对路径转换为相对路径.
  • 可以使用pathconvert 任务当一个属性中有多个绝对路径时,也可以这样做.
  • the property task supports an attribute relative= that can be used to convert from an absolute path to relative.
  • the pathconvert task can be used to do the same, when there are several absolute paths in one property.

示例:

<!-- Absolute paths -->
<property name="myprop" value="${basedir}/x" />
<property name="myprop2" value="${basedir}/x:${basedir}/y" />

<!-- Convert single path to relative to basedir -->
<property name="myprop_rel" location="${myprop}" relative="yes"/>

<!-- Go via path/pathconvert for the property with multiple paths -->
<path id="mypath2" path="${myprop2}" />
<pathconvert property="myprop2_rel" refid="mypath2" pathsep=",">
    <map from="${basedir}/" to="" />
</pathconvert>

<fileset id="my_fileset" dir="." includes="${myprop2_rel}" />

回复评论:我不知道有一种简单的方法可以在 Ant 中获取文件集的最长公共前缀目录,但这里有一个宏包装了一个脚本任务,它执行您可能称之为收缩包装"的操作.您可以使用对输入"文件集的引用以及要设置的属性名称来调用它.

In response to a comment: I'm not aware of a simple way to get the longest common prefix directory of a fileset in Ant, but here's a macro wrapping a script task that does what you might call 'shrink wrapping'. You call it with a reference to the 'input' fileset, and the name of the property to set.

<macrodef name="common-prefix-dir">
  <attribute name="refid" />
  <attribute name="outputproperty" />
  <sequential>
  <script language="javascript"><![CDATA[

    srcFiles = project.getReference( "@{refid}" )
              .getDirectoryScanner( project )
              .getIncludedFiles( );

    if ( srcFiles.length > 0 ) {
      prefix = "" + srcFiles[0];
      for ( i = 0; i < srcFiles.length; i++ ) {
        while ( prefix.length && srcFiles[i].substr( 0, prefix.length ) != prefix ) {
          prefix = prefix.substr( 0, prefix.length - 1);
        }

        if ( !prefix.length ) {
          break;
        }
      }
    }
    else {
      prefix = "";
    }
    prefix = prefix.substring( 0, prefix.lastIndexOf( '/' ) );
    project.setProperty( "@{outputproperty}", prefix );

  ]]></script>
  </sequential>
</macrodef>

(这源自 php 实现.)

您可以尝试使用以下方法:

You can try it out with something like:

<property name="my.dir" location="...your dir here..." />
<fileset id="my.fs" dir="${my.dir}">
  <!-- define fileset here -->
</fileset>
<echo message="Input fileset is: '${toString:my.fs}'" />

<common-prefix-dir refid="my.fs" outputproperty="prefix" />
<echo message="Longest common prefix is: '${prefix}'" />

pathconvert 任务现在可用于创建收缩包装的文件集:

The pathconvert task can now be used to create a shrink-wrapped fileset:

<pathconvert property="shrink" refid="my.fs" pathsep=",">
  <map from="${my.dir}/${prefix}/" to="" />
</pathconvert>
<fileset id="my.fs.out" dir="${my.dir}/${prefix}" includes="${shrink}" />
<echo message="Shrink-wrapped fileset is: '${toString:my.fs.out}'" />

这篇关于Ant 从(绝对)属性创建文件集、目录集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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