Access sourceSets of another projects in multiproject build

Goal:

  • Copy source files from all related projects into one folder (no compromise on this one, those files will be used for other stuff as well)
  • Do a JavaCompile from that folder and then make jar from result

Problem
I am trying to copy source files of :libs:p2p and other projects. p2p project has sourceSets configured in its own build.gradle.
When I try to access project(":libs:p2p").sourceSets from :module:server build.gradle I get error that sourceSets is not recognized.
Same happens if i try to add dependency of a task from that project.

// in `:module:server` build.gradle
task copyAllSource(type: Copy) {
    from project(":libs:p2p").sourceSets.main.java.srcDirs // this fails
    into file("target_dir")
}

// this doesnt work either
task compileAllFiles(type: JavaCompile) {
     dependsOn(project(":libs:p2p").tasks.named("copyToShared")) // fails with task the name not found error
}

I think this has something to do with afterEvaluate but that would be a bit to complicated. May be I need to evaluate something more lazily here. I am out of ideas here.