Classes in buildSrc throws NoClassDefFoundError on dependencies defined in gradle script

Hi, I have the following gradle script:

buildscript {
    repositories {
    dependencies {
        classpath(group: 'commons-io', name: 'commons-io', version: '2.0.1')
task helloCommons << {
    new MyWriter().write()

and a basic class in buildSrc/src/main/groovy/MyWriter.groovy

  class MyWriter {
    void write() {
        FileUtils.writeStringToFile(new File('test.txt'),"hello")

When running gradle helloCommons it throws a NoClassDefFoundError

Build file '/home/detlef/develop/gradle/my.gradle' line: 11
  * What went wrong:
Execution failed for task ':helloCommons'.
Cause: java.lang.NoClassDefFoundError:

When writing in build.gradle:

task helloCommons << { File('test.txt'),"hello")

it work, but why doesnt it work when extracting that to a class ?

Can somebody spot my problem here ? Thanks


You need to add a ‘build.gradle’ for the ‘buildSrc’ project, and add commons-io as a ‘compile’ dependency.

Thanks, this work perfect.