有没有办法拆分/分解 Gradle 构建的公共部分
我们有几个独立的构建(每个独立构建都是一个多项目构建)。主要构建脚本变得相当大,因为我们有一组由子项目重用的常见任务。独立构建之间也存在大量重复。我们正在寻找的是:
- 一种分割主构建文件的方法 分成更小的文件
- 重用某些部分的方法 在其他独立构建中构建
在 Gradle 中实现此目的的最佳方法是什么?
We have several independent builds (each independent build is a multi-project build). The main build scripts have become quite big as we have a set of common tasks reused by subprojects. There is also a lot of repetition between independent builds. What we are looking for is:
- A way to split the main build file
into smaller files - A way to reuse some parts of the
build in other independent builds
What is the best way to achieve this in Gradle?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Gradle 0.9 允许您从另一个构建脚本导入构建脚本。看一下:使用外部构建脚本配置项目。基本上它是
从“other.gradle”应用
。用户指南没有提到的一件事是“from”参数可以是一个 URL,因此您可以通过 HTTP 在某个地方(例如您的 subversion 存储库)提供共享脚本,并从多个构建中导入它们。
Gradle 0.9 allows you to import a build script from another build script. Have a look at: Configuring the project using an external build script. Basically it's
apply from: 'other.gradle'
.One thing the user guide doesn't mention is that the 'from' parameter can be a URL, so you can make your shared scripts available via HTTP somewhere (eg your subversion repository), and import them from multiple builds.
我找到的解决方案意味着映射您在
other.gradle
文件中的内容。然后,在您的
build.gradle
文件中:然后,
versionName
将包含调用结果。注释:
VERConsts['NAME'] = getVersionName()
将调用getVersionName()
并存储其结果。在脚本中使用它,例如versionName VERConsts['NAME']
将分配存储的值。VERConsts['NAME_CALL']
将存储对该函数的引用。在脚本中使用 VERConsts['NAME_CALL']() 实际上会调用该函数并将结果分配给您的变量。前者将导致在整个脚本中分配相同的值,而后者可能会导致不同的值(例如,如果有人在您的脚本运行时推送另一个版本)。
The solution I found implies mapping the things you have in your
other.gradle
file.Then, in your
build.gradle
file:Then, the
versionName
will have the call result.Notes:
VERConsts['NAME'] = getVersionName()
will callgetVersionName()
and store its result. Using it in your script e.g.versionName VERConsts['NAME']
will then assign the stored value.VERConsts['NAME_CALL']
will instead store a reference to the function. UsingVERConsts['NAME_CALL']()
in your script will actually call the function and assign the result to your variableThe former will result in the same value being assigned across the script while the latter may result in different values (e.g. if someone pushes another version while your script is running).