How to keep Java code and Junit tests together building with Gradle

gradle

Solution

This should do the trick:

sourceSets {
    main {
        java {
            srcDirs = ["some/path"]
            exclude "**/*Test.java"
        }
    }
    test {
        java {
            srcDirs = ["some/path"]
            include "**/*Test.java"
        }
    }
}

Problem

I have a project in which the main source and the test cases for that source are kept in the same package/directory. Each test class is the name of the class which it is testing with "Test" appended on the end. So if I have a Foo.java there will be a FooTest.java right next to it. My question is, how do I build this project with Gradle? I'd still like to keep the class files separate, i.e. a folder for main classes and a folder for test classes.

Original source