How can I exclude certain java files from being compiled?

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

How can I configure that?

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.

1 Like

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.

my bad, it should be

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

Now works perfect.

2 Likes