How to add an extra sourceSet in a Gradle Groovy build?

Hi

How do I add a second Groovy build SourceSet in Gradle? I am using Gradle 1.7 at the moment and I cannot seem to get Gradle to understand exactly the specification.

I have these folders:

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

I would like Groovy in the under ‘src/templates/groovy’ to compile and be added to the build. I get an exception org.gradle.api.tasks.TaskExecutionException: Execution failed for task ‘:compileTemplatesGroovy’. Caused by: java.lang.ClassNotFoundException: groovy.lang.GroovyObject

Here is my ‘build.gradle’ file:

// Peter A. Pilgrim present the Java EE 7 Developer Handbook
apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: 'maven'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'application'
  // Define equivalent Maven GAV coordinates.
group = 'uk.co.xenonique.example'
archivesBaseName = 'example-generator'
version = '1.0-SNAPSHOT'
  mainClassName = "uk.co.xenonique.example.ExampleGenerators"
  repositories {
    mavenLocal()
    mavenCentral()
    maven {
        url 'https://maven.java.net/content/groups/promoted'
    }
    maven {
        url 'http://repository.jboss.org/nexus/content/groups/public'
    }
}
  task wrapper(type: Wrapper) {
    gradleVersion = '1.7'
}
  // 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'
        println "-----------------------------------"
        println "output.classesDir="+output.classesDir
        println "output.resourcesDir="+output.resourcesDir
        println "compileClasspath="+compileClasspath.asPath
        println "runtimeClasspath="+runtimeClasspath.asPath
        println "-----------------------------------"
        groovy {
            srcDir 'src/templates/groovy'
        }
        resources {
            srcDir 'src/templates/resources'
        }
    }
    test {
        resources {
            srcDir 'src/test/resources'
        }
          output.resourcesDir = 'build/classes/test'
        output.classesDir
 = 'build/classes/test'
    }
}
    dependencies {
    compile 'org.codehaus.groovy:groovy-all:2.0.0'
    compile 'jline:jline:1.0'
}
  jar {
    dependsOn compileTemplatesGroovy
}
  run {
    dependsOn compileTemplatesGroovy
    dependsOn processTemplatesResources
    standardInput = System.in
}
  // End

Every source set ‘foo’ has its own ‘fooCompile’ and ‘fooRuntime’ configurations. A quick fix is:

dependencies {
    templatesCompile configurations.compile
    templatesRuntime configurations.runtime
}

PS: Given the current script order, the 'println’s for ‘compileClasspath’ and ‘runtimeClasspath’ will resolve the ‘compile’ and ‘runtime’ configurations before their dependencies have been declared, which will cause troubles. Besides, configurations shouldn’t be resolved in the configuration phase, as this will slow down every invocation of Gradle.

Thanks very much Peter. This works for me.

Hi Peter

I am back 6 months later.

It would appear that the sourcesSet src/templates/groovy is not automatically add when I run a unit test.

How do I get the following?

src/main/groovy

src/template/groovy

src/test/groovy

I don’t understand the question. Perhaps start a new thread.