gpt4 book ai didi

plugins - 如何从 build.gradle 文件中提取插件?

转载 作者:行者123 更新时间:2023-12-02 01:56:36 25 4
gpt4 key购买 nike

为了简化我的构建,我想从“build.gradle”中提取自定义插件并将它们放入单独的 gradle 文件中。

这是我能想出的最简单的例子。


1。在 build.gradle 中定义一个插件 (WORKS)

以下“build.gradle”:

apply plugin: GreetingPlugin

class GreetingPlugin implements Plugin<Project> {

void apply(Project project) {

project.task('hello') << {
println "Hello, World"
}
}
}

当你运行时:

gradle hello

产生:

:hello
Hello, World

BUILD SUCCESSFUL


2。从 build.gradle 中提取插件(不起作用)

我想将插件定义移动到另一个文件“hello.gradle”中:

class GreetingPlugin implements Plugin<Project> {

void apply(Project project) {

project.task('hello') << {
println "Hello, World"
}
}
}

并将“build.gradle”更改为:

apply from: 'hello.gradle'
apply plugin: GreetingPlugin

现在当我运行时:

gradle hello

它产生:

FAILURE: Build failed with an exception.

* Where:
Build file 'build.gradle' line: 2

* What went wrong:
A problem occurred evaluating root project 'gradle-problem'.
> Could not find property 'GreetingPlugin' on root project 'gradle-problem'.

BUILD FAILED

Total time: 1.723 secs

我确信这一定是可能的,那么你是怎么做到的呢?

最佳答案

我找到了两个合理的解决方案:

1。在外部文件中应用插件

你好.gradle:

apply plugin: GreetingPluginclass GreetingPlugin implements Plugin<Project> {    void apply(Project project) {        project.task('hello').doLast {            println "Hello, World"        }    }}

build.gradle:

apply from: 'hello.gradle'


2。通过将插件类添加到 ext 属性

来导出插件类

你好.gradle:

class GreetingPlugin implements Plugin<Project> {    void apply(Project project) {        project.task('hello').doLast {            println "Hello, World"        }    }}ext.GreetingPlugin = GreetingPlugin

build.gradle:

apply from: 'hello.gradle'
apply plugin: GreetingPlugin

关于plugins - 如何从 build.gradle 文件中提取插件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19667148/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com