How can I exclude sources in a javac task in ant?

33,891

Solution 1

A couple of people suggested using <exclude>. This didn't work with the way my task was specified. trashgod's answer linked to the sixth example on this page which gave me an idea of how to restructure my task specification.

It looks like my problem was related to the way I was specifying the source files. Rather than using <pathelement> elements in a <src>, like this:

<src>
   <pathelement location="${src.dir}/com/foo/bar/baz/" />
   <pathelement location="${src.dir}/com/foo/bar/quux/" />
</src>

I switched to using a single <src> with a path and then a set of <include> elements, like this:

<src path="${src.dir}" />
<include name="com/foo/bar/baz/**" />
<include name="com/foo/bar/quux/**" />

This appears to be functionally identical, but is compatible with the use of <exclude>:

<exclude name="${src.dir}/com/foo/bar/quux/dontwant/**"/>

(Actually, I'm surprised that what was there in the first place worked at all.)

Solution 2

From my experiments you should not include full path for file you want to exclude. This one doesn't work:

<javac>
(...>
   <exclude name="${src.dir}/com/foo/blah/blah1/FILENAME.java"/>
(...)
</javac>

but this one does:

<javac>
(...>
   <exclude name="com/foo/blah/blah1/FILENAME.java"/>
(...)
</javac>

Solution 3

I'm not sure about the rest, but the <exclude/> nested element should work in the Javac task. See the sixth example down.

Addendum: Patterns, including the ** notation, are discussed in Directory-based Tasks.

<target name="compile.baz" depends="init">
    <javac destdir="${build.dir}/classes" debug="on">
        <compilerarg value="-Xlint:deprecation"/>
        <src>
            <pathelement location="${src.dir}/com/foo/bar/baz/" />
            <pathelement location="${src.dir}/com/foo/bar/quux/" />
        </src>
        <exclude name="${src.dir}/com/foo/bar/quux/dontwant/**"/>
        ...
    </javac>
    ...
</target>

Solution 4

Try

<javac>
(...>
<exclude name="${src.dir}/com/foo/bar/quux/dontwant/*" />
(...)
</javac>

Solution 5

try folder "test" under {source.dir} would not be complied

<javac destdir="${compile.dir}"  ...>
   <src path="${source.dir}" />
   <exclude name="test/**"/>
</javac>
Share:
33,891

Related videos on Youtube

Laurence Gonsalves
Author by

Laurence Gonsalves

I'm the founder of Niphtio. If you'd like to work with me, we're hiring! When it comes to computing, I'm a bit of a generalist: I'm interested in programming language design, compilers, computer graphics, machine learning, robotics, games, and more. I regularly use Python, Kotlin, Java and C++, and am also a fan of Scheme. I like functional programming and static typing, but am not really a fan of Haskell... go figure. My editor of choice is Vim. I grew up with a Commodore 64, then a 128 and eventually had an Amiga. These days I primarily use Linux.

Updated on July 09, 2022

Comments

  • Laurence Gonsalves
    Laurence Gonsalves almost 2 years

    I have the following in my build.xml:

    <target name="compile.baz" depends="init">
       <javac destdir="${build.dir}/classes" debug="on">
          <compilerarg value="-Xlint:deprecation"/>
          <src>
             <pathelement location="${src.dir}/com/foo/bar/baz/" />
             <pathelement location="${src.dir}/com/foo/bar/quux/" />
             <!-- Need to exclude ${src.dir}/com/foo/bar/quux/dontwant/ -->
          </src>
          <classpath refid="classpath.jars" />
       </javac>
       ...
    </target>
    

    This mostly does what I want, except that (as the comment says) I do not want the files in
    ${src.dir}/com/foo/bar/quux/dontwant/ to be compiled by this task (but I do want everything else under ${src.dir}/com/foo/bar/quux/ to be compiled in this task).

    I'm a complete ant n00b, and the documentation hasn't been much help to me. I see several places where it says there are various exclude/excludes elements/attributes, but every variation I can think of either has no effect or results in an error like "blah doesn't support the 'exclude' attribute".

  • Laurence Gonsalves
    Laurence Gonsalves almost 14 years
    Nope, that doesn't work. It still tries to compile those files.
  • Laurence Gonsalves
    Laurence Gonsalves almost 14 years
    Aside from the extra *, that's the same thing matiasf said. Just to be sure I tried it anyway, but it doesn't work. The example you linked to helped me figure out something that works, though.
  • trashgod
    trashgod almost 14 years
    Yes, +1 to @matiasf. I also wanted show that <exclude/> is nested in javac, but not in <src/>. I added a link to the section on **, because I can never find it when I need it. :-)