Access submodule dependencies from root in multi-module Gradle build
Asked Answered
U

2

1

Is there a way in the root build.gradle to access the dependencies declared in the subproject build.gradle files? I've tried the following --

subprojects.each { p ->
  println "subproject: ${p.name}"
  p.configurations.each { c ->
    println "  ${c}"
    c.dependencies.each { d ->
      println "    ${d}"
    }
  }
}

but I guess because the root build.gradle is getting executed first, the submodule dependency declarations haven't run yet, because all I get is:

subproject: foo
subproject: bar
subproject: baz

(I'm trying to write a multi-module build.gradle for building a bunch of related modules that don't know they're part of a multi-module project, or can be part of many different multi-module proejcts. My plan was to go through the submodule dependencies programmatically and add a corresponding project dependency to each one -- hoping to do something a little like Pieter Niederweiser's elastic-deps example but without having to modify the subproject buildfiles. In a single-module project seems like Gradle is smart enough to use project dependencies to resolve the Maven/Ivy dependencies, if they overlap.)


ETA: If there's a way to declare at the top level a block of code that all subprojects will run at some point after they've declared their dependencies, that might do it.

Unreconstructed answered 5/5, 2014 at 20:42 Comment(0)
P
2

If there's a way to declare at the top level a block of code that all subprojects will run at some point after they've declared their dependencies, that might do it.

evaluationDependsOnChildren() does the trick. All code that follows this method call will be evaluated after child projects have been evaluated.

Peddling answered 5/5, 2014 at 23:2 Comment(1)
Thanks, that did the trick. I added the hack I came up with it as this answer and if you have time, I'd be interested in your feedback.Unreconstructed
G
0

Since Gradle 3.2 you can simply call ResolutionStrategy#preferProjectModules.
https://docs.gradle.org/current/dsl/org.gradle.api.artifacts.ResolutionStrategy.html#org.gradle.api.artifacts.ResolutionStrategy:preferProjectModules()

Galata answered 5/9, 2020 at 15:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.