Gradle - 访问其他插件版本

4
我想从自己的插件中获取其他插件版本。我无法通过PluginContainer或PluginManager找到方法来做到这一点。
例如,对于build.gradle.kts:
plugins {
    id("some.plugin") version "1.2.3"
    id("my.plugin") version "3.4.5"
}

我希望能够从“my.plugin”的代码中读取出“some.plugin”版本号为“1.2.3”。

2个回答

1
如果您想访问其他插件版本,可以像这样操作:
plugins {
    id("base")
    id("org.hidetake.ssh") version "2.10.1"
    id("com.jfrog.artifactory") version "4.29.2"
}

apply plugin: MyPlugin

class MyPlugin implements Plugin<Project> {

    @Override
    void apply(Project target) {
        // retrieve plugins dependencies from buildscript classpath
        DependencySet pluginDependencies = target.buildscript.configurations.getByName("classpath").allDependencies

        // use the found plugins dependencies
        pluginDependencies.forEach { Dependency classpathDep ->
            println(" Plugin dependency found:  $classpathDep.name, version=$classpathDep.version")
        }
    }
}

这将会弹出提示框:
> Configure project :
 Plugin dependency found:  org.hidetake.ssh.gradle.plugin, version=2.10.1
 Plugin dependency found:  com.jfrog.artifactory.gradle.plugin, version=4.29.2

> Task :prepareKotlinBuildScriptModel UP-TO-DATE

BUILD SUCCESSFUL in 434ms

你说得对,那就是我在 https://dev59.com/IrLma4cB1Zd3GeqPjPca#74153683 中写的内容,而且你说得对,这个答案很合适。答案并不是“如何设置验证”(这是我试图解决的问题),而只是“如何读取版本”。 - childno͡.de

0

无操作...直到2022年,这仍然是一个甚至谷歌在其上运送了大量代码行的问题。


即使是你的api()声明(带版本范围)


我也看不出依赖约束会对我们有所帮助

结合起来

就像

class YourPlugin : Plugin<Project> {
  override fun apply(project: Project) {
    project
      .buildscript.dependencies.constraints
      .add("classpath", "com.example:other-plugin") { 
        it.version {
          it.require("[1.0, 2.0)")
        } 
      }
  }
}

由于您无法在以此结尾的Gradle阶段中添加约束

   > Cannot change dependencies of dependency configuration ':app:classpath' after it has been resolved.

简而言之; 所以我看不到除此之外的任何东西

  1. 迭代所有项目构建依赖项,检查版本
  2. 等待依赖插件提供版本

https://docs.gradle.org/current/userguide/toolchains.html是针对JVM特定的,甚至没有涉及到这个方面


顺便说一下,当我尝试使用版本约束进行比较时,我并没有成功。

if (project.pluginManager.hasPlugin("com.example.other") == false) {
  throw DependencyVerificationException("This plugin needs com.example.other")
}
project
  .buildscript
  .configurations
  .getByName("classpath")
  .withDependencies {
    it.find {
      it.group == "com.example" &&
      it.name == "other-plugin"
    }
    ?.version
    ?.let { 
      if (DefaultImmutableVersionConstraint.of("1.0") /*no clue how to compare with*/ == it)
       else throw DependencyVerificationException("This plugin needs com.example.other at least with version 1.0")
    } ?: throw DependencyVerificationException("This plugin needs com.example.other")
  }

顺便提一下,因为它与编程有关:为了测试不同的版本,确实有一些东西在这里,比如https://github.com/ajoberstar/gradle-stutter

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