对于maven项目来讲,目录结构是固定的,也就是像这样:html
src/main/
src/main/java/
src/main/resources/
src/test/
src/test/java/
src/test/jresources/
Gradle也是同样的,他也有一个约定的目录结构,格式和maven的结构同样。但不一样的是,gradle的目录结构是能够改的,官网中叫作Changing the project layout。怎么改,或者说怎么自定义?这就要用到SourceSet了。java
SourceSet究竟是什么,官网中像下面这样解释的,大意是说SourceSet包括源文件及位置、它们的依赖、编译输出的位置这三个概念,SourceSet能够把不一样文件目录里的文件按照逻辑关系组织起来。具体请参考Declaring your source files via source sets。app
Gradle’s Java support was the first to introduce a new concept for building source-based projects: source sets. The main idea is that source files and resources are often logically grouped by type, such as application code, unit tests and integration tests. Each logical group typically has its own sets of file dependencies, classpaths, and more. Significantly, the files that form a source set don’t have to be located in the same directory!maven
Source sets are a powerful concept that tie together several aspects of compilation:ide
the source files and where they’re located单元测试
the compilation classpath, including any required dependencies (via Gradle configurations)测试
where the compiled class files are placedgradle
Gradle有两个SourceSet是默认必须有的,这两个SourceSet的特殊性在于,你不须要在build文件中去申明它们,而且,操做它们的时候,也不须要带上他们的名字,好比你编译的时候,只须要compile,测试的时候,只须要test,而不是compilemain,testtest之类的。ui
main
thisContains the production source code of the project, which is compiled and assembled into a JAR.
test
Contains your test source code, which is compiled and executed using JUnit or TestNG. These are typically unit tests, but you can include any test in this source set as long as they all share the same compilation and runtime classpaths
SourceSet有一些属性,好比名字、源文件位置、资源位置、classpath等等,这些属性有些是只读的,有些是可写的,而且能够被看成变量来引用。有可写的属性那么就提供了自定义的功能了,好比,你能够改变一个SourceSet源代码的位置,像这样下面这样,你就改变了main这个SourceSet的源代码目录和资源目录。
sourceSets { main { java { srcDirs = ['src/java'] } resources { srcDirs = ['src/resources'] } } }
这样,gradle就只在src/java下搜源代码,而不是在src/main/java下。若是你只是想添加额外的目录,而不想覆盖原来的目录,则像下面这样:
sourceSets { main { java { srcDir 'thirdParty/src/main/java' } } }
此时,gradle就会同时在src/main/java和thirdParty/src/main/java两个目录下都进行源代码搜索。
除了能够更改原有的SourceSet,你也能够添加一个新的SourceSet。好比咱们在test这个SourceSet中,作的通常都是单元测试,若是咱们要作集成测试和验收测试,由于这些测试有它们本身的dependencies, classpaths, 以及其余的资源,因此这些测试应该是单独分开的,而不该该和单元测试混为一谈。可是,集成测试和验收测试也是这个项目的一部分,所以咱们也不能为集成测试或验收测试单独创建一个项目。它们也不是子项目,因此用subproject也不合适。此时,咱们就能够新建一个集成测试或者验收测试的SourceSet,把相关的测试资源管理起来。好比你须要创建一个集成测试,则你定义以下SourceSet:
sourceSets { integrationTest {
java
resource compileClasspath += sourceSets.test.runtimeClasspath runtimeClasspath += sourceSets.test.runtimeClasspath } }
而后,你添加一个集成测试的运行任务:
task integrationTest(type: Test) { testClassesDir = sourceSets.integrationTest.output.classesDir classpath = sourceSets.integrationTest.runtimeClasspath }
在这个任务中,你指定了测试的目录为集成测试SourceSet的输出目录,classpath为集成测试的runtimeClasspath,这样,集成测试就能够运行了。
参考:
https://docs.gradle.org/current/userguide/java_plugin.html#sec:java_source_sets