StefMa
StefMa

Reputation: 3434

gradle dependencie managment with sub dependencies (nested depenencies)

I have three projects

../server/modelProject/
../client/apiProject/
../client/appProject/

The apiProject need to depends on modelProject and appProject need to depends on apiProject. Api need the models and the app need the api. Just a normal thing, i think :)

My modelProject have nothing special.

The apiProject setup looks like this:

settings.gradle

include ':app', 'models'
project(':models').projectDir = new File('../../server/modelProject')

build.gradle

dependencies {
    compile project(':models')
}

Last my appProject:

settings.gradle

include ':app', ':api'
project(':api').projectDir = new File("../client/apiProject")

build.gradle

dependencies {
    compile project(':api')
}

I expected that my appProject on which I'm working on, depend on api which depends on model so that I can use all models classes in my appProject too. But it isn't so.

I get the following error:

Project with path :models could not be found in projects ':api'.

So solve these problem I can expand the appProjects settings.gradle to:

include ':app', ':api', ':models'
project(':api').projectDir = new File('../client/apiProject')
project(':models').projectDir = new File('../../server/modelProject')

But this isn't a nice solution and not what I expected.

So my question: It this the normal behavior of gradle? Or do I make something wrong? Can I make any changes that appProject only depends on apiProject which will automatically include the `modelProject?

Upvotes: 3

Views: 257

Answers (3)

CkurtM
CkurtM

Reputation: 195

you need 1 top level settings.gradle in the root folder containing all of your modules if you're going to create dependencies between modules

Upvotes: 0

adnbsr
adnbsr

Reputation: 635

Just one settings.gradle is enough. Your project file tree should be something like:

Project

  • client
    • apiProject
      • build.gradle
    • appProject
      • build.gradle
  • server
    • build.gradle
  • setings.gradle
  • build.gradle

settings.gradle ->

include ':app', ':api', ':models'

project(':app').projectDir = new File(rootProject.projectDir,'client/appProject')
project(':api').projectDir = new File(rootProject.projectDir,'client/apiProject')
project(':models').projectDir = new File(rootProject.projectDir,'server/modelProject')

And your dependency flow is you explained.

Upvotes: 0

wojtek.kalicinski
wojtek.kalicinski

Reputation: 1100

Your first settings.gradle file will not be used. From the gradle docs:

A multiproject build must have a settings.gradle file in the root project of the multiproject hierarchy. It is required because the settings file defines which projects are taking part in the multi-project build

So your second approach with listing all projects in one settings.gradle file is correct.

Upvotes: 1

Related Questions