Ant: Using a fileset with the javac task

ant

I want to use a FileSet to indicate exactly which files I want compiled with the Javac task. I don't want any implicit behavior. My attempts at this have failed however. Here's what I've got so far:

A FileSet that contains all my source files.

<fileset id="srcfiles" dir="${srcdir}">
    <include name="**/*.java"/>
</fileset>

And a target that tries to pass it to javac.

<target name="build">
    <javac srcdir=".">
        <fileset refid="srcfiles"/>
    </javac>
</target>

This gives me this error: javac doesn't support the nested "fileset" element.

After reading the Ant docs, I tried changing the inner most tag from fileset to sourcepath. This worked in the sense that the files were compiled, but it still errored out: srcfiles doesn't denote a path. I think the source was only compiled due to implicit rules.

How can I tell javac explicitly which individual files I want compiled? Or is this simply not how Ant is supposed to work? I come from a C++ background where identifying source files is a significant part of the build process. However, given Java's file naming and directory structure requirements, perhaps the implicit **/*.java pattern rule covers all use cases.

Best Answer

The ant manual for javac says :

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>

I don't think it's a good idea, though. In a well-designed project, all the files in a given directory (or in a set of directories) should be compiled. The files which shouldn't be compiled shouldn't be there at all, or should be in a separate directory.

Related Topic