Search code examples
javagradleplatform

Gradle dependency platform in parent module


I have a gradle project with a lot of subprojects and I want a BOM file to apply to all the subprojects. I tried to put this in some subproject and it works fine:

dependencies{
  implementation enforcedPlatform('group:bom-artifact:version')
}

But when I put it a parent gradle.build, or wrap it like:

allprojects {
    dependencies {
        implementation enforcedPlatform('group:bom-artifact:version')
    }
}

It ends with error:

> Could not find method implementation() for arguments [DefaultExternalModuleDependency{group='group', name='bom-artifact', version='version', configuration='default'}] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

Can't figure out what's wrong. How to fix this? Or is there a better way to apply one BOM to all subprojects and manage it from one place?


Solution

  • I don't think the issue is the platform itself here

    The message you're getting usually appears if you have not (yet) applied the Java plugin.

    Gradle's configuration scopes like implementation, api and compileOnly are initialized as part of the Java plugin's init phase.

    So depending on the structure of your subproject you might have one or more subprojects that don't use the Java plugin that does not recorgnize the scope. I'm not quite sure about the execution order between subprojects, this might play also play a role.

    A simple solution would be to apply the plugin also into the allprojects closure, like

    allprojects {
        apply plugin: 'java'
        dependencies {
            implementation enforcedPlatform('group:bom-artifact:version')
        }
    }