How can I exclude certain java files from being compiled?


(Gradle FAQ Bot) #1

I want certain files in my source tree not to be compiled by Gradle.

How can I configure that?


(Luke Daley) #2

The source set is what specifies what source is to be compiled, and you can configure that with something like:

sourceSets {
    main {
        java {
            srcDir 'src'
            exclude '**/uncompilable/**'
       }
   }
}

(A little bit about source sets here)

Here we are configuring the java SourceDirectorySet of the main SourceSet, which is what is used as the description of what to compile.

Take a look at the documentation for PatternFilterable (which is the interface that SourceDirectorySet) implements for all the different ways that you can exclude/include files in the source.


How can I declare an exclusion on my source folder?
(Gururaj R) #3

Based on the above snippet I tried (on Gradle 1.7)

sourceSets {
    main {
        java {
            srcDir 'src'
            exclude 'src/test/**'
       }
   }
}

But it fails to exclude the src/test folder.


(Gururaj R) #4

my bad, it should be

sourceSets {
    main {
        java {
            srcDir 'src'
            exclude 'test/**'
       }
   }
}

Now works perfect.