Before Gradle 2.1 I could apply plugin to all projects by using allProjects
closure (by prevoisly resolving the jar, of course):
buildscript { repositories { jcenter() } dependencies { classpath "org.jfrog.buildinfo:build-info-extractor-gradle:3.0.1" } } allprojects { apply plugin: "com.jfrog.artifactory" }
With new publishing mechanism it looks like the plugins
closure can't be used inside allprojects
:
allprojects { plugins { id "com.jfrog.artifactory" version "3.0.1" } }
fails with:
"Could not find method plugins() for arguments [build_xxxx_run_closure1_closure4@yyyyy] on root project"
What are the rules of using plugins
closure? Is the plugin applied to current project only? If so, how can I apply it to all projects without repeating the plugins
closure inside each build?
While creating a custom plugin, you need to write an implementation of plugin. Gradle instantiates the plugin and calls the plugin instance using Plugin. apply() method. The following example contains a greeting plugin, which adds a hello task to the project.
Applying a plugin to a project allows the plugin to extend the project's capabilities. It can do things such as: Extend the Gradle model (e.g. add new DSL elements that can be configured) Configure the project according to conventions (e.g. add new tasks or configure sensible defaults)
There are two general types of plugins in Gradle, script plugins and binary plugins.
The new plugins {...}
syntax cannot be used within a allprojects {...}
or subprojects {...}
closure. Additionally, it can only be used within build scripts (no script plugins, init scripts, etc). If you want to avoid having to apply the plugin to each project individually I'd suggest using the old notation. This is an issue the Gradle team is aware of and a solution will be introduced in future versions.
Update: Starting with Gradle 3.0 you can do this in a slightly modified way. You still have to explicitly use apply()
but you no longer have to deal with all the buildscript { }
nonsense to get the plugin on your classpath. This also allows you to conditionally apply plugins. Check out the Gradle 3.0 release notes for more information.
plugins { id 'my.special.plugin' version '1.0' apply false } allprojects { apply plugin: 'java' apply plugin: 'my.special.plugin' }
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