buildSrc && The Spock JUnit runner cannot execute because Spock 0.7.0-groovy-2.0 is not compatible with Groovy 1.8.6

I want to add spock to my buildSrc code. After adding the spock dependencies, I get the above error when I run ‘gradle test’.

If I rename buildSrc to any other name, it works, spock runs, no groovy version conflict.

If I rename buildSrc to foo, cd foo and gradle test, it works. If I rename foo back to buildSrc, cd buildSrc and gradle test, it fails with the version problem.

What is special about the folder being named buildSrc and how does that affect the groovy version?

Here’s my buildSrc/build.gradle

apply plugin: 'groovy'
apply plugin: 'eclipse'
   repositories {
    mavenCentral()
    maven { url "http://oss.sonatype.org/content/repositories/snapshots/" }
}
  dependencies {
      compile(group: 'com.google.code.gson', name: 'gson', version: '2.2.4')
    compile(group: 'org.slf4j', name: 'slf4j-api', version: '1.7.5')
    compile(group: 'commons-lang', name: 'commons-lang', version: '2.6')
    compile(group: 'commons-io', name: 'commons-io', version: '2.4')
    compile(group: 'commons-collections', name: 'commons-collections', version: '3.2.1')
    compile(group: 'org.apache.pdfbox', name: 'pdfbox', version: '1.8.2')
    compile(group: 'com.nativelibs4java', name: 'bridj', version: '0.6.2')
    compile(group: 'com.typesafe', name: 'config', version: '1.0.2')
   // mandatory dependencies for using Spock
 groovy "org.codehaus.groovy:groovy-all:2.1.7"
 testCompile "org.spockframework:spock-core:1.0-groovy-2.0-SNAPSHOT"
   // optional dependencies for using Spock
 testCompile "org.hamcrest:hamcrest-core:1.3" // only necessary if Hamcrest matchers are used
 testRuntime "cglib:cglib-nodep:2.2.2"
      // allows mocking of classes (in addition to interfaces)
 testRuntime "org.objenesis:objenesis:1.2"
  // allows mocking of classes without default constructor (together with CGLIB)
}

Gradle tasks and plugins always get executed against Groovy 1.8, and therefore should also be compiled against 1.8. That said, I’m not quite sure why 1.8 ends up on your test runtime class path with the build script given above.

Note that the ‘groovy’ configuration has been deprecated in recent Gradle versions. Instead, the Groovy dependency should now be added to the ‘compile’ configuration. Or, if Groovy is only used for testing, to the ‘testCompile’ configuration.

You’re getting Groovy 1.8 because the following is implicitly configured for all buildSrc projects:

https://github.com/gradle/gradle/blob/release/subprojects/core/src/main/resources/org/gradle/initialization/buildsrc/defaultBuildSourceScript.txt#L4

And, there’s no conflict resolution with ‘localGroovy()’.

You should drop your explicit Groovy dependency and use:

testCompile “org.spockframework:spock-core:1.0-groovy-1.8-SNAPSHOT”

I worked around it by moving my tests into the parent project so they were not impacted by the buildSrc implicit config that Luke indicated.

Thanks, this solved my problem.