1

I'm trying to discard @WebService annotated classes in my Ant build using not and contains keywords. However, it isn't working and still includes classes annotated with @WebService. Here is the task used to copy sources to the working build directory:

<copy todir="${compile.dir}">
    <fileset dir="${src.dir}">
        <include name="**/*" />
        <exclude name="**/*.class" />
        <exclude name="log4j.properties" />
        <exclude name="log4j.properties.*" />
        <exclude name="log4j.xml" />
        <exclude name="log4j.xml.*" />
        <exclude name="*.jocl" />
        <not>
            <contains text="@WebService(" casesensitive="true" />
        </not>
    </fileset>
</copy>

However, it seems like the not section is ignored, and those classes are still copied. Why isn't this working? Is the syntax wrong? Also, how do I add a condition, so that the restriction concerns only .java files?

I'm using Apache Ant(TM) version 1.9.6 compiled on July 8 2015.

martin clayton
  • 76,436
  • 32
  • 213
  • 198
Tuomas Toivonen
  • 21,690
  • 47
  • 129
  • 225

1 Answers1

1

(Perhaps this will guide you, it works for me, but I'm not quite sure I've fully understood your requirement.)

Try this fileset:

<fileset dir="${src.dir}">
  <exclude name="**/*.class" />
  <exclude name="log4j.properties" />
  <exclude name="log4j.properties.*" />
  <exclude name="log4j.xml" />
  <exclude name="log4j.xml.*" />
  <exclude name="*.jocl" />
  <or>
    <filename name="**/*.java" negate="yes" />
    <not>
      <contains text="@WebService(" casesensitive="true" />
    </not>
  </or>
</fileset>

A few explanatory comments:

There's no need to specify <include name="**/*" /> this is implicit in a fileset.

When you include a nested selector inside a fileset (like <not>...<contains>...</not>) this is implicitly ANDed with the patternsets (which are implicitly ORed with each other). See <fileset> docs:

Selectors are available as nested elements within the FileSet. If any of the selectors within the FileSet do not select the file, the file is not considered part of the FileSet. This makes a FileSet equivalent to an <and> selector container.

The upshot of this is that you need to use an <or> selector container with two negative selectors. In other words, "select the file if it's not a java file OR it doesn't match the text string".

The selector container could equivalently be written this way if you prefer:

<or>
  <not>
    <filename name="**/*.java" />
  </not>
  <not>
    <contains text="@WebService(" casesensitive="true" />
  </not>
</or>
martin clayton
  • 76,436
  • 32
  • 213
  • 198