Gradle中的SourceSet理解

對於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 setsapp

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

mainthis

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

相關文章
相關標籤/搜索