Why does ANT update the contents of a fileset after it was created, and can I override this?

StackOverflow https://stackoverflow.com/questions/12427040

  •  02-07-2021
  •  | 
  •  

Pregunta

I think this may be easiest explained by an example, so here goes:

<target name="test">
    <fileset id="fileset" dir="target">
        <include name="*"/>
    </fileset>
    <echo>${toString:fileset}</echo>
    <touch file="target/test"/>
    <echo>${toString:fileset}</echo>
</target>

Outputs:

test:
     [echo] 
    [touch] Creating target/test
     [echo] test

What I ideally want is to have the fileset stay the same so I can have a before/after set (in order to get a changed set using <difference>, so if you know of a way to skip right to that...).

I've tried using <filelist> instead, but I can't get this correctly populated and compared in the <difference> task (they're also hard to debug since I can't seem to output their contents). I also tried using <modified/> to select files in the fileset, but it doesn't seem to work at all and always returns nothing.

Even if there is an alternative approach I would appreciate a better understanding of what ANT is doing in the example above and why.

¿Fue útil?

Solución

The path selector is evaluated on the fly. When a file is added, it will reflect in the set when you use it.

You may able to evaluate and keep it in variable using pathconvert. Then this can be converted back to filest using pathtofilest

Otros consejos

A fileset is something like a selector. It's a set of "instructions" (inclusions, exclusions, patterns) allowing to get a set of files.

Each time you actually do something with the fileset (like printing the files it "references"), the actual set of files is computed based on the "instructions" contained in the fileset.

As Jayan pointed out it might be worth posting the final outcome as an answer, so here's a simplified version with the key parts:

<fileset id="files" dir="${target.dir}"/>
<pathconvert property="before.files" pathsep=",">
    <fileset refid="files"/>
</pathconvert>
<!-- Other Ant code changes the file-system. -->
<pathconvert property="after.files" pathsep=",">
    <fileset refid="files"/>
</pathconvert>
<filelist id="before.files" files="${before.files}"/>
<filelist id="after.files" files="${after.files}"/>
<difference id="changed.files">
    <filelist refid="before.files"/>
    <filelist refid="after.files"/>
</difference>
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top