如何配置Gradle脚本以处理具有根级构建的多项目

huangapple go评论70阅读模式
英文:

How to configuration gradle scripts to handle multi project with root level build

问题

这可能吗?

A(文件夹)
-B(独立项目)
-C(独立项目)
-D(独立项目),依赖于B和C

注意:我还需要一个根级别的Gradle文件来在A级别构建所有项目。

到目前为止,我已经得到了这个...

A(build.gradle)根级别

buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
    }
}

allprojects {
    apply plugin: 'maven-publish'

    repositories {
        mavenLocal()
        mavenCentral()
    }
}

A(settings.gradle)根级别

rootProject.name = 'A'
include "B"
include "C"
include "D"

各个独立项目的构建非常简单。但是当我尝试构建项目D时卡住了

dependencies {
    api project(':B')
    api project(':C')
}

基本上Gradle期望B和C在D下面。我明白。但是,我该如何使其按照现有方式工作?

答案:在D的settings.gradle中添加以下内容

rootProject.name = 'D'

include 'B'
project(':B').projectDir = file('../B')

include 'C'
project(':C').projectDir = file('../C')
英文:

Is this possible?

A (folder)
-B (independent project) 
-C (independent project)
-D (independent project) and depends on B and C

Note: I also need a root level gradle to build all the projects at folder A level.

This is what I got so far...

A (build.gradle) root level

buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
    }
}

allprojects {
    apply plugin: 'maven-publish'

    repositories {
        mavenLocal()
        mavenCentral()
    }
}

A (settings.gradle) root level

rootProject.name = 'A'
include "B"
include "C"
include "D"

The individual independent project builds are pretty straight forward. I am stuck when I try to build project D

dependencies {
    api project(':B')
    api project(':C')
}

Basically gradle expects B and C to be under D. I get it. But, how can I get it working the way it is?

ANSWER : added the following in settings.gradle for D

rootProject.name = 'D'

include 'B'
project(':B').projectDir = file('../B')

include 'C'
project(':C').projectDir = file('../C')

答案1

得分: 0

项目结构与项目依赖完全独立。您可以在任何项目之间定义任何有效的(例如非循环的)依赖关系,使用它们的任意项目名称和项目目录。


> 基本上,Gradle 期望 B 和 C 在 D 下面。

不,Gradle 并不期望那样。实际上,您当前的设置应该可以正常工作。所有涉及的(子)项目都必须在一个 单独的 settings.gradle 文件中注册。无论传递给 include 的是什么,它只是一个项目名称,不一定与项目的实际位置有关。include 方法使用一种约定来通过将项目名称转换为路径来推断项目的位置,但可以根据文档中所示进行更改:

// 包括两个项目,'foo' 和 'foo:bar'
// 目录是通过将 ':' 替换为 '/' 来推断的
include 'foo:bar';

// 包括一个项目,其项目目录与逻辑项目路径不匹配
include 'baz';
project(':baz').projectDir = file('foo/baz');

如评论中所解释的,第一个示例将创建两个项目,因为 foo:bar 是一个需要存在名为 foo 的项目的项目路径。您还可以使用 includeFlat 来创建具有默认路径的项目,该路径从根目录的父目录开始(根目录是根项目的项目目录)。

英文:

The project structure is completely independent from project dependencies. You may define any valid (e.g. non-circular) dependency between any projects with their arbitrary project names and project directories.


> Basically gradle expects B and C to be under D.

No, Gradle does not expect that. Actually, your current setup should work without a problem. All involved (sub-)projects must be registered inside a single settings.gradle file. Whatever gets passed to include is just a project name and is not necessarily related to the actual location of the project. The method include uses a convention to infer the location of the project by translating the project name into a path, but it may be changed as shown in the documentation:

// include two projects, 'foo' and 'foo:bar'
// directories are inferred by replacing ':' with '/'
include 'foo:bar'

// include one project whose project dir does not match the logical project path
include 'baz'
project(':baz').projectDir = file('foo/baz')

As explained in the comment, the first example will create two projects, as 'foo:bar' is a project path that requires a project called foo to exist. You may also use includeFlat to create projects with a default path that starts in the parent directory of your root directory (which is the project directory of your root project).

huangapple
  • 本文由 发表于 2020年9月2日 01:24:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/63692602.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定