This is a simplified version of my problem:
I have a multi-project Gradle 5.4.1 build with three sub projects. These sub projects have an extra project property named isLibrary
which may be true
or false
:
$ROOT_PROJ_DIR/library/build.gradle
:
ext {
isLibrary = true
}
description = "Library"
// ...
$ROOT_PROJ_DIR/module1/build.gradle
:
ext {
isLibrary = false
}
description = "App Module 1"
// ...
$ROOT_PROJ_DIR/module2/build.gradle
:
ext {
isLibrary = false
}
description = "App Module 2"
// ...
In my root build.gradle
file, amongst other things, I want to:
- Add a dependency on
org.postgresql:postgresql:42.2.5
to all sub-projects - For all
!isLibrary
projects:- Apply the
foo
plugin - Add a dependency on
:library
to all non-library sub-projects
- Apply the
Here’s how I tried to do this:
plugins {
id "foo" version "1.0.0" apply false
}
subprojects {
apply plugin: 'java'
group = '...'
version = '...'
sourceCompatibility = '1.8'
// ...
dependencies {
implementation 'org.postgresql:postgresql:42.2.5'
// ...
}
afterEvaluate { Project project ->
if (!project.isLibrary) {
apply plugin: 'foo'
dependencies {
compile project(':library')
}
}
}
}
but this fails with an error:
* What went wrong:
A problem occurred configuring project ':module1'.
> Could not find method call() for arguments [:library] on project ':module1' of type org.gradle.api.Project.
To workaround this, for the time being I am adding the conditional dependency by filtering based on the project name:
subprojects {
apply plugin: 'java'
group = '...'
version = '...'
sourceCompatibility = '1.8'
// ...
dependencies {
implementation 'org.postgresql:postgresql:42.2.5'
// ...
}
afterEvaluate { Project project ->
if (!project.isLibrary) {
apply plugin: 'foo'
}
}
}
configure(subprojects.findAll { it.name != 'library' }) {
dependencies {
compile project(':library')
}
}
But I prefer to:
- Use conditional logic based on extra properties instead of project names
- Keep the conditional dependencies logic together with the conditional
apply plugin: 'foo'
logic (e.g. in theafterEvaluate
block)
Any ideas what am I doing wrong and how can this error be fixed?
Thanks in advance.