Gradle multiple projects - include other gradle files

I am struggling to include one gradle file in another. I basically want to define all versions and configuration in one gradle file and just add dependencies in another. Kind of like a parent child relationship.

Following is the structure:

parent
- java-build.gradle
- spring-build.gradle

libraries (multi module project)
- build.gradle
libA
- build.gradle
libB
- build.gradle

module
- build.gradle

E.g. parent/java-build.gradle will have the java plugin defined and java version set to 11. An there will be one more libraries/build.gradle in a completely different application and folder which “includes” the parent/java-build.gradle and adds the dependencies on top of it (like apache commons deps).

java-build.gradle
plugins {
id ‘java’
}

java {
sourceCompatibility = JavaVersion.toVersion("11")
targetCompatibility = JavaVersion.toVersion("11")
}

library/build.gradle

apply from: ‘…/parent/java-build.gradle’
dependencies {
implementation group: ‘org.apache.commons’, name: ‘commons-lang3’, version: ‘3.11’
}

When I do a gradle build on gradle build on libraries/build.gradle , I get "Could not find method plugins() for arguments . If I just put the dependencies block in the java-build.gradle , it works fine.

Need help on how to include / apply multiple gradle files across projects. Thank you!

You cannot use a plugins block in a script plugin. You either have to use the legacy way to apply plugins using the apply method, or better use a pre complied script plugin as convention plugin, there you can use the plugins block without problems.

@Vampire - Thank you for the response. Looks like a custom plugin is the only way forward. I will give it a try. Thank you!