使用FlatDir来源的gradle插件 [英] using gradle plugin from flatDir source

查看:1743
本文介绍了使用FlatDir来源的gradle插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何修改Gradle插件{}管理存储库 在本文中没有重复,因为它不涵盖flatDir的使用.


问题

如何使用新的plugin {}语义而不是已弃用的apply()语义,使用在本地JAR中定义的Gradle插件?

当前状态

没有任何解决方案,在发布问题并进行了相当长的搜索后,我提出了问题,想知道在Gradle修改后的插件语义中,是否出于设计或监督目的,不支持应使用的这种通用性和直接性.

很遗憾,我的报告已经关闭,没有提供有用的信息.

我要求在新问题中进行澄清,但我仍在等待. >

我很沮丧,因为期望社区至少对讨论这个问题感兴趣.

如果您可以提供信息,请这样做.

首次更新

在澄清了用于配置插件源的新样式之后,我更新了settings.gradle文件以使用以下代码块打开.但是,我感到遗憾的是,仅凭此更改我看不到任何改善. (对于build.gradle文件中引用的插件ID字段,我已经尝试了JAR元数据中发布的全局ID和JAR fie的基本名称.两者均同样失败.)

pluginManagement {
    repositories {
        gradlePluginPortal()
        jcenter()
        flatDir {
            dirs 'lib`'
        }
    }
}

该文档说明了如何使用自定义存储库,但似乎忽略了一个简单的平面目录的情况.

第二次更新

如果将版本号添加到JAR文件和plugins {}块中的相应语句,我会有所改进.在这种情况下,消息变为:

Plugin [id: 'plugin-id', version: '1.0.0'] was not found in any of the following sources:

- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (could not resolve plugin artifact 'plugin-id:plugin-id.gradle.plugin:1.0.0')
  Searched in the following repositories:
    Gradle Central Plugin Repository
    BintrayJCenter
    flatDir(/absolute/path/to/lib)

在这种情况下,该目录将添加到搜索到的源列表中.

奇怪的是,后缀.gradle.plugin被附加到我在打印的工件中的ID上.奇怪的是,将版本号添加到要搜索的内容中会影响要搜索的地点列表.

所以我的项目仍然无法构建.感谢您的进一步帮助.

原始背景

我在项目的lib目录中放置了一个包含自定义插件定义的JAR文件.使用下面的build.gradle构建文件,该构建成功运行.

buildscript {
  repositories {
      flatDir {
          dirs 'lib'
      }
  }
}

apply plugin: 'plugin-id'

但是,不赞成使用apply()语义,而赞成使用plugins {}块,因此我尝试按以下方式更新构建文件.

plugins {
    id 'plugin-id'
}

repositories {
    flatDir {
        dirs 'lib'
    }
}

我知道plugins {}的内容可以从repositories {}定义中得出.

但是,更改会导致失败:

* What went wrong:
Plugin [id: 'plugin-id'] 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)

保留plugin {}块,但将repositories {}块移回领先的buildscript {}块不能解决该错误.也就是说,即使我恢复到早期版本,仅用plugin {}块替换apply()语句,该错误仍然存​​在.

最后,将dependencies { classpath: ':jarname' }块添加到repositories {}块中是没有任何作用的,尽管我不知道为什么会这样,但一些消息人士认为这是必要的.

解决方案

这有效.经过gradle 6.3测试.

build.gradle :

plugins {
    id 'plugin-id'
}

settings.gradle :

pluginManagement {
    buildscript {
        repositories {
            flatDir {
                dirs '/plugin-folder/build/libs'
            }
        }
        dependencies {
            classpath ':plugin-jar:0.0.1'
        }
    }
}

更新:我今天才发现,可以在不使用上述dependencies块的情况下解决您的插件jar.在这种情况下,您应该将插件jar命名为[plugin-id].gradle.plugin[-version].jar.请注意,[-version]部分是可选的,plugin-id.gradle.plugin.jar也将起作用.

注意:不建议使用Flat dir存储库,而应使用本地maven repo文件夹.特别是在您要本地覆盖远程存储库中存在的工件的情况下.请参见 https://docs.gradle.org/current/userguide/declaring_repositories .html#sub:flat_dir_resolver .考虑到gradle本身不支持在maven repo文件夹中安装本地工件,似乎不可能完全脱离maven而转而使用gradle.

How to amend Gradle plugins {} management repository for custom plugins? is not duplicated in this post, because it does not cover use of flatDir.


Question

How do I use a Gradle plugin defined in a local JAR, using the new plugin {} semantics, instead of the deprecated apply() semantics?

Current Status

Not having any resolution, after posting the question and searching at considerable length, I filed an issue, wondering whether this use, which ought to be common and straightforward, is unsupported, either by design or oversight, within Gradle's revised plugin semantics.

Unfortunately, my report was closed, with no useful information provided.

I requested clarification in a new issue, but am still waiting.

I am frustrated, having expected that the community would be interested in at least discussing this problem.

If you can contribute information, please do so.

First Update

Following the clarification about the new style for configuring plugin sources, I updated my settings.gradle file to open with the following block. However, I regret that I see no improvement by this change alone. (For the plugin id field referenced in the build.gradle file, I have tried both the global ID published in the JAR metadata, and the basename of the JAR fie. Both fail equally.)

pluginManagement {
    repositories {
        gradlePluginPortal()
        jcenter()
        flatDir {
            dirs 'lib`'
        }
    }
}

The documentation explains how to use custom repositories, but appears to overlook the case of a trivial flat directory.

Second Update

I get some improvement if I add a version number to the JAR file and to the corresponding statement in the plugins {} block. In this case, the message becomes:

Plugin [id: 'plugin-id', version: '1.0.0'] was not found in any of the following sources:

- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (could not resolve plugin artifact 'plugin-id:plugin-id.gradle.plugin:1.0.0')
  Searched in the following repositories:
    Gradle Central Plugin Repository
    BintrayJCenter
    flatDir(/absolute/path/to/lib)

In this case, the directory is added to the list of sources searched.

It is strange that the .gradle.plugin suffix is being appended to my ID in the printed artifact. It is also strange that adding the version number to what is being searched for affects the list of places being searched.

So my project still cannot build. I appreciate any further help.

Original Background

I placed a JAR file containing a custom plugin definition in the lib directory of a project. With the build.gradle build file as below, the build runs successfully.

buildscript {
  repositories {
      flatDir {
          dirs 'lib'
      }
  }
}

apply plugin: 'plugin-id'

However, the apply() semantics are deprecated, favoring a plugins {} block, so I tried updating the build file as below.

plugins {
    id 'plugin-id'
}

repositories {
    flatDir {
        dirs 'lib'
    }
}

I understand that the plugins {} contents can draw from the repositories {} definitions.

However, the change creates a failure:

* What went wrong:
Plugin [id: 'plugin-id'] 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)

Keeping the plugin {} block but moving the repositories {} block back into a leading buildscript {} block does not resolve the error. That is, the error persists even if I revert to the earlier version only replacing the apply() statement with the plugin {} block.

Finally, it has no effect to add to the repositories {} block a dependencies { classpath: ':jarname' } block, which some sources suggest is necessary, though I don't know why it would be.

解决方案

This works. Tested with gradle 6.3.

build.gradle:

plugins {
    id 'plugin-id'
}

settings.gradle:

pluginManagement {
    buildscript {
        repositories {
            flatDir {
                dirs '/plugin-folder/build/libs'
            }
        }
        dependencies {
            classpath ':plugin-jar:0.0.1'
        }
    }
}

Update: I just found out today that it is possible to have your plugin jar resolved without using the dependencies block above. In that case you should name your plugin jar as [plugin-id].gradle.plugin[-version].jar. Note that the [-version] part is optional and plugin-id.gradle.plugin.jar will also work.

NB: Flat dir repositories are discouraged and local maven repo folder should be used instead. Especially in the case when you want to override locally an artifact which exists on a remote repo. See https://docs.gradle.org/current/userguide/declaring_repositories.html#sub:flat_dir_resolver. It seems impossible to fully move away from maven in favour of gradle, considering that installing local artifacts in a maven repo folder is not supported by gradle itself.

这篇关于使用FlatDir来源的gradle插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆