gpt4 book ai didi

Gradle插件DSL : Restriction on declaration location

转载 作者:行者123 更新时间:2023-12-04 17:47:35 28 4
gpt4 key购买 nike

我有一些通过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

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

引用文件如下。
  • my-build.gradle文件:
    plugins {
    id "net.saliman.cobertura" version "2.2.5"
    }
  • build.gradle文件:
    apply from: "my-build.gradle"

    // Other stuff
  • 最佳答案

    这是您可以在外部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做一些事情:

    [...] plugins [need to] be specified in a way that Gradle can easily and quickly extract [them], before executing the rest of the build script. It also requires that the definition of plugins to use be somewhat static.



    但是 rejoice:

    Future versions of Gradle will remove this restriction.

    关于Gradle插件DSL : Restriction on declaration location,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28674352/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com