0
votes

Is there any way to exclude files from an ant fileset based on the file content?

We do have test servers where code files are mixed up with files that have been generated by a CMS. Usually, the files are placed in different folders, but there is a risk that real code files are in the middle of generated code.

The only way to differentiate generated files is to open the files and look at it's content. If the file contains a keyword, it should be excluded.

Does anyone know a way to perform this with Ant?

From the answer provided by Preet Sangha, Ishould use a filterchain. However, I'm missing a step here.

Let's say I load a text file of exclusions to be performed:

<loadfile property="exclusions" srcFile="exclusions.txt" />

But I don't know how to integrate it into my current copy task:

<copy todir="${test.dir}">
  <fileset dir="${src.dir}">

  </fileset>
</copy>

I tried to add the following exclude to the fileset but it does not do anything:

<exclude name="${exclusions}"/>

I'm sure I'm missing a simple step...

2
You're talking about content-based exclusion, but your example here excludes based on file extensions. So which one is it?Cata
@Cata: Sorry for the confusion. I'm trying to add content based exclusion. The file extensions here are the exclusions already in place.E. Jaep

2 Answers

1
votes

Have a look at the not and contains selectors.

The not selector contains an example of pretty much exactly what you're trying to do.

<copy todir="${test.dir}">
  <fileset dir="${src.dir}">
    <not>
      <contains text="your-keyword-here"/>
    </not>
  </fileset>
</copy>

There's also the containsregexp selector which might be useful if your criteria for exclusion is more complicated.

There's a load more selectors you can use to refine your selection if needed.

0
votes

I don't know ant but reading the docs....

  • Can you build a files list using a filterchain, and put this into the excludefiles of a fileset?

or