title: Gradle: package multi-project into a single jar
body: <p>I have a gradle multi-project and want to create a single jar (library) containing all the classes of my subprojects and external dependencies. </p>
  
  <p>I have the following project structure. Each project has its own 3rd party dependencies. Common dependencies are included in the root project. The two modules A and B are dependent on the core.</p>
  
  <pre><code>+ root-project (only build.gradle and settings.gradle)
    - core       (src/main/java, src/main/resources, ..)
    - module-A   (src/main/java, src/main/resources, ..)
    - module-B   (src/main/java, src/main/resources, ..)
  </code></pre>
  
  <p>To export a single jar, I added the following task to the build.gradle of the root project:</p>
  
  <pre><code>apply plugin: "java"
  
  subprojects.each { subproject -&gt; evaluationDependsOn(subproject.path)}
  
  task allJar(type: Jar, dependsOn: subprojects.jar) {
      baseName = 'multiproject-test'
      subprojects.each { subproject -&gt;
          from subproject.configurations.archives.allArtifacts.files.collect {
              zipTree(it)
          }
      }
  }
  
  artifacts {
      archives allJar
  }
  </code></pre>
  
  <p>This approach works, but does only collect the project source files. The 3rd party dependencies are ignored. So I tried out the Shadow Plugin (<a href="http://imperceptiblethoughts.com/shadow/" rel="nofollow noreferrer">http://imperceptiblethoughts.com/shadow/</a>) which should also include external dependencies.</p>
  
  <p>Unfortunately the plugin does not collect anything at all. This is most probably due to missing dependencies between the root project and its sub projects. How can I tell the shadow plugin, that it should collect the sources of the subprojects? Or is there a better approach to export a single library out of multiple projects?</p>
  
  <p>complete build.gradle using the shadow plugin:</p>
  
  <pre><code>/****************************************
   * instructions for all projects
   ****************************************/
  allprojects {
      apply plugin: 'idea'
      apply plugin: 'eclipse'
  
      group = 'com.test.multi-project'
      version = '1.0'
  }
  
  /****************************************
   * instructions for each sub project
   ****************************************/
  subprojects {
      apply plugin: "java"
  
      sourceCompatibility = 1.9
      targetCompatibility = 1.9
  
      repositories {
          mavenCentral()
      }
  
      dependencies {
          compile "org.slf4j:slf4j-api:1+"
          compile "ch.qos.logback:logback-core:1+"
          compile "ch.qos.logback:logback-classic:1+"
  
          testCompile "junit:junit:4+"
      }
  }
  
  /****************************************
   * Single jar out of all sub projects
   ****************************************/
  buildscript {
      repositories {
          jcenter()
      }
      dependencies {
          classpath 'com.github.jengelman.gradle.plugins:shadow:2.0.2'
      }
  }
  
  apply plugin: 'com.github.johnrengelman.shadow'
  apply plugin: 'java'
  
  shadowJar {
      baseName = 'multiproject-test'
  } 
  </code></pre>
  
  <p>The submodules are included in the settings.gradle of the root project</p>
  
  <pre><code>rootProject.name = 'myproject-root'
  
  // submodules
  include ":core"
  
  include ":module-A"
  include ":module-B"
  </code></pre>
  
  <p>Thanks for your help!</p>