Another 'fat Jar' question - multiple subprojects one jar

Hi,

I found a bunch of ‘fatJar’ questions all over the internet and tried to implement my own solution. I can’t get it working however. This is what I currently have;

def allJarSubprojects = subprojects.findAll {
    it.name == 'subproject1'    ||
    it.name == 'subproject2     ||
    it.name == 'subproject3'
}

// We need to make sure the relevant subprojects are evaluated first.
allJarSubprojects.each { subproject -> evaluationDependsOn(subproject.path) }

// Actual task producing the fat jar.
task allJar(type: Jar) {
    baseName = 'fatjar'
    destinationDir = new File('./lib')

    // set dependencies of this allJar task
    allJarSubprojects.each { subproject -> dependsOn subproject.tasks["classes"] }

    // create a jar from all the subproject's artifacts
    allJarSubprojects.each { subproject ->
        // no property 'allArtifactFiles'
        //from subproject.configurations.archives.allArtifactFiles.collect { zipTree(it) } 

        // works but doesn't pack the META-INF or other extra folders
        //from subproject.sourceSets.main.output

       // works but packs all dependencies of the subprojects too which we don't want
       //from subproject.configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
    }
}

I am trying to keep the list of subprojects to be packed easy to configure so I can do it from the properties file later on.

The second line I have (based on sourceSets) works but doesn’t include some special META-INF files from some subprojects ( like a gradle-plugins folder ).

The third approach packs everything, all dependencies of the subprojects too, into one gigantic Jar. This is not the objective, I only want the subproject’s files in there.

It feels like the ‘allArtifactFiles’ approach would do that but I can’t get that one working.

You should be able to use subproject.configurations.archives.artifacts.files (see dsl reference for Configuration).

And you’ll probably want to depend on the ‘jar’ task of subprojects instead of classes - classes just assembles the classes and resources but not the jar.