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.