Ant create a set of files, dirsets from (absolute) properties

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

Most ant tasks require file sets, so to create a set of files from a property that I would need to do:

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

The problem is what mypropis absolute, and I have to omit the dir attribute (which is impossible), so there is a way to define (absolute) properties and use them effectively to create sets of files, dirsets, etc. or in ant is bad practice (only relative paths should be used) ?

Thank.

+3
source share
2

Ant. , , . , - , ${basedir} - .

, ..:

:

<!-- 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, , script, , " ". "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>

( php-.)

- :

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

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

dir, ${myprop}, , . ${parent.dir} ${target.filename}.

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

Source: https://habr.com/ru/post/1793607/


All Articles