Java 如何将一个 Gradle 脚本导入另一个脚本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2265283/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I import one Gradle script into another?
提问by Anthony Roy
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.
我有一个复杂的 gradle 脚本,它包含了围绕构建和部署多个 netbeans 项目到多个环境的大量功能。
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.
我想将任务抽象到另一个文件中,以便我可以简单地在一个简单的构建文件中定义我的地图,并从另一个文件导入任务。通过这种方式,我可以对多个项目使用相同的核心任务,并使用一组简单的映射来配置这些项目。
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.
谁能告诉我如何以与 Ant 任务类似的方式将一个 gradle 文件导入另一个文件?到目前为止,我已经搜索了 Gradle 的文档,但无济于事。
Additional Info
附加信息
After Tom's response below, I thought I'd try and clarify exactly what I mean.
在汤姆在下面的回复之后,我想我会尝试澄清我的意思。
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.
基本上我有一个运行许多子项目的gradle脚本。但是,子项目都是 Netbeans 项目,并且带有自己的 ant 构建脚本,所以我在 gradle 中有任务来调用每个。
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.
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.
所以在这个例子中,tasks.gradle 将包含所有通用任务生成代码,并将为主 build.gradle 文件中定义的项目运行。这样,tasks.gradle 是一个所有大型项目都可以使用的文件,这些大型项目由多个带有 Netbeans ant 构建文件的子项目组成。
采纳答案by Anthony Roy
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.
问题的答案原来是在插件系统中,在那里您可以在一组插件中添加所需的功能,这些插件可以是位于目录中的 groovy 文件buildSrc/src/main/groovy
。插件也可以捆绑为 Jar,尽管我还没有尝试过。
Details here: Custom Plugins
此处的详细信息:自定义插件
回答by Tom
Well, it is hard to tell what serves you best without actually seeing your build file.
好吧,如果没有实际看到您的构建文件,很难说出什么对您最有用。
I could assume that stetting up your environment as multi-project build should provide you the abstraction you are looking for.
我可以假设将您的环境设置为多项目构建应该为您提供您正在寻找的抽象。
In your project root build.gradle
you define all your domain specific stuff as well as the things that apply to allyour subprojects:
在您的项目根目录中, build.gradle
您定义了所有特定于域的内容以及适用于所有子项目的内容:
repositories {
add(new org.apache.ivy.plugins.resolver.FileSystemResolver()) {
name = 'destRepo'
addIvyPattern( file( project.properties['repo.dest.dir']).absolutePath + '/[organisation]/[module]/ivys/ivy(-[revision]).xml')
addArtifactPattern( file( project.properties['repo.dest.dir']).absolutePath + '/[organisation]/[module]/[type]s/[artifact](-[revision]).[ext]')
descriptor = 'optional'
checkmodified = true
}
...
}
...
subprojects {
sourceCompatibility = 1.5
targetCompatibility = 1.5
group = 'my.group'
version = '1.0'
uploadArchives {
uploadDescriptor = true
repositories {
add rootProject.repositories.destRepo
}
}
apply{ type my.group.gradle.api.plugins.MyPlugin }
...
}
dependsOnChildren()
The project root directory might also contain a gradle.properties
file where you define properties used by your projects:
项目根目录可能还包含一个gradle.properties
文件,您可以在其中定义项目使用的属性:
buildDirName=staging
repo.dest.dir=/var/repo
...
Then in an additional file from your project root named settings.gradle
you actually point to your subprojects:
然后在您的项目根目录中的一个附加文件中,您settings.gradle
实际上指向您的子项目:
include 'my-first-component',
'my-second-component'
...
project(':my-first-component').projectDir = new File(rootDir, 'path/to/first/component')
project(':my-second-component').projectDir = new File(rootDir, 'path/to/second/component')
...
Each sub-project directory contains a build.gradle
file containing the sub-project specific stuff only.
每个子项目目录都包含一个build.gradle
仅包含子项目特定内容的文件。
No matter if you invoke gradle
from your project root or sub-project directory, gradle will automatically consider all your definitions done in the various files.
无论您是gradle
从项目根目录还是子项目目录调用,gradle 都会自动考虑您在各种文件中完成的所有定义。
Also note that no compile task will be executed for your project root as long as you don't load any plugin beyond the default plugin at the root level.
另请注意,只要您不在根级别加载默认插件之外的任何插件,就不会为您的项目根执行任何编译任务。
回答by Andrey Adamovich
There is a new feature in 0.9. You can use apply from: 'other.gradle'
command.
0.9 中有一个新功能。您可以使用apply from: 'other.gradle'
命令。
Read my question about same thing at: Is there a way to split/factor out common parts of Gradle build
阅读我关于同一件事的问题:Is there a way to split/factor out of common parts of Gradle build