Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

compiling only part of the source tree with ant

Tags:

java

jar

javac

ant

Say I have my sources in my src/ tree (and possibly in my test/ tree). Say I would like to compile only part 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!

This can be easily achieved from the command line with:

javac -d build/ -cp whatever -sourcepath src src/path/to/MyClass.java

Now, how can you do that with ant? The javac ant task compiles everything:

The source and destination directory will be recursively scanned for Java source files to compile.

One can use the excludes and includes parameters, but they are problematic for this purpose. In fact, it seems that one has to explicitly setup all the includes (not automatic dependency lookup), and even worst that excludes has priority on includes:

When both inclusion and exclusion are used, only files/directories that match at least one of the include patterns and don't match any of the exclude patterns are used.

Thus, you cannot use

<javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="classpath"
            excludes="**/*.java" includes="src/path/to/MyClass.java" />  

Because it will not compile anything :-(

Is there any way of achieving that simple command line javac with ant?


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):

1) I did read the documentation (see links above), but it's unclear that with just includes you are actually also excluding everything else

2) When you just includes ant logs something like

[javac] Compiling 1 source file to /my/path/to/build

even if the dependencies make it compiling (much) more than just one source file.

like image 640
Davide Avatar asked Oct 14 '08 23:10

Davide


2 Answers

Why are you excluding as well as including? If you have at least one include, then files are only compiled if they're explicitly included. So this should work:

<javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="classpath"
        includes="src/path/to/MyClass.java" />

Or more flexibly:

<javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="classpath">
    <include name="src/path/to/MyClass.java"/>
    <include name="src/path/to/AnotherClass.java"/>
</javac>

To include only certain packages or classes in a jar, use a fileset attribute

<jar jarfile="${outlib}/something.jar">
    <fileset dir="${build.dir}">
        <include name='src/path/to/classes' />
    </fileset>
</jar>

Again, you can use multiple includes to combine separate packages. Experiment with includes and read the documentation and you're sure to find the answer you need.

like image 52
Marcus Downing Avatar answered Nov 10 '22 20:11

Marcus Downing


Old question, but I was struggling with the same problem and found a a more elegant solution. So here it is, for future reference:

According to the ant docs the <javac> element is an implicit <fileset> and as such can take Selectors like <filename name="**/MyClass.java"/>, so this would only compile MyClass.java:

<javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="classpath">
  <filename name="**/path/to/MyClass.java"/>
</javac>
like image 3
Tor P Avatar answered Nov 10 '22 22:11

Tor P