Reputation: 16215
(Please keep in mind I've simplified the problem for purposes of discussion here)
I've got a set of applications and dependent libraries, something like this (each with a src/
directory and build.gradle
):
appa/
appb/
libx/
liby/
libz/
In build.gradle
, the dependencies are currently declared like this:
appa/build.gradle:
compile "com.asdf:libx:1.0"
compile "com.asdf:liby:1.0"
appb/build.gradle:
compile "com.asdf:liby:1.0"
liby/build.gradle:
compile "com.asdf:libz:1.0"
appa
, and I need to make changes to libx
. I need to do multiple steps:
libx
from source control and make changes locallyappa
(pulling the recently updated libx
from repo)libx
, I've got to repeat that over again.I've written a small gradle plugin (referenced in each project's build.gradle
) that identifies com.asdf
dependencies, and uses dependency substitution to replace the artifact dependency with a project dependency if that project exists locally.
configurations.all {
resolutionStrategy.dependencySubstitution {
all { DependencySubstitution dependency ->
if (dependency.requested instanceof ModuleComponentSelector && dependency.requested.group == 'com.asdf') {
def targetProject = findProject(":${dependency.requested.module}")
if (targetProject != null) {
dependency.useTarget targetProject
}
}
}
}
}
Yay! With a few modifications to settings.gradle
(see below), I've accomplished my goal... Except...
I need to modify settings.gradle
to include lines like this for every dependency (otherwise findProject
doesn't resolve the dependent project during build):
include ':libx'
project(':libx').projectDir = new File(settingsDir, '../libx')
While it's possible to go through all the settings.gradle
files and do this (I've done it for a handful as my proof-of-concept), it's ugly, repetitive, and is logically the same information that is being passed to compile
arguments for the build.gradle
dependencies.
settings.gradle
, or introduces a typo between them.settings.gradle
just define projects for all directories it finds at that level, but then building any project turns into a mega-build of all projects.What's a better way to do this, without duplicating information between settings.gradle
and build.gradle
? I want to make it so adding new dependencies is still just as easy as adding the compile
reference in build.gradle
, without touching settings.gradle
...
I'm still rather new to groovy/gradle, so maybe I'm missing something that's obvious to the more experienced gradle master?
Upvotes: 1
Views: 976
Reputation: 24468
I believe your use-case is the motivation for Composite Builds.
I have a demo here, which writes to a jars
folder as a mock publishing of artifacts. Be sure to check-out the README.md as the demo is a mini-laboratory for trying out the use-case before and after composite builds.
In the demo mainBuild
is appa
; utils
is libx
. The key syntax in mainBuild/settings.gradle
(here) is:
includeBuild '../utils'
This tells Gradle to use the local codebase instead of the published artifact. Of course, one would not commit this line to source-control.
Upvotes: 1