How to specify output.classesDir for custom sourceSet in Gradle?

My build uses source code from two projects: ProjectA and ProjectB, and produces JAR with classes and resources from ProjectB. I defined custom sourceSet mainProjectB which is supposed to have output in a separate directory:

sourceSets {
    mainProjectB {
        output.classesDir = "$buildDir/build/classes/projectB"
        output.resourcesDir = "$buildDir/build/resources/projectB"
        java { srcDirs = ['src/main/java']}
        resources { srcDirs = ['src/main/resources']}
    }
    mainProjectA {
        java { srcDirs = [
                '../projectA/src/main/java'
            ]}
        resources { srcDirs = [
                '../projectA/src/main/resources'
            ]}
    }
    test {
        java {
            srcDirs = [
                '../projectA/src/test/java',
                'src/test/java'
            ]}
        resources {
            srcDirs = [
                '../projectA/src/test/resources',
                'src/test/resources'
            ]}
    }
}

compileJava {
    source sourceSets.mainProjectB.allJava
    source sourceSets.mainProjectA.allJava
}

processResources {
    from sourceSets.mainProjectB.resources
    from sourceSets.mainProjectA.resources
}

jar {
    from sourceSets.mainProjectB.output.classesDir
    from sourceSets.mainProjectB.output.resourcesDir
}

Problem: custom sourceSet mainProjectB ingores specified output directories.
The directories “$buildDir/build/classes/projectB” and “$buildDir/build/resources/projectB” are not created, and as a consequence, JAR includes files from both projects (instead of ProjectB).