如何使用新的Gradle插件机制将插件应用于所有项目? [英] How to apply plugin to allprojects with new Gradle plugins mechanism?

查看:139
本文介绍了如何使用新的Gradle插件机制将插件应用于所有项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Gradle 2.1之前,我可以使用allProjects闭合将插件应用于所有项目(当然,先解决了jar):

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

使用新的发布机制,看来plugins闭包不能在allprojects内部使用:

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

失败:

在根项目上找不到参数[build_xxxx_run_closure1_closure4 @ yyyyy]的方法plugins()"

使用plugins闭包的规则是什么?插件仅适用于当前项目吗?如果是这样,我如何将其应用于所有项目,而无需在每个内部版本中重复plugins闭包?

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?

推荐答案

新的plugins {...}语法不能在allprojects {...}subprojects {...}闭包内使用.此外,它只能在构建脚本中使用(没有脚本插件,初始化脚本等).如果您希望避免将插件分别应用于每个项目,则建议使用旧的符号.这是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.

更新:从Gradle 3.0开始,您可以进行一些稍微的修改.您仍然必须显式地使用apply(),但是您不再需要处理所有buildscript { }废话,就可以在类路径上使用插件.这也使您可以有条件地应用插件.有关详细信息,请查看Gradle 3.0 发行说明. >

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

这篇关于如何使用新的Gradle插件机制将插件应用于所有项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆