7

I've looked at a bunch of different examples, and tried several variations, but can't seem to get this working correctly. It also appears that you can't exclude an entire directory with javac, but only files, which I suppose means you can't specify a package? Here is what I have:

<javac srcdir="src" destdir="WEB-INF/classes" excludes="path/to/excluded/package/*.java">
    <classpath refid="compile.classpath"/> <!-- reference defined earlier -->
</javac>
BartoszKP
  • 34,786
  • 15
  • 102
  • 130

4 Answers4

12

You can exclude whole directories or directory trees with "**" and exclude. Example

<dirset dir="aDirectory">
    <include name="a/package/**"/>
    <exclude name="**/package/to/exclude**"/>
</dirset>
Andreas Kraft
  • 3,754
  • 1
  • 30
  • 39
5

Please note that excludes does not work if any of the files in "src" (or in "includes" if specified) have dependency in the files specified in excludes. So first ensure that files excluded are not referred in any of the include files.

Ravi
  • 51
  • 1
  • 1
1

The javac task implicity includes all files in the srcdir and you can just add an exclude element to the task, for example:

<javac destdir="build" srcdir="src" >
    <exclude name="**/my/package/test/*.java" />
</javac>

This would not compile any of the classes in the my.package.test package.

biegleux
  • 13,179
  • 11
  • 45
  • 52
Kevin Sadler
  • 2,306
  • 25
  • 33
  • 2
    Problem is that when another class imports from that package, the compiler pulls in that file, even though it states here to exclude it. The whole include/exclude thing is just the initial set that's fed to javac. Javac is then free to pull in other files, based on the src dir(s). There is a need to tell javac to explicitly not access certain files. I think this can only be done by copying only the files you're willing to give to javac, and then compile that sub-set. – Mike Jan 22 '13 at 22:00
  • 2
    This is useful where you have a package that no other code depends on - such as test classes - that shouldn't be deployed. – Kevin Sadler Jan 29 '13 at 22:52
1

Mike, another way to to tell javac to explicitly not access certain files is to unset sourcepath. I just figured this out...

From the Ant manual:

Note: If you wish to compile only source files located in certain packages below a common root, use the include/exclude attributes or / nested elements to filter for these packages. Do not include part of your package structure in the srcdir attribute (or nested elements), or Ant will recompile your source files every time you run your compile target. See the Ant FAQ for additional information.

If you wish to compile only files explicitly specified and disable javac's default searching mechanism then you can unset the sourcepath attribute:

<javac sourcepath="" srcdir="${src}" destdir="${build}" >
    <include name="**/*.java"/>
    <exclude name="**/Example.java"/>
</javac>

That way the javac will compile all java source files under "${src}" directory but skip the examples. The compiler will even produce errors if some of the non-example files refers to them.

robodanny
  • 96
  • 5