Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply plugin to allprojects with new Gradle plugins mechanism?

Tags:

gradle

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?

like image 213
JBaruch Avatar asked Oct 07 '14 12:10

JBaruch


People also ask

How do I add plugins to build Gradle?

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.

When Gradle plugins are applied to a 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)

Which are the two types of plugins in Gradle?

There are two general types of plugins in Gradle, script plugins and binary plugins.


1 Answers

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' } 
like image 114
Mark Vieira Avatar answered Nov 13 '22 10:11

Mark Vieira