Custom JavaCompile task on a set of files returns NO-SOURCE

I have a multi-project setup. Each project has it’s own sourceSets{main{java{srcDirs=["src/main/java"]}}. From the :module:server project, which also has its own sourceSet configured, I am copying source files of all other projects into one allsources directory and then trying to build from that directory.

I made a custom JavaCompile task as an equivalent of

> cd allsources
> javac -classpath . ./com/myapp/

These are the tasks I made to do above

task copyAll(type: Copy) {
    from "${project.projectDir}/src/main/java"
    from "${project(":module:lib1").projectDir}/src/main/java"
    from "${project(":module2:lib1").projectDir}/src/main/java"
    from "${project(":module2:lib2").projectDir}/src/main/java"
    into "$rootProject.buildDir/allsources"

task compileFatJar(type: JavaCompile) {
    // files(copyAll).asPath returns the path to 'allsources' directory from the copy task
    source = files("${files(copyAll).asPath}")
    classpath = files("${files(copyAll).asPath}")
    include "${files(copyAll).asPath}/com/myapp/"

    // options.sourcepath = files("${files(copyAll).asPath}") // don't know how this can help

    destinationDir = "$rootProject.buildDir/compiledAllSources"

Now when I run this it results in NO-SOURCE and does not do anything at all. How to fix this?

(I know can do a project.exec to run javac as the last resort)

I saw somewhere that source property in JavaCompile can accept a single file. I removed the include and set source to and it worked

task compileAll(type: JavaCompile) {

    source = files("${files(copyAll).asPath}/com/myapp/")
    classpath = files("${files(copyAll).asPath}")
    options.sourcepath = files("${files(copyAll).asPath}") // this makes it work correctly

    destinationDir = "$rootProject.buildDir/compiledAllSources"

and for final jar with all dependencies which were copied in one directory

task fatJar(type: Jar) {

from "$rootProject.buildDir/compiledAllSources"
destinationDir = file("$rootProject.buildDir/jar")


It’s not clear from the docs what is the difference between source and options.sourcepath. It would be helpful if someone can explain that.