蚂蚁创建文件集,从(绝对值)的属性dirsets [英] Ant create filesets, dirsets from (absolute) properties

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

问题描述

在蚂蚁,我已经定义了定义一些路径(绝对)需要在我的编译系统。

一些属性

大多数的Ant任务需要的文件集,因此建立从属性文件集我必须做的:

 <文件集DIR =包括=$ {} myprop/>

问题是, myprop 是绝对的,我应该忽略dir属性(这是不可能的),这样​​的有一种方法来定义(绝对)性能并有效地使用它们来创建文件集,dirsets等。或在蚂蚁的这是一个不好的做法(只是相对的路径应使用) <? / p>

感谢。


解决方案

有是关于绝对与相对与蚂蚁没有特别的规定。你应该避免硬编码绝对路径,如果可能的话 - 让你的构建更便携。如果必须使用绝对路径,构建的某些部分,试图建立他们 - 从 $ {BASEDIR} 项目的说 - 这样的变化更容易使

两件事情,可能会转换绝对路径相对路径用于文件集之类的是有用的:


  • 属性任务支持的属性 =相对,可用于从一个绝对路径转换为相对的。

  • pathconvert 任务可以使用做同样的,当有一个属性几个绝对路径。

例如:

 &LT;! - 绝对路径 - &GT;
&LT;属性名=myprop值=$ {BASEDIR} / X/&GT;
&LT;属性名=myprop2值=$ {} BASEDIR / X:$ {BASEDIR} / Y/&GT;&LT;! - 转换单个路径相对于BASEDIR - &GT;
&LT;属性名=myprop_rel位置=$ {} myprop相对=YES/&GT;&LT;! - 经路径/ pathconvert去多路径属性 - &GT;
&LT;路径ID =mypath2PATH =$ {} myprop2/&GT;
&所述; pathconvert属性=myprop2_relREFID =mypath2pathsep =,&GT;
    &LT;从地图=$ {BASEDIR} /到=/&GT;
&LT; / pathconvert&GT;&LT;文件集ID =my_filesetDIR =。包括=$ {} myprop2_rel/&GT;

在回应评论:的我不知道一个简单的方法来获得在蚂蚁文件集的最长的共同preFIX目录,
但这里有一个宏观包装脚本任务已经做了你可以称之为收缩包装。
您能够在输入文件集的引用调用它,和属性的名称来设置。

 &LT; macrodef NAME =共preFIX-dir的&GT;
  &LT;属性名=REFID/&GT;
  &LT;属性名=outputproperty/&GT;
  &LT;&连续GT;
  &LT; SCRIPT LANGUAGE =JavaScript的&GT;&LT;![CDATA [    srcFiles = project.getReference(@ {} REFID)
              .getDirectoryScanner(项目)
              .getIncludedFiles();    如果(srcFiles.length大于0){
      preFIX =+ srcFiles [0];
      对于(i = 0; I&LT; srcFiles.length;我++){
        而(prefix.length&放大器;&安培; srcFiles [I] .substr(0,prefix.length)= preFIX!){
          preFIX = prefix.substr(0,prefix.length - 1);
        }        如果(!prefix.length){
          打破;
        }
      }
    }
    其他{
      preFIX =;
    }
    preFIX = prefix.substring(0,prefix.lastIndexOf('/'));
    project.setProperty(@ {} outputproperty,preFIX);  ]]&GT;&LT; / SCRIPT&GT;
  &LT; /顺序&GT;
&LT; / macrodef&GT;

(这是从<派生href=\"http://stackoverflow.com/questions/1336207/finding-common-$p$pfix-of-array-of-strings/1336357#1336357\">php实施。)

您可以用类似尝试一下:

 &LT;属性名=my.dir位置=...您的目录在这里....../&GT;
&LT;文件集ID =my.fsDIR =$ {} my.dir&GT;
  &LT;! - 在此定义的文件集 - &GT;
&LT; /文件集&GT;
&LT;回声消息=输入文件集:$ {的toString:my.fs}'/&GT;&LT;共preFIX-DIR REFID =my.fsoutputproperty =preFIX/&GT;
&LT;回声消息=最长公共preFIX是:$ {preFIX}'/&GT;

pathconvert 任务现在可以用来创建一个收缩包装文件集:

 &LT; pathconvert财产=缩水REFID =my.fspathsep =,&GT;
  &LT;从地图=$ {my.dir} / $ {preFIX} /到=/&GT;
&LT; / pathconvert&GT;
&LT;文件集ID =my.fs.outDIR =$ {my.dir} / $ {preFIX}包括=$ {}缩水/&GT;
&LT;回声消息=收缩包装的文件集:$ {的toString:my.fs.out}'/&GT;

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

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

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

Thanks.

解决方案

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:

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

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

(This was derived from a php implementation.)

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

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

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