How to specify output.classesDir for a custom source in Gradle?

My assembly uses the source code from two projects: ProjectA and ProjectB, and produces a JAR with classes and resources from ProjectB. I defined a custom sourceSet mainProjectB that should 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 uses the specified output directories. The directories $ buildDir / build / classes / projectB and $ buildDir / build / resources / projectB are not created, and as a result, the JAR includes files from both projects (instead of ProjectB).

UPDATE:
Projects A and B have circular dependencies. That is why they should share the source code.

+4
source share
1

- gradel docs. jar :

group 'CoreProject'
version '1.0-SNAPSHOT'
subprojects {
    apply plugin: 'java'
    repositories {
        mavenCentral()
    }
}

project (':projectA') {

}

project (':projectB') {
    def generatedResources = "$buildDir"
    //in case you want resources and classes to be written to custom location where 
    //redefined paths are relative to projectB root folder 
    sourceSets {
        main {
            output.classesDir = 'build/classes/projectB'
            output.resourcesDir = 'build/resources/projectB'
        }
    }
    dependencies {
        compile project(':projectA')
    }
    jar {
        manifest.mainAttributes(
                'Main-Class': "ProjectBClass"
        )
    }

    //To create fat Jar that will contain classes and resources from all dependencies
    task fatJar(type: Jar) {
        manifest.from jar.manifest
        classifier = 'all'
        from {
            configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) }
        } {
            exclude "ProjectAResource" //if want to exclude resources from projectA
            exclude "META-INF/*.SF"
            exclude "META-INF/*.DSA"
            exclude "META-INF/*.RSA"
        }
        with jar
    }
}

jar task B, , : projectA/build/libs/projectA.jar, projectB/build/libs/projectB.jar ( "JAR ProjectB", )

farJar B, jar, , projectA projectB, : projectB/build/libs/projectB-all.jar

, ( ): enter image description here

P.S. , projectB/build projectA/build - , , Gradle .

+1

All Articles