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

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

问题描述

我有以下项目结构:

I have the following project structure

-->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看起来像 p>

and Project1 settings.gradle will looks like

include 'SubProject11'
         'SubProject12'

其他depndecy命令是在相应的build.gradle文件中定义的
如果我将rand gradle clean build安装在根位置(Standar位置)内,它似乎不会使用配置在项目级别settings.gradle文件中。

other depndecy 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.

我在这里做了什么错?

What I'm doing here wrong?

推荐答案

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

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

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

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添加一个新项目时, A / settings.gradle 也必须改变如果新项目仅由B使用。为避免这种情况,您可以尝试 apply B / settings.gradle A / 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 projectDir c $ c>和:bap 。它错误地认为,当从该项目根目录调用Gradle时,B's包含相对于 settingsDir 恰好是 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'

rootProject.children.each { project ->
    String relativeProjectPath = project.projectDir.path.replace(settingsDir.path, "")
    project.projectDir = new File("B/$relativeProjectPath")
}

这将剥离 settingsDir.path ,并以 B / 。这可以通过让每个图层都添加到路径中来扩展到设置[-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天全站免登陆