Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 55189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:20:18+00:00 2026-05-10T17:20:18+00:00

Say I have my sources in my src/ tree (and possibly in my test/

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T17:20:19+00:00Added an answer on May 10, 2026 at 5:20 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a project like that: (dev dir) - README - INSTALL
I have a gradle project that contains only Selenium/TestNG test classes. They are executed
I want to record what various sources have to say about a historical figure.
Say I have a build machine and test machine and the source code is
Let's say I have this directory structure: SConstruct src/ a.cpp b.cpp include/ a.h b.h
In my project, I have a resources directory ( src/main/resources ) that contains properties
Say I have two complex nested arrays in PHP, like these: $a = array(
Most of our Eclipse projects have multiple source folders, for example: src/main/java src/test/java When
I would like to retrieve ONLY the library jar when I make use of
I am using ANT to compile an Android app that I have built. It

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.