问题描述
我有一个复杂的 gradle 脚本,它包含了围绕构建和部署多个 netbeans 项目到多个环境的大量功能.
I have a complex gradle script that wraps up a load of functionality around building and deploying a number of netbeans projects to a number of environments.
该脚本运行良好,但本质上都是通过六个包含项目和环境信息的地图进行配置的.
The script works very well, but in essence it is all configured through half a dozen maps holding project and environment information.
我想将任务抽象到另一个文件中,以便我可以在一个简单的构建文件中简单地定义我的地图,并从另一个文件导入任务.通过这种方式,我可以为多个项目使用相同的核心任务,并使用一组简单的映射配置这些项目.
I want to abstract the tasks away into another file, so that I can simply define my maps in a simple build file, and import the tasks from the other file. In this way, I can use the same core tasks for a number of projects and configure those projects with a simple set of maps.
谁能告诉我如何以与 Ant 任务类似的方式将一个 gradle 文件导入另一个文件?到目前为止,我已经搜索了 Gradle 的文档,但无济于事.
Can anyone tell me how I can import one gradle file into another, in a similar manner to Ant's task? I've trawled Gradle's docs to no avail so far.
附加信息
在汤姆在下面的回答之后,我想我会尝试澄清我的意思.
After Tom's response below, I thought I'd try and clarify exactly what I mean.
基本上我有一个运行多个子项目的 gradle 脚本.但是,子项目都是Netbeans项目,并且自带ant构建脚本,所以我在gradle中有任务来调用每一个.
Basically I have a gradle script which runs a number of subprojects. However, the subprojects are all Netbeans projects, and come with their own ant build scripts, so I have tasks in gradle to call each of these.
我的问题是我在文件顶部有一些配置,例如:
My problem is that I have some configuration at the top of the file, such as:
projects = [
[name:"MySubproject1", shortname: "sub1", env:"mainEnv", cvs_module="mod1"],
[name:"MySubproject2", shortname: "sub2", env:"altEnv", cvs_module="mod2"]
]
然后我生成任务,例如:
I then generate tasks such as:
projects.each({
task "checkout_$it.shortname" << {
// Code to for example check module out from cvs using config from 'it'.
}
})
我有很多这样的任务生成片段,它们都是通用的 - 它们完全取决于项目列表中的配置.
I have many of these sort of task generation snippets, and all of them are generic - they entirely depend on the config in the projects list.
所以我想要的是一种将它放在一个单独的脚本中并以下列方式导入它的方法:
So what I want is a way to put this in a separate script and import it in the following sort of way:
projects = [
[name:"MySubproject1", shortname: "sub1", env:"mainEnv", cvs_module="mod1"],
[name:"MySubproject2", shortname: "sub2", env:"altEnv", cvs_module="mod2"]
]
import("tasks.gradle") // This will import and run the script so that all tasks are generated for the projects given above.
所以在这个例子中,tasks.gradle 将包含所有通用任务生成代码,并将为主 build.gradle 文件中定义的项目运行.这样,tasks.gradle 是一个可以被所有大型项目使用的文件,这些大型项目由许多带有 Netbeans ant 构建文件的子项目组成.
So in this example, tasks.gradle will have all the generic task generation code in, and will get run for the projects defined in the main build.gradle file. In this way, tasks.gradle is a file that can be used by all large projects that consist of a number of sub-projects with Netbeans ant build files.
推荐答案
问题的答案原来是在 Plugins 系统中,在那里你可以在一组插件中添加所需的功能,这些插件可以是位于目录 buildSrc/src/main/groovy
.插件也可以打包成 Jar,不过我没试过.
The answer to the question turned out to be in the Plugins system, where you can add the desired functionality in a set of plugins which can be groovy files located in the directory buildSrc/src/main/groovy
. Plugins can also be bundled as a Jar though I haven't tried this.
此处的详细信息:自定义插件
这篇关于如何将一个 Gradle 脚本导入另一个脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!