4

I want to extend a configuration in project b with a configuration in project a.

If both configurations are in the same project it is possible to do:

    configurations.conf1.extendsFrom configurations.conf2

How do I achieve the same for configurations in different projects?

Below is an minimal example that currently does not work since the dependencies are not inherited.

Example folder setup:
a/build.gradle
b/build.gradle
settings.gradle

a/build.gradle:

    repositories {
        mavenCentral()
    }
    configurations {
        aconf
    }
    dependencies {
        aconf 'abbot:abbot:1.4.0'
    }

b/build.gradle

    configurations {
        bconf
    }
    configurations.bconf.extendsFrom project(':a').configurations.aconf 

settings.gradle

    include ':a'
    include ':b'

Executing ./gradlew :a:dep :b:dep -i shows that bconf does not inherit dependencies from aconf.

    Included projects: [root project 'projectDeps', project ':a', project ':b']
    
    > Configure project :
    Evaluating root project 'projectDeps' using build file '/projectDeps/build.gradle'.
    
    > Configure project :a
    Evaluating project ':a' using build file '/projectDeps/a/build.gradle'.
    
    > Configure project :b
    Evaluating project ':b' using build file '/projectDeps/b/build.gradle'.
    All projects evaluated.
    Selected primary task ':a:dependencies' from project :a
    Selected primary task ':b:dependencies' from project :b
    Tasks to be executed: [task ':a:dependencies', task ':b:dependencies']
    :a:dependencies (Thread[Task worker for ':',5,main]) started.
    
    > Task :a:dependencies
    Task ':a:dependencies' is not up-to-date because:
      Task.upToDateWhen is false.
    
    ------------------------------------------------------------
    Project :a
    ------------------------------------------------------------
    
    aconf
    \--- abbot:abbot:1.4.0
         \--- junit:junit:4.8.2
    
    > Task :b:dependencies
    Task ':b:dependencies' is not up-to-date because:
      Task.upToDateWhen is false.
    
    ------------------------------------------------------------
    Project :b
    ------------------------------------------------------------
    
    bconf
    No dependencies
aSemy
  • 5,485
  • 2
  • 25
  • 51
ki_
  • 619
  • 1
  • 10
  • 21

2 Answers2

0

In a multi-project build, Gradle will evaluate/configure sub-projects in a given order which is by default project's name alphabetic order (I don't know if this order can be changed), so:

  • sub-project projectA will be evaluated/configured before subproject projectB
  • in projectA configuration you reference a configuration from projectB which is not yet knwon by Gradle, that' why you get an error (which should be something like: Could not get unknown property 'conf2' ... )

One solution would be to let Gradle configure projectB before creating the dependency between your configurations: you can use for example afterEvaluate {} API as follows:

In projectA's build.gradle:

project(":projectB").afterEvaluate { proj ->
    configurations.conf1.extendsFrom proj.configurations.confg2

}
M.Ricciuti
  • 11,070
  • 2
  • 34
  • 54
  • Thanks for the evaluation order tip but I don't think that's the issue since I don't get it to work even though the evaluation order is correct. I updated my question with a more clear example where evaluation order should not be an factor. – ki_ Feb 25 '19 at 12:24
  • I now reproduce your issue described in your edited question: it seems that when creating dependency from a configuration to another configuration from other sub-project , the dependencies are not properly inherited . I don't know if this is really supported by Gradle, I did not find similar case in the official docs/forums. Maybe you should try another approach, using `project dependency type` : `dependencies { bconf project(path: ':projectA', configuration: 'aconf')}` – M.Ricciuti Feb 25 '19 at 14:33
0

The best way is to have a root => sub-projects architecture.

So in the root project, you can define "shared" configurations.

According to https://remonsinnema.com/2016/05/09/how-to-manage-dependencies-in-a-gradle-multi-project-build/ :

Root project build.gradle

subprojects {
  configurations {
    commonsIo
  }

  dependencies {
    commonsIo 'commons-io:commons-io:2.5'
  }
}

Sub-project build.gradle

configurations {
  compile.extendsFrom commonsIo
}
Thomas Decaux
  • 21,738
  • 2
  • 113
  • 124