Gradle 全局构建目录 [英] Gradle global build directory

查看:23
本文介绍了Gradle 全局构建目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以将 Gradle 配置为对所有项目使用一个全局 build 目录?我需要这个,因为我将所有项目存储在 Google Drive 文件夹中(用于备份和从其他设备快速访问),并且当我构建项目时,Google Drive 在同步项目 build 目录时加载 CPU.所以我想将 build 目录移到 Google Drive 之外.

I want to know is it possible to configure Gradle to use one global build directory for all projects? I need this because I store all my projects in Google Drive folder (for backup and fast access from other devices) and when I build project Google Drive is loading CPU while syncing project build directory. So I want to move build directories outside of Google Drive.

对于那些想要使用 RAM 磁盘作为 build 目录的存储来构建项目的人来说,此选项也很有用.

Also this option will be useful for those who want to build projects using RAM disk as storage for build directory.

推荐答案

感谢 Peter Niederwieser.由于更多细节,我决定发布我自己的答案.

Thanks to Peter Niederwieser. I decided to post my own answer because of additional details.

要设置全局构建目录,您需要将这些行添加到~/.gradle/init.gradle:

To setup global build directory you need to add these lines to ~/.gradle/init.gradle:

gradle.projectsLoaded {
    rootProject.allprojects {
        buildDir = "/path/to/build/${rootProject.name}/${project.name}"
    }
}

RAM 磁盘上拥有全局 build 目录的不错选择.

Nice option to have global build directory on the RAM disk.

如果您是 macOS (OS X) 用户,您可以使用:

If you are macOS (OS X) user you can do this with:

diskutil erasevolume HFS+ 'RAMDiskName' `hdiutil attach -nomount ram://XXXXXX`

其中 XXXXXX 是 512 字节块的计数(例如,对于 128MB RAM 磁盘,它是 262114)

where XXXXXX is a count of 512-byte blocks (for example, it's 262114 for 128MB RAM disk)

buildDir 行将是:

buildDir = "/Volumes/RAMDiskName/${rootProject.name}/${project.name}"

您还可以扩展配置:

  1. 从环境变量或回退到项目目录中的build目录获取全局构建路径;
  2. 在路径中包含项目组,这会使路径更加独特,尤其是当您有多个同名的项目时.
  1. get global build path from environment variable or fallback to build directory inside project directory;
  2. include project group in path, this makes path more unique especially if you have several projects with the same name.

~/.gradle/init.gradle:

def configProject(p, buildDir) {
    p.buildDir = "${buildDir}/${p.name}"  
    p.subprojects { s ->
        configProject(s, "${p.buildDir}")
    }
}

def buildDir = System.env.GRADLE_GLOBAL_BUILD_PATH
if (!buildDir?.trim()) {
    buildDir = "build"
}

gradle.projectsLoaded {
    if (ext.has("group")) {
        buildDir += "/${ext.group}"
    }
    configProject(rootProject, buildDir)
}

在项目settings.gradle中:

gradle.ext.group = 'com.example.yourproject'

您也可以在项目 build.gradle 中使用此设置来设置项目组,但这是可选的:

Also you can use this setting in project build.gradle to set project group but this is optional:

allprojects {
    ...
    group gradle.ext.group
}

这篇关于Gradle 全局构建目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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