How to Add Another Groovy SourceSet Dependency to Runtime and Test Class Paths

I reopened a project from 6 months ago.This is the structure

src/main/groovy
src/template/groovy
src/test/groovy

I found that the unit tests are broken (src/test/groovy), because the class path is not picking up code from src/template/groovy

This is my gradle file:

apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: 'maven'
apply plugin: 'eclipse'
apply plugin: 'idea'
  group = 'uk.co.xenonique.acme'
archivesBaseName = 'acme'
version = '1.0-SNAPSHOT'
  repositories {
    mavenLocal()
    mavenCentral()
}
  task wrapper(type: Wrapper) {
    gradleVersion = '1.11'
}
  // Override Gradle defaults - a force an exploded JAR view
sourceSets {
    main {
        output.resourcesDir = 'build/classes/main'
        output.classesDir
 = 'build/classes/main'
        groovy {
            srcDir 'src/main/groovy'
        }
        resources {
            srcDir 'src/main/resources'
        }
    }
    templates {
        output.resourcesDir = 'build/classes/main'
        output.classesDir
 = 'build/classes/main'
        groovy {
            srcDir 'src/templates/groovy'
        }
        resources {
            srcDir 'src/templates/resources'
        }
    }
    test {
        output.resourcesDir = 'build/classes/test'
        output.classesDir
 = 'build/classes/test'
        groovy {
            srcDir 'src/test/groovy'
        }
        resources {
            srcDir 'src/test/resources'
        }
    }
}
  dependencies {
    // http://forums.gradle.org/gradle/topics/how_to_add_an_extra_sourceset_in_a_gradle_groovy_build
    templatesCompile configurations.compile
    templatesRuntime configurations.runtime
      compile 'org.codehaus.groovy:groovy-all:2.0.0'
    compile 'jline:jline:1.0'
    compile 'org.apache.ant:ant:1.8.4'
    compile 'org.apache.ant:ant-launcher:1.8.4'
    compile 'org.fusesource.jansi:jansi:1.11'
    compile 'org.fusesource.jansi:jansi-native:1.5'
      testCompile 'junit:junit:4.11'
}

Also when I execute: gradle idea

It generate source sets as:

src/main/groovy
src/template/java
src/test/groovy

It gets the wrong source set for template. It is groovy not java.

Have you noticed the ‘template’ vs. ‘templates’?

I wish it was this simple? The real file paths are

src/main/groovy
src/main/resources
src/templates/groovy
src/templates/resources
src/test/groovy
src/test/resources

The code in src/test/groovy cannot be linked dynamically to code in src/templates/groovy.

And those match the gradle.build file. I went back to the Gradle-all bundle and look at the example

vi /Library/opt/gradle-1.11/samples/java/withIntegrationTests/build.gradle

This stanza

dependencies {
    templatesCompile configurations.compile
    templatesRuntime configurations.runtime
      ...
}

and that stanza

dependencies {
    integrationTestCompile sourceSets.main.output
    integrationTestCompile configurations.testCompile
    integrationTestCompile sourceSets.test.output
    integrationTestRuntime configurations.testRuntime
}

is different. Am I missing more configuration?

[SOLVED]

I attacked the issue differently. Why are the paths not been correctly? Because the paths are not configured in Gradle.

Gradle has to be told what the configuration is.

dependencies {
    // http://forums.gradle.org/gradle/topics/how_to_add_an_extra_sourceset_in_a_gradle_groovy_build
    templatesCompile configurations.compile
    templatesRuntime configurations.runtime
    testCompile sourceSets.templates.output
    testRuntime sourceSets.templates.output
      ...
}

Tell Gradle that the test sourceset is dependent on the templates sourceset for both the compile and runtime scopes!

I found the following task very useful for debugging the issue

task debugme << {
    sourceSets.each {
        println "sourceSet.name = "+it.name
        println "
  output.classesDir = "+it.output.classesDir
        println "
output.resourcesDir = "+it.output.resourcesDir
        println "
   compileClasspath = "+it.compileClasspath.asPath
        println "
   runtimeClasspath = "+it.runtimeClasspath.asPath
        println ""
    }
}

Thanks for your time, Peter

2 Likes