使用其他Gradle文件中的Gradle功能 [英] Use Gradle function from other gradle file

查看:84
本文介绍了使用其他Gradle文件中的Gradle功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将我的300行build.gradle逻辑上拆分为多个构建文件,以使其易于维护和扩展.

I want to split my 300 lines build.gradle logically into multiple build files to make it easier to maintain and extend.

正如我所注意到的,可以将gradle任务拆分为多个文件,并与以下命令一起使用:

As I've noticed, it is possible to split gradle tasks into multiple files and use them with:

apply from: "myGradleFile"

使用这种方法,令人遗憾的是,我无法访问在 second 构建脚本中定义的功能.

With this approach I sadly have no access to functions, defined in the second build script.

是否还可以将Gradle函数拆分为多个文件?

示例:

假设我的默认build.gradle 具有使用函数的Task

Let's say I have my default build.gradle with a Task which uses a function

task doSomethingWithMyFunction {
    myFunction()
}

现在我有 functions.gradle

def myFunction(){
}

所以我想从 build.gradle

推荐答案

无法使用函数来完成,但是如果将函数转换为闭包,下面的示例将很好地工作:

It's not possible to be done with functions, but if you turn the functions into closures the following example will work well:

lol.gradle

project.ext.lolFunction = {
   println it
}

build.gradle

apply from: 'lol.gradle'

ext.lolFunction(1)                                                             

这篇关于使用其他Gradle文件中的Gradle功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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