【问题标题】:Gradle plugins DSL: Restriction on declaration locationGradle 插件 DSL:声明位置的限制
【发布时间】:2015-02-23 12:57:38
【问题描述】:

我有一些通过 apply from: 'my-build.gradle' 应用的 Gradle 脚本。如果我在外部构建文件my-build.gradle 中按如下方式使用新插件 DSL,则会失败并出现以下错误:

> startup failed:
  Only Project build scripts can contain plugins {} blocks
  See http://gradle.org/docs/2.3/userguide/plugins.html#sec:plugins_block 
  for information on the plugins {} block

查看错误消息中指出的文档并没有揭示为什么存在限制。为什么插件声明的位置有限制?

以下文件供参考。

  1. my-build.gradle文件:

    plugins {
        id "net.saliman.cobertura" version "2.2.5"
    }
    
  2. build.gradle文件:

    apply from: "my-build.gradle"
    
    // Other stuff
    

【问题讨论】:

    标签: gradle build.gradle


    【解决方案1】:

    这是您可以在外部 Gradle 文件(例如 my-build.gradle)中使用插件的方法:

    buildscript {
      repositories {
        mavenCentral()
        maven { url "https://plugins.gradle.org/m2/" }
      }
      dependencies {
        classpath "org.sonarqube.gradle:gradle-sonarqube-plugin:1.1"
        classpath "net.saliman:gradle-cobertura-plugin:2.2.8"
      }
    }
    // Because this is a helper script that's sourced in from a build.gradle, we can't use the ID of external plugins
    // We either use the full class name of the plugin without quotes or an init script: http://www.gradle.org/docs/current/userguide/init_scripts.html
    apply plugin: org.sonarqube.gradle.SonarQubePlugin
    apply plugin: net.saliman.gradle.plugin.cobertura.CoberturaPlugin
    
    // rest of my-build.gradle omitted
    

    上面我已经激活了SonarQubeCobertura 的插件。

    通常,要获得插件的完全限定类名,您必须查看其 .jar 文件。

    至于技术原因为什么你不能在外部文件中使用plugins {} 块,我不知道。它可能与following 相关:

    [...] 插件 [需要] 以 Gradle 可以轻松指定的方式指定 并在执行其余构建之前快速提取 [它们] 脚本。它还要求要使用的插件的定义是 有点静态。

    但是rejoice:

    Gradle 的未来版本将取消此限制。

    【讨论】:

    • 感谢您的参考 :) 我知道解决方法,但有点困惑为什么 plugins 闭包在外部文件中不起作用。
    • 您将如何使用此解决方法来应用诸如 applicationjava-library 之类的内置插件之一?
    【解决方案2】:

    我最近也遇到了类似的问题,通过在 Intellij 中更改 Gradle 设置得到了解决,如下所示:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-15
      • 1970-01-01
      • 2019-07-10
      • 1970-01-01
      • 2016-08-16
      • 2020-09-15
      • 2016-07-10
      相关资源
      最近更新 更多