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

Gradle

Gradle Problem Overview


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?

Gradle Solutions


Solution 1 - Gradle

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'
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJBaruchView Question on Stackoverflow
Solution 1 - GradleMark VieiraView Answer on Stackoverflow