How to exclude source package using javac in Ant?

I looked at a bunch of different examples and tried several options, but it seemed that I couldn’t work normally. It also seems that you cannot exclude the whole directory with javac, but only the files, which I suppose means that you cannot specify the 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> 
+6
javac ant
source share
4 answers

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> 
+11
source share

Note that the exception does not work if any of the files in "src" (or in the "include" if specified) depends on the files specified in the exceptions. Therefore, first make sure that the excluded files are not transferred to any of the included files.

+5
source share

The explicit javac task includes all the files in srcdir, and you can simply add an exception element to the task, for example:

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

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

+1
source share

Mike, another way to tell javac not to explicitly access specific files is to disable the original path. I just figured it out ...

In the Ant manual:

Note. If you want to compile only source files located in specific packages under a common root, use the include / exclude attributes or / nested elements to filter these packages. Do not include part of the structure of your package in the srcdir attribute (or nested elements), or Ant will recompile the source files every time you run your compilation target. See Frequently Asked Questions Ant.

If you want to compile only files explicitly specified and disabled by javac by default, you can disable the source path Attribute:

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

This way javac will compile all java source files under "$ {src}", but skip the examples. The compiler will even produce errors if they include some non-standard files.

0
source share

All Articles