Gradle依赖平台在父模块中

huangapple go评论75阅读模式
英文:

Gradle dependency platform in parent module

问题

我有一个包含许多子项目的Gradle项目,我希望有一个BOM文件可以应用于所有子项目。我尝试将以下内容放入某个子项目中,而且运行良好:

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

但是,当我将其放入父级gradle.build中,或者像这样进行包装:

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

就会出现错误:

> 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.

我无法弄清楚问题出在哪里。如何修复这个问题?或者是否有更好的方法可以将一个BOM应用于所有子项目并从一个地方进行管理?

英文:

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?

答案1

得分: 1

我不认为问题在于平台本身

您收到的消息通常会在您尚未应用Java插件(尚未)的情况下出现。

Gradle的配置范围,如implementation、api和compileOnly,是作为Java插件的初始化阶段的一部分进行初始化的。

因此,根据您的子项目结构,您可能有一个或多个子项目不使用Java插件,无法识别该范围。关于子项目之间的执行顺序,我不太确定,这也可能发挥作用。

一个简单的解决方案是在allprojects闭包中也应用插件,如下所示:

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

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')
    }
}

huangapple
  • 本文由 发表于 2020年10月14日 19:13:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/64352047.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定