简体   繁体   English

Ant从(绝对)属性创建文件集,dirsets

[英]Ant create filesets, dirsets from (absolute) properties

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: 大多数ant任务都需要文件集,因此要从属性构建文件集,我必须这样做:

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

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) ? 问题是myprop是绝对的,我应该省略dir属性( 这是不可能的 ),所以有没有办法定义(绝对)属性并有效地使用它们来创建文件集,dirsets等..或者在蚂蚁是这个不好的做法(只应使用相对路径)

Thanks. 谢谢。

There's no particular rules about absolute versus relative with Ant. 关于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. 如果必须对构建的某些部分使用绝对路径,请尝试构建它们 - 例如从项目的${basedir}构建它们 - 以便更容易进行更改。

Two things that may be useful for converting absolute paths to relative paths for use in filesets and the like: 将绝对路径转换为相对路径以在文件集等中使用可能有用的两件事:

  • the property task supports an attribute relative= that can be used to convert from an absolute path to relative. property任务支持属性relative= ,可用于将绝对路径转换为相对路径。
  • the pathconvert task can be used to do the same, when there are several absolute paths in one property. 当一个属性中有多个绝对路径时, pathconvert任务可用于执行相同操作。

Examples: 例子:

<!-- 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}" />

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'. 回应一条评论:我不知道在Ant中获取文件集的最长公共前缀目录的简单方法,但是这里有一个宏包装脚本任务,它执行你可能称之为“收缩包装”的内容。 You call it with a reference to the 'input' fileset, and the name of the property to set. 您可以通过引用'input'文件集以及要设置的属性的名称来调用它。

<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>

(This was derived from a php implementation .) (这是从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}'" />

The pathconvert task can now be used to create a shrink-wrapped fileset: pathconvert任务现在可用于创建收缩包装文件集:

<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}'" />

I would suggest you to use a different property for the dir target, if you are doing an absolute reference to ${myprop}, I guess it's a file name. 我建议你为dir目标使用不同的属性,如果你对$ {myprop}进行绝对引用,我想这是一个文件名。 Try it separating it into ${parent.dir} and ${target.filename}. 尝试将其分成$ {parent.dir}和$ {target.filename}。

<fileset dir="${parent.dir}" includes="${target.filename}"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM