Search code examples
javaeclipsegradlebuild.gradlemulti-project

Add multi project itself as a dependency in another project


After a lot of searching, it was the last option to raise it here! In eclipse, I am designing such project structure using Gradle, as shown below...

Algorithms              //Parent Project
    -SubModuleCore      //Child Project for common utilities & dependencies
        -build.gradle
    -SubModuleOne       //Child project for any operation
        -build.gradle   //Added 'SubModuleCore' as a dependency like compile project(':SubModuleCore')
    -SubModuleTwo       //Child project for another operation
        -build.gradle   //Added 'SubModuleCore' as a dependency like compile project(':SubModuleCore')
    -build.gradle
    -settings.gradle

Services                //Stand-Alone project
    -build.gradle       //Here I want to add 'Algorithms' as a single dependency
    -settings.gradle

Project structures are same in eclipse work-space as shown above. I am able to generate individual .jar of Algorithms project. So the problem is I want to add this Algorithms project as a single dependency in project Services like compile project(':Algorithms'). But eclipse just saying 'shut-up!'. I don't want to publish it somewhere like maven central / jitpack etc. instead I want to do it locally. I'm trying this way...

Services/build.gradle

apply plugin: 'war'
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'eclipse-wtp'
project.webAppDirName = 'WebContent'

repositories {
    jcenter()
    mavenCentral()
}

dependencies {
    compile fileTree(dir: 'lib', include: ['*.jar'])
    compile project(':Algorithms')
}

Services/settings.gradle

rootProject.name = 'Services'
include 'Algorithms'
project(':Algorithms').projectDir = new File(settingsDir, '../Algorithms')

Algorithms/build.gradle

apply plugin: 'java'
apply plugin: 'eclipse'

sourceCompatibility = "1.8";
targetCompatibility = "1.8";

subprojects {
    apply plugin: 'java'
    apply plugin: 'eclipse'

    sourceCompatibility = "1.8";
    targetCompatibility = "1.8";

    buildscript {
       dependencies {
          repositories {
            jcenter()
             mavenCentral()
          }
        }
    }

    repositories {
        jcenter()
        mavenCentral()
    }

}
subprojects.each { subproject -> 
   evaluationDependsOn(subproject.path) 
}
jar.dependsOn subprojects.tasks['classes']
jar {
  baseName = 'algorithms'
  subprojects.each { subproject ->
    from subproject.sourceSets.main.output.classesDir
  }
  from files('resources/log4j2.xml')
  from files('resources/application.properties')
}

Algorithms/settings.gradle

rootProject.name = 'Algorithms'
include ':SubModuleCore', ':SubModuleOne', ':SubModuleTwo'

I tried several solutions from SO, still not succeeded. Somebody please help me, I got stuck badly here. It seems I am very close to this, but don't know what is missing!

Thanks


Solution

  • You can use the includeBuild feature.

    Declare the included build in Services/settings.gradle

    rootProject.name='Services'
    
    includeBuild '../Algorithms'
    

    and then express the dependency using

    compile "${project.group}:${project.name}"
    

    where project group and name the one from the Algorithms project.