Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle plugins DSL: Restriction on declaration location

I have a few Gradle scripts that get applied via apply from: 'my-build.gradle'. If I use the new plugins DSL as follows in the external build file my-build.gradle, it fails with the following error:

> 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

Looking at the documentation pointed in the error message didn't reveal as to why the restriction is in place. Why is there a restriction on the location of the plugins declaration?

Files for reference below.

  1. my-build.gradle file:

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

    apply from: "my-build.gradle"
    
    // Other stuff
    
like image 585
Invisible Arrow Avatar asked Feb 23 '15 12:02

Invisible Arrow


People also ask

What is Gradle plugin DSL?

The plugins DSL provides a succinct and convenient way to declare plugin dependencies. It works with the Gradle plugin portal to provide easy access to both core and community plugins. The plugins DSL block configures an instance of PluginDependenciesSpec. To apply a core plugin, the short name can be used: Example 1.

Which DSL does Gradle use?

The Kotlin DSL provides property extensions for all Gradle core plugins, as shown above with the java , jacoco or maven-publish declaration. Third party plugins can be applied the same way as with the Groovy DSL.

What is the use of Buildscript in Gradle?

buildscript: This block is used to configure the repositories and dependencies for Gradle. dependencies: This block in buildscript is used to configure dependencies that the Gradle needs to build during the project.

What is subprojects in Gradle?

The subproject producer defines a task named buildInfo that generates a properties file containing build information e.g. the project version. You can then map the task provider to its output file and Gradle will automatically establish a task dependency.


2 Answers

I also faced similar issue recently and it got solved by changing Gradle settings in Intellij as follows:

enter image description here

like image 54
Anshul Singhal Avatar answered Sep 21 '22 15:09

Anshul Singhal


This is how you can use plugins in external Gradle files such as your 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

Above I've activated the plugins for SonarQube and Cobertura.

Generally, to get the fully qualified class name of your plugin you will have to look inside its .jar file.

As for the technical reasons why you can't use a plugins {} block in an external file, I do not know. It might have to do something with the 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.

But rejoice:

Future versions of Gradle will remove this restriction.

like image 38
Matthias Braun Avatar answered Sep 21 '22 15:09

Matthias Braun