Gradle中的SourceSet理解

对于maven项目来说,目录结构是固定的,也就是像这样:

src/main/
    src/main/java/
    src/main/resources/

src/test/
    src/test/java/
    src/test/jresources/

Gradle也是一样的,他也有一个约定的目录结构,格式和maven的结构一样。但不同的是,gradle的目录结构是可以改的,官网中叫做Changing the project layout。怎么改,或者说怎么自定义?这就要用到SourceSet了。

SourceSet到底是什么,官网中像下面这样解释的,大意是说SourceSet包括源文件及位置、它们的依赖、编译输出的位置这三个概念,SourceSet可以把不同文件目录里的文件按照逻辑关系组织起来。具体请参考Declaring your source files via source sets

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!

Source sets are a powerful concept that tie together several aspects of compilation:

  • the source files and where they’re located

  • the compilation classpath, including any required dependencies (via Gradle configurations)

  • where the compiled class files are placed

Gradle有两个SourceSet是默认必须有的,这两个SourceSet的特殊性在于,你不需要在build文件中去申明它们,并且,操作它们的时候,也不需要带上他们的名字,比如你编译的时候,只需要compile,测试的时候,只需要test,而不是compilemain,testtest之类的。

main

Contains 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

https://docs.gradle.org/current/userguide/building_java_projects.html#sec:custom_java_source_set_paths

https://blog.csdn.net/honjane/article/details/52579304

 

posted on 2019-04-19 15:00  J.M.Liu  阅读(4612)  评论(0编辑  收藏  举报