更好的方式来组织Gradle的pluginManagement块。

huangapple 未分类评论44阅读模式
英文:

Better way to organize gradle pluginManagement block

问题

以下是一个示例,在此示例中,为了将 custom-plugin 添加到应用程序中,我们需要在解析策略中指定它的依赖关系,并使用 useModule 将其添加到类路径中。

-------build.gradle----------
plugins {
  id 'custom-plugin'
}
-------settings.gradle----------
 pluginManagement {
    resolutionStrategy {
      eachPlugin {
        if (requested.id.namespace == 'custom-plugin') {
            useModule('org.gradle.sample:custom-plugins:1.0.0')
        }
      }
    }
    repositories {
       maven { url 'maven-repo'  }
    }
}

有没有更简单/更好的方法来添加依赖关系(useModule 部分)?

我不想每次添加新的自定义插件时都添加 if 条件并检查 requested.id.namespace 命名空间。

有没有一种方法可以避免这种情况,就像在以前添加插件到应用程序中的旧方法中,在 buildscript 块中我们只需要使用 classpath 添加依赖项。

buildscript {
repositories {
    maven { url "https://privaterepo.myemployer.com" }
}

dependencies {
    classpath "org.gradle.sample:custom-plugins:1.0.0"
}
英文:

Below is a example where, in order to add a custom-plugin to the application, we need to specify it dependency in resolution strategy and by using useModule, to add it in classpath

-------build.gradle----------
plugins {
  id 'custom-plugin'
}
-------settings.gradle----------
 pluginManagement {
    resolutionStrategy {
      eachPlugin {
        if (requested.id.namespace == 'custom-plugin') {
            useModule('org.gradle.sample:custom-plugins:1.0.0')
        }
      }
    }
    repositories {
       maven { url 'maven-repo'  }
    }
}

Is there any simpler/better way to add the dependency (useModule section)?

I don't want to add if condition and check the namespace requested.id.namespace every time for any new custom plugin I add.

Is there a way to avoid this just like in the old way of adding plugin in the application, wherein buildscript block we just have to add that dependency using classpath.

buildscript {
repositories {
    maven { url "https://privaterepo.myemployer.com" }
}

dependencies {
    classpath "org.gradle.sample:custom-plugins:1.0.0"
}

答案1

得分: 0

这取决于情况,如果代码库知道所请求的 requested.id.id,则会添加默认的 .gradle.plugin 后缀。您的问题导致了另一个答案——我在那里提供的示例可能显示了何时需要使用 resolutionStrategy 以及何时不需要。

英文:

This depends, if the repository knows the requested requested.id.id, with the default .gradle.plugin suffix added. Your question led to another answer - and the example which I provide there, probably shows when it's required to use resolutionStrategy and when not.

huangapple
  • 本文由 发表于 2020年7月26日 00:04:20
  • 转载请务必保留本文链接:https://java.coder-hub.com/63090495.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定