Adding sourcSets as a dependency?

In a project I have multiple sourceSets:

sourceSets {
  main {
    java {
      srcDir 'src'
    }
  }
  other {
    java {
      srcDir 'other'
    }
  }
  test {
    java {
      srcDir 'test'
    }
  }
}

Code in ‘src’ use code from ‘other’ therefore I need:

dependencies {
 compile sourceSets.other.output
}

Now in a plugin (.groovy) I would like to set this dependency dynamically. I have tried:

def compileConf = project.getConfigurations().findByName("compile")
      compileConf.getDependencies().add("sourceSets.other.output")

¨ which gives:

java.lang.String cannot be cast to org.gradle.api.artifacts.Dependency

Is it possible to do it with a Closure instead?

As the error message indicates, that method only accepts ‘Dependency’ objects. But you can do it in the same way as in a build script:

project.dependencies {
  compile project.sourceSets.other.output
}