gradle sourceSets on mutli-project

时间:2017-02-23 16:08:01

标签: gradle source-sets

我的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 更改为完整性

我的 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,我试图理解为什么配置不适用于多项目。

1 个答案:

答案 0 :(得分:5)

我找到了解决方案。 我需要将根项目中的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) {
}