用于多个项目构建的多个设置 gradle 文件 [英] Multiple settings gradle files for multiple projects building

查看:27
本文介绍了用于多个项目构建的多个设置 gradle 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下项目结构

-->Starnderd Location
        -->Project1 
           -->settings.gradle 
           -->build.gradle
           -->Subproject11
              -->build.gradle
           -->Subproject12
              -->build.gradle
        -->Project2 
           -->settings.gradle 
           -->build.gradle
           -->Subproject21
              -->build.gradle
           -->Subproject22
              -->build.gradle
        -->build.gradle
        -->settings.gradle

上述项目结构的想法是我们有多个项目,其中包含子项目,每个项目都可以依赖于其他项目.项目内的子项目也可以依赖于同一项目内的其他子项目.项目将在根目录的 settings.gradle 中指定.每个项目中的 settings.gradle 也会说明该特定项目的子项目是什么.

Idea of above project structure is that we have multiple projects which contains subprojects, each project can have dependencies to other projects. Also subprojects inside the project can have dependencies to other subprojects within the same project. Projects will be specified in the settings.gradle in the root. Also settings.gradle inside each project will say what are the subprojects of that particular project.

我在根目录下的 settings.gradle 看起来像

My settings.gradle in the root would looks like

include 'Project1',
         'Project2'

和 Project1 settings.gradle 看起来像

and Project1 settings.gradle will looks like

include 'SubProject11'
         'SubProject12'

其他依赖顺序在各自的build.gradle文件中定义如果我在根位置(标准位置)内 rand gradle clean build install 它似乎没有使用项目级别 settings.gradle 文件中的配置.

other dependency orders are defined in the respective build.gradle files If I rand gradle clean build install inside the root location(Standar location) it doesn't seems to use configurations in the Project level settings.gradle file.

我在这里做错了什么?

推荐答案

我能够以一种相对干净的方式解决这个问题.当然欢迎改进!

I was able to solve this problem in a relatively clean way. Improvements are certainly welcome!

尽管 Gradle 不支持多个开箱即用的 settings.gradle 脚本,但可以创建单独的子项目,每个子项目都有自己的 settings.gradle 文件.假设您有一个依赖于多项目 B 的多项目 A,每个项目都有自己的子项目.您的目录结构可能如下所示:

Although Gradle does not support multiple settings.gradle scripts out of the box, it is possible to create individual sub-projects each with their own settings.gradle file. Let's say you have multi-project A that depends on multi-project B, each with their own sub-projects. Your directory structure might look like:

A
- settings.gradle
- foo
- faz
 B
  - settings.gradle
  - bar
  - bap

开箱即用,Gradle 期望 A/settings.gradle 看起来像这样:

Out of the box, Gradle expects A/settings.gradle to look something like this:

include ':foo', ':faz', 'B:bar', 'B:bap'

这样做的问题是每次 B 添加一个新项目时,即使新项目只被 B 使用,A/settings.gradle 也必须改变.为了避免这种情况,你可以尝试在 A/settings.gradleapply B/settings.gradle 而不是添加冗余声明:

The problem with this is that every time B adds a new project, A/settings.gradle must also change even if the new project is only used by B. To avoid this situation, you could try to apply B/settings.gradle in A/settings.gradle instead of adding redundant declarations:

apply from: 'B/settings.gradle'
include ':foo', ':faz'

如果您尝试这样做,您会发现 Gradle 失败,因为它为 :bar:bap 生成了错误的 projectDir.它错误地假设 B 的包含与 settingsDir 相关,当从该项目根调用 Gradle 时,它​​恰好是 A/.要解决此问题,您可以添加另一个脚本,例如 B/settings-parent.gradle(确切名称不重要):

If you try this, you'll find that Gradle fails because it generates the wrong projectDir for :bar and :bap. It mistakenly assumes that B's includes are relative to settingsDir which happens to be A/ when Gradle is invoked from that project root. To fix this you can add another script such as B/settings-parent.gradle (the exact name is not significant):

apply from: 'settings.gradle'

def updateProjectPaths(Set<ProjectDescriptor> projects) {
    projects.each { ProjectDescriptor project ->
        String relativeProjectPath = project.projectDir.path.replace(settingsDir.path, "")
        project.projectDir = new File("B/$relativeProjectPath")
        // Recursively update paths for all children
        updateProjectPaths(project.children)
    }
}

updateProjectPaths(rootProject.children)

这会去除 settingsDir.path 并在路径前加上 B/ 前缀.通过让每一层将自身添加到路径中,这可以扩展到多层 settings[-parent].gradle 文件.现在你将把这个脚本应用到 A/settings.gradle:

This strips settingsDir.path and prefixes the path with B/. This can be extended to multiple layers of settings[-parent].gradle files by having each layer add itself onto the path. Now you will apply this script to A/settings.gradle:

apply from: 'B/settings-parent.gradle'
include ':foo', ':faz'

使用这种方案,新的 B 项目不会不必要地破坏 A/settings.gradle 并且所有项目都可以在不明确引用 B 子项目的情况下使用.例如,如果 ':foo' 想使用 'B:bap',它可以简单地声明:

With this scheme, new B projects do not unnecessarily break A/settings.gradle and all projects can be used without explicitly referencing the B sub-project. For example, if ':foo' wanted to use 'B:bap', it may simply declare:

compile project(':bap')

这篇关于用于多个项目构建的多个设置 gradle 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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