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.
my-build.gradle
file:
plugins {
id "net.saliman.cobertura" version "2.2.5"
}
build.gradle
file:
apply from: "my-build.gradle"
// Other stuff
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.
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.
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.
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.
I also faced similar issue recently and it got solved by changing Gradle settings in Intellij as follows:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With