Gradle Kotlin所有项目依赖项都会导致生成失败
在我的Gradle项目中,我可以用Gradle Kotlin所有项目依赖项都会导致生成失败,gradle,kotlin,Gradle,Kotlin,在我的Gradle项目中,我可以用实现条目声明一个依赖项块,没有问题。但是,当我尝试声明类似的内容时,我会得到一个错误: allprojects { dependencies { implementation("...") } } 我得到的错误是: Could not find method implementation() for arguments [org.mockito:mockito-core:2.25.0] on object o
实现
条目声明一个依赖项
块,没有问题。但是,当我尝试声明类似的内容时,我会得到一个错误:
allprojects {
dependencies {
implementation("...")
}
}
我得到的错误是:
Could not find method implementation() for arguments [org.mockito:mockito-core:2.25.0]
on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
我无法在Java项目中重现这一点,因此它可能与Kotlin项目有关。我是格拉德尔的新手,所以我可能只是在做些傻事?以下是我的环境信息:
$ gradle --version
------------------------------------------------------------
Gradle 5.2.1
------------------------------------------------------------
Build time: 2019-02-08 19:00:10 UTC
Revision: f02764e074c32ee8851a4e1877dd1fea8ffb7183
Kotlin DSL: 1.1.3
Kotlin: 1.3.20
Groovy: 2.5.4
Ant: Apache Ant(TM) version 1.9.13 compiled on July 10 2018
JVM: 1.8.0_112 (Oracle Corporation 25.112-b15)
OS: Windows 10 10.0 amd64
最小繁殖 我可以用最少的项目重现这个问题 build.gradle:
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.3.21'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
// This works.
dependencies {
implementation 'org.mockito:mockito-core:2.25.0'
}
// Causes an error. Using a random dependency to reproduce the issue.
allprojects {
dependencies {
implementation 'org.mockito:mockito-core:2.25.0'
}
}
设置。渐变:
rootProject.name = 'demo'
// Removing this line causes the error to go away, but means the module is missing.
include 'submodule'
// Empty file. I've tried adding various plugins (java / kotlin) to no avail.
子模块/build.gradle
rootProject.name = 'demo'
// Removing this line causes the error to go away, but means the module is missing.
include 'submodule'
// Empty file. I've tried adding various plugins (java / kotlin) to no avail.
build.gradle
中的问题是您试图使用构建依赖项配置,在本例中是implementation
,而不指定java
插件。格拉德尔博士说
Java插件为您的应用程序添加了许多依赖项配置
项目,如下所示。任务,如compileJava和test,然后使用
一个或多个配置,以获取相应的文件和
使用它们,例如将它们放在编译或运行时上
类路径
解决这个问题的一种方法是包含java
插件,如下所示(我已经在5.2.1上进行了测试,效果很好)
构建配置与java
插件的关系已在上进行了全面描述
及
此外,请确保不要在子项目中复制此依赖项(或在根目录中声明的任何其他依赖项或所有项目的依赖项)。太棒了,谢谢!我在我的项目中使用了Kotlin DSL(但希望尽可能减少复制)。这对我使用Kotlin很有效:allprojects{apply())dependencies{…}同样值得注意的是,我已经在我的子模块中启用了java插件,但是我猜
allprojects{}
块是在子模块的plugins{}
block.Great之前执行的。很高兴这有帮助。是的,首先选择包含所有项目的根模块。在谷歌上寻找gradle多模块项目,有一个很好的文档。