gradle 多项目上的 sourceSets

gradle sourceSets on mutli-project

我的gradle多项目布局是这样的:

rootProject
  |-- project1
  |
  |-- project2
  |
  |-- src
  |    |-- main
  |    |    |-- groovy
  |    |    `-- resources
  |    |-- test
  |    |    |-- groovy
  |    |    `-- resources
  |    `-- sanity
  |         |-- groovy
  |         `-- resources
  |
  |---- settings.gradle
  `---- build.gradle

project1 和 project2 在 build.gradle(在 rootProject)中配置。
我想将 project1 和 project2 testClassDir 更改为 sanity.

我的build.gradle是这样的:

subprojects{
    apply plugin: "groovy"

    sourceSets {
        sanity {
           groovy.srcDir "src/sanity/groovy"
           resources.srcDir "src/sanity/resources"
        }
    }

    dependencies {
        sanityCompile sourceSets.main.output
        sanityCompile sourceSets.test.output

        sanityCompile configurations.compile
        sanityCompile configurations.testCompile

        sanityRuntime configurations.runtime
        sanityRuntime configurations.testRuntime
   }
   runTest {
        testClassesDir = sourceSets.sanity.output.classesDir
        classpath = sourceSets.sanity.runtimeClasspath
    }
}

project('project1) {
}

我已经关注了这个 Blog,我正在尝试了解为什么该配置不适用于多项目。

我找到了解决办法。 我需要将根项目中的 sourceSets 定义到新的测试文件夹。 在我需要的子项目中,将测试目录设置为 rootProject。 新的build.gradle:

// On root project
sourceSets {
    sanity {
       groovy.srcDir "src/sanity/groovy"
       resources.srcDir "src/sanity/resources"
    }
}

dependencies {
    sanityCompile sourceSets.main.output
    sanityCompile sourceSets.test.output

    sanityCompile configurations.compile
    sanityCompile configurations.testCompile

    sanityRuntime configurations.runtime
    sanityRuntime configurations.testRuntime
}

subprojects {
   apply plugin: "groovy"    

   runTest {
        // Redirect the class dir to rootProject 
        testClassesDir = rootProject.sourceSets.sanity.output.classesDir
        classpath = rootProject.sourceSets.sanity.runtimeClasspath
    }
}

project('project1) {
}

project('project2) {
}