Gradle多项目构建:找不到插件

17

我有一个非常基础的多项目/多模块Gradle构建用例。我最终需要的结构复杂度不会比官方Gradle文档中声明子项目之间依赖关系的部分提出的更高。

将他们的文档复制到这里,我们有以下项目结构:

.
├── buildSrc
│   ...
├── api
│   ├── src
│   │   └──...
│   └── build.gradle
├── services
│   └── person-service
│       ├── src
│       │   └──...
│       └── build.gradle
├── shared
│   ├── src
│   │   └──...
│   └── build.gradle
└── settings.gradle

还有这些构建文件:

// settings.gradle
rootProject.name = 'dependencies-java'
include 'api', 'shared', 'services:person-service'

// buildSrc/src/main/groovy/myproject.java-conventions.gradle
plugins {
    id 'java'
}

group = 'com.example'
version = '1.0'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation "junit:junit:4.13"
}

// api/build.gradle
plugins {
    id 'myproject.java-conventions'
}

dependencies {
    implementation project(':shared')
}
shared/build.gradle
plugins {
    id 'myproject.java-conventions'
}

// services/person-service/build.gradle
plugins {
    id 'myproject.java-conventions'
}

dependencies {
    implementation project(':shared')
    implementation project(':api')
}

然而,当我尝试执行项目根目录下的 gradle build 命令时,实际测试时出现以下错误:

Plugin [id: 'myproject.java-conventions'] was not found in any of the following sources:

- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (plugin dependency must include a version number for this source)

为了好玩,我尝试添加版本号,但可预见的是 Gradle 一如既往地抱怨,称无法解析该构件。我做错了什么?

我的 Gradle 版本信息:

------------------------------------------------------------
Gradle 6.5.1
------------------------------------------------------------

Build time:   2020-06-30 06:32:47 UTC
Revision:     66bc713f7169626a7f0134bf452abde51550ea0a

Kotlin:       1.3.72
Groovy:       2.5.11
Ant:          Apache Ant(TM) version 1.10.7 compiled on September 1 2019
JVM:          11.0.8 (Oracle Corporation 11.0.8+10)
OS:           Windows 10 10.0 amd64
3个回答

阿里云服务器只需要99元/年,新老用户同享,点击查看详情
23

看起来你在文档中发现了一个问题。

名为buildSrc/src/main/groovy/myproject.java-conventions.gradle的文件声明了称为预编译脚本插件的东西。为了使其工作,您必须在buildSrc项目中使用名为groovy-gradle-plugin(适用于Groovy插件)的插件:

# buildSrc/build.gradle
plugins {
    id 'groovy-gradle-plugin'
}

完成后,它应该能够找到您的自定义myproject.java-conventions插件。如果是这样,您可以在Gradle问题跟踪器上创建一个问题,建议他们将上述代码片段添加到有关项目依赖关系文档中的示例中。


4
很好!以下是来自Gradle项目的更准确的示例:在子项目之间共享构建逻辑示例 - thokuest
这对我有用,谢谢Bjørn。我还注意到的另一件事是,约定插件的顶级目录必须命名为buildSrc,这在文档中并不明确。但我会记录一个问题,让Gradle将build.gradle添加到该目录中。 - Austin Brown
1
@AustinBrown 如果您能在此处链接您将要创建的问题以供将来参考,那将很酷。 - thokuest
你说得对!@thekuest 这是链接:https://discuss.gradle.org/t/gradle-multi-project-documentation-clarification/38108 (他们的 GitHub 存储库似乎没有任何与文档相关的问题标签,因此这个论坛是我能找到提出问题的最佳地点。) - Austin Brown
2
对于 Kotlin(build.gradle.kts):plugins { \kotlin-dsl` }` - st.ebberr

2
我和你一样从头开始,遇到了同样的问题。尽管Bjorn的答案可能适用于Java,但对于我来说(对于Kotlin实现),它并没有解决问题,因为它还需要在repositories块中使用gradlePluginPortal()。 因此,补充一下已接受的答案,对我而言最好的方法是简单地运行./gradlew init任务,类型为application。请参阅Gradle文档Building Java Applications with libraries Sample。在这里,无论实现语言如何,您都应该始终获得一个运行的多项目。 在我的Kotlin应用程序(使用Groovy DSL for Gradle)中,这将生成以下buildSrc/build.gradle
/*
 * This file was generated by the Gradle 'init' task.
 */

plugins {
    // Support convention plugins written in Groovy. Convention plugins are build scripts in 'src/main' that automatically become available as plugins in the main build.
    id 'groovy-gradle-plugin'
}

repositories {
    // Use the plugin portal to apply community plugins in convention plugins.
    gradlePluginPortal()
}

dependencies {
    implementation 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.72'
}

0
你可以进入设置--> Gardle,并按照所示更改设置,以允许你的项目在IntelliJ IDEA中构建、运行甚至进行测试。 屏幕截图

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,