Search code examples
gradlesource-sets

gradle sourceSets on mutli-project


My gradle multi-project layout is so:

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

project1 and project2 are configured in the build.gradle (in rootProject).
I want to change project1 and project2 testClassDir to sanity.

My build.gradle is so:

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) {
}

I've followed this Blog, I'm trying to understand why the configuration doesn't work on multi-project.


Solution

  • I've found the solution. I needed to define the sourceSets in the root project to the new test folder. In the subprojects I needed set the test directory to the rootProject. The new 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) {
    }