How create jar from 2 subprojects?


(dusiabh) #1

I have legacy project with following configuration:

dir/dir2/dir3/dir4/my_code1 dir/dir2/dir3/dir4/my_code2

I need to have following jars:

jar1: dir/dir2/dir3/dir4/my_code1

jar2: dir/dir2/dir3/dir4/my_code1 dir/dir2/dir3/dir4/my_code2

settings.gradle:

include 'jar1_proj', 'jar2_proj'
project(":jar1_proj").projectDir = new File (settingsDir, "dir/dir2/dir3/dir4/my_code1")
project(":jar2_proj").projectDir = new File (settingsDir, "dir/dir2/dir3/dir4/my_code2")

build.gradle

subprojects {
 apply plugin: 'java'
}
  project(':jar1_proj') {
 sourceSets {
  main{
   java {
                   srcDirs = ['.']
    }
  }
     }
   project(':jar2_proj') {
 sourceSets {
  main{
   java {
                   srcDirs = ['.']
    }
  }
     }
       dependencies {
           compile project(':jar1_proj')
      }
         task newJar(type: Jar) {
            archiveName = 'jar2'
            from sourceSets.main.output
             from
subprojects.jar1_proj.sourceSets.main.output
      }
}

But the jar2 contains only classes from jar2_proj’. How I can include classes from 2 subprojects in one jar?


(dusiabh) #2

I’m so cool! I found the solution - you must go one level up (rootProject) to get the access to subprojects!

task newJar(type: Jar) {
            archiveName = 'jar2'
            from sourceSets.main.output
             from getRootProject().subprojects.findAll{it.name == 'jar1_proj'}.sourceSets.main.output
}