【问题标题】:How to build a Gradle plugin project together with a subproject that uses it?如何与使用它的子项目一起构建 Gradle 插件项目?
【发布时间】:2021-01-02 18:16:38
【问题描述】:

我有一个带有多个模块的 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 插件项目与使用它的子项目一起构建?

【问题讨论】:

  • 如果我理解正确,你need this
  • 尤金是正确的。如果你的项目中有你的项目使用的 Gradle 插件,它应该在buildSrc 中实现。
  • 您可以使用 + 作为类路径版本:classpath "myplugin:gradle-plugin:0.1.+",来依赖 最新发布的 版本。但是,您将无法使用当前版本。
  • @Phil thast 是我正在做的,但是很不方便。

标签: java gradle groovy gradle-plugin


【解决方案1】:

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

build.dependsOn ":plugin-subproject:build"

附: settings.gradle中有定义模块的方法

rootProject.name = 'project-name'

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

并像这样建立依赖关系:

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

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

【讨论】:

  • 谢谢。 plugin-subprojecta Gradle plugin。它不需要在依赖项中,它必须在 buildscript 部分中,因为它涉及 building sample-subproject。尽管如此,我还是试了一下。不幸的是,我得到了同样的错误。
猜你喜欢
  • 2014-03-24
  • 1970-01-01
  • 2018-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-16
  • 2014-02-08
相关资源
最近更新 更多