如何将Java代码和Junit测试与Gradle一起构建

如何将Java代码和Junit测试与Gradle一起构建

问题描述:

我有一个项目,其中主要来源和该来源的测试用例保存在相同的包/目录中.每个测试类都是要测试的类的名称,并在其末尾附加"Test".因此,如果我有一个Foo.java,它旁边将是一个FooTest.java.

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.

我的问题是,如何使用Gradle构建该项目?我仍然希望将类文件分开,即,用于主类的文件夹和用于测试类的文件夹.

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.

这应该可以解决问题:

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