title: compiling only part of the source tree with ant
body: <p>Say I have my sources in my src/ tree (and possibly in my test/ tree). Say I would like to compile only <em>part</em> of that tree. The reasons why I might want to do that are various. Just as an example, I might want to create the smallest possible jar (without including certain classes), or I might want the fastest compile time for what I am compiling. I absolutely want to compile all the dependencies, though!</p>
  
  <p>This can be easily achieved from the command line with:</p>
  
  <pre><code>javac -d build/ -cp whatever -sourcepath src src/path/to/MyClass.java
  </code></pre>
  
  <p>Now, how can you do that with ant? The javac ant <a href="http://ant.apache.org/manual/Tasks/javac.html" rel="noreferrer">task compiles everything</a>:</p>
  
  <blockquote>
    <p>The source and destination directory
    will be recursively scanned for Java
    source files to compile.</p>
  </blockquote>
  
  <p>One can use the <code>excludes</code> and <code>includes</code> parameters, but they are problematic for this purpose. In fact, it seems that one has to explicitly setup all the <code>includes</code> (not automatic dependency lookup), and <strong>even worst</strong> that <a href="http://ant.apache.org/manual/dirtasks.html#patternset" rel="noreferrer">excludes has priority on includes</a>:</p>
  
  <blockquote>
    <p>When both inclusion and exclusion are
    used, only files/directories that
    match at least one of the include
    patterns and don't match <em>any</em> of the
    exclude patterns are used.</p>
  </blockquote>
  
  <p>Thus, you cannot use </p>
  
  <pre><code>&lt;javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="classpath"
              excludes="**/*.java" includes="src/path/to/MyClass.java" /&gt;  
  </code></pre>
  
  <p>Because it will not compile anything :-(</p>
  
  <p>Is there any way of achieving that simple command line <code>javac</code> with ant?</p>
  
  <hr>
  
  <p>EDITED: Thank you for your answer, Sadie, I'm accepting it, because it does work in the way I was wondering in this question. But I have a couple of comments (too long to be in the comment field of your answer):</p>
  
  <p>1) I did read the documentation (see links above), but it's unclear that with just <code>includes</code> you are actually also excluding everything else</p>
  
  <p>2) When you just <code>includes</code> ant logs something like</p>
  
  <pre><code>[javac] Compiling 1 source file to /my/path/to/build
  </code></pre>
  
  <p>even if the dependencies make it compiling (much) more than just one source file.</p>