gpt4 book ai didi

Gradle Kotlin 所有项目依赖项导致构建失败

转载 作者:行者123 更新时间:2023-12-02 12:53:39 25 4
gpt4 key购买 nike

在我的 Gradle 项目中,我可以声明一个 dependenciesimplementation 阻止条目,没问题。但是,当我尝试声明这样的内容时,出现错误:

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 的新手,所以我可能只是在做一些愚蠢的事情?这是我的环境信息:
$ 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'
}
}
settings.gradle:
rootProject.name = 'demo'

// Removing this line causes the error to go away, but means the module is missing.
include 'submodule'
submodule/build.gradle
// Empty file. I've tried adding various plugins (java / kotlin) to no avail.

最佳答案

您的问题 build.gradle您是否正在尝试使用构建依赖项配置,在这种情况下 implementation不指定 java插入。 gradle 文档说;

The Java plugin adds a number of dependency configurations to your project, as shown below. Tasks such as compileJava and test then use one or more of those configurations to get the corresponding files and use them, for example by placing them on a compilation or runtime classpath.



解决此问题的一种方法是包含 java插件如下(我在 5.2.1 上测试过,效果很好);
allprojects {
apply plugin: 'java'
dependencies {
implementation 'org.mockito:mockito-core:2.25.0'
}
}

构建配置与 java 的关系插件已经全面描述
https://docs.gradle.org/current/userguide/java_plugin.html#sec:java_plugin_and_dependency_management



https://docs.gradle.org/current/userguide/managing_dependency_configurations.html#managing_dependency_configurations

另外,请确保不要在子项目中复制此依赖项(或在根目录或所有项目中声明的任何其他依赖项)。

关于Gradle Kotlin 所有项目依赖项导致构建失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55100904/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com