Java – ant creates filesets from (absolute) attributes, dirsets

In ant, I define some attributes to define some paths (absolute) required to build the system

Most ant tasks require filesets, so to build filesets from attributes, I have to do this:

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

The problem is that myprop is absolute, and I should omit the dir attribute (which is impossible), so there is no way to define (absolute) attributes and use them effectively to create filesets, dirsets, etc Or is this bad practice in ants (only relative paths should be used)?

thank you.

Solution

There are no special rules about the absolute and relative of ant If possible, you should avoid hard coding absolute paths – to make your build more portable If you must use absolute paths for some parts of the build, try building them – for example, from ${basedir} of the project – to make changes easier

There are two things that can be useful in converting absolute paths to relative paths for use in filesets and so on:

>Property task supports the attribute relative = which can be used to convert absolute paths to relative paths@ H_ 403_ 28 @ > when there are multiple absolute paths in an attribute, pathconvert task can be used to perform the same operation

example:

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

Response comment: I don't know a simple way to get the longest public prefix directory of a file set in ant, @ H_ 403_ 28 @ but this is a macro that wraps script tasks and executes what you might call shrink wrap@ H_ 403_ 28 @ you can call it by referring to the 'input' file set and the name of the attribute to be 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 is from PHP implementation.)

You can try:

<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 shrink wrap filesets:

<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}'" />
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>