首页 > 解决方案 > 如何与使用它的子项目一起构建 Gradle 插件项目?

问题描述

我有一个带有几个模块的 Gradle 项目,其中我有一个实现 Gradle 插件plugin-subproject的模块()和一个带有该插件展示的模块(sample-subproject):

plugin-subproject
│   build.gradle
sample-subproject
│   build.gradle
other subprojects...
│   build.gradle   
build.gradle

在 中sample-subproject,我依赖于插件:

buildscript {
    repositories {
        mavenCentral()
        mavenLocal()
    }
    dependencies {
        classpath "myplugin:gradle-plugin:0.1.0"
    }
}

apply plugin: "myplugin.plugin"

它必须从本地存储库中获取插件。

问题:为了构建sample-subproject,插件必须安装到本地 Maven 存储库中。但我无法安装它,因为如果我运行./gradlew :plugin-subproject:install它会失败。

* What went wrong:
A problem occurred configuring project ':sample-subproject'.
> Could not resolve all artifacts for configuration ':sample-subproject:classpath'.
> Could not find myplugin:gradle-plugin:0.1.0.
    Searched in the following locations:
    - https://repo.maven.apache.org/maven2/myplugin/gradle-plugin/0.1.0/gradle-plugin-0.1.0.pom
    - file:/Users/myuser/.m2/repository/myplugin/gradle-plugin/0.1.0/gradle-plugin-0.1.0.pom
    - https://plugins.gradle.org/m2/myplugin/gradle-plugin/0.1.0/gradle-plugin-0.1.0.pom
    Required by:
        project :sample-subproject

我可以删除sample-subproject,将插件安装到本地 maven 存储库,然后我可以成功添加和构建示例项目。但这是一个 hack,我想以标准方式解决问题。

问题:如何将 Gradle 插件项目与使用它的子项目一起构建?

标签: javagradlegroovygradle-plugin

解决方案


您可以在 main 中添加行build.gradle

build.dependsOn ":plugin-subproject:build"

PS有办法在settings.gradle

rootProject.name = 'project-name'

include "plugin-subproject"
include "sample-subproject"
include "other subprojects"

并使依赖项像这样:

dependencies {
    implementation project(':plugin-subproject')
}

但没有书面证据表明这是按正确顺序组装模块的正确方法。


推荐阅读