Gradle - 递归地包含外部项目及其子项目 [英] Gradle - Recursively include external project and its subprojects

查看:854
本文介绍了Gradle - 递归地包含外部项目及其子项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为共享库的许多子模块的gradle项目。我有一个名为 service ,这取决于共享库的其中一个模块。例如,它取决于:shared-library:module1 。现在我想修改共享库,并使用测试我的更改依赖项目。而不是改变共享库,构建,部署到maven,然后重建我的服务,我会像 service 直接取决于共享库 gradle项目 。所以我发现你可以将gradle指向文件系统上的任意项目目录:
$ b

service / settings.gradle

  includeshared-library
project(:shared-library) .projectDir = new File(/ projects / shared-library)

但是当我这样做时,该项目不知道共享库的子模块。我不能这样做:

service / build.gradle

  compile(
project(:shared-library:module1),

所以我直接尝试 include :shared-library:module1 取决于:shared-library:module2 所以我也包含了这个:



service / settings.gradle

  include 共享库
project(:shared-library)。projectDir = new File(/ projects / shared-library)
includeshared-library:module2
include shared-library:module1

但是现在当我尝试运行它时,它抱怨:shared-library:module1 找不到名为:module2 的项目。这是因为它的依赖关系是这样配置的:

共享库/ module1 / build.gradle


  compile(
project(:module2)

但是,如果我将其更改为绝对项目路径,现在共享库无法自行编译:


$ b

共享库/ module1 / build.gradle

 编译(
project(:shared-library:module2)

tl; dr ,看起来像 service 解析共享库的方式不匹配 code>子模块名称以及共享库如何执行它。

解决方案

你是对的,你可以导入一个外部项目甚至外部子项目,并在你的主项目中引用它们。但是,一旦你要编译外部实体,他们将无法用他们期望的名字解决彼此。



我发现你可以重命名外部项目在您的主项目中,以便它们匹配外部项目的名称。这样您的主项目和外部项目将使用相同的名称。



将您的 service / settings.gradle 更改为:

  includeshared-library
project(:shared-library)。projectDir = new File(/ projects / shared -library)

includeshared-library:module2
project('shared-library:module2')。name =':module2'

include shared-library:module1
project('shared-library:module1')。name =':module1'

现在在您的项目和外部项目中,始终将模块称为:module1 :module2 。在 service / build.gradle 中使用:

  compile(project(:module1))


I have a gradle project with many submodules named shared-library.

I have a project named service that depends on one of the modules of shared-library. e.g., it depends on :shared-library:module1. Normally, I get this dependency from maven.

Now I want to modify shared-library and test my changes using the dependent project. Instead of making a change to shared-library, building, deploying to maven, then rebuilding my service, I'd like to instead have service depend on the shared-library gradle project directly.

So I found out that you can point gradle to arbitrary project directories on the filesystem:

service/settings.gradle

include "shared-library"
project(":shared-library").projectDir = new File("/projects/shared-library")

But when I do this, the project is not aware of shared-library's submodules. I cannot do this:

service/build.gradle

compile(
project(":shared-library:module1"),
)

So I tried includeing them directly. :shared-library:module1 depends on :shared-library:module2 so I include that one as well:

service/settings.gradle

include "shared-library"
project(":shared-library").projectDir = new File("/projects/shared-library")
include "shared-library:module2"
include "shared-library:module1"

But now when I try to run this, it complains that :shared-library:module1 cannot locate a project named :module2. This is because its dependency is configured as such:

shared-library/module1/build.gradle

compile(
project(":module2")
)

But if I change that to an absolute project path, now shared-library cannot compile on its own:

shared-library/module1/build.gradle

compile(
project(":shared-library:module2")
)

tl;dr, it seems like there is a mismatch between the way service resolves the shared-library submodule names and how shared-library does it.

解决方案

You're right you can import an external project or even external subprojects and reference them in your main project. But as soon as you're going to compile the external entities they will fail to resolve each other with their expected names.

I find out that you can rename the external projects in your main project so that they match the names of the external projects. This way your main project and the external projects would use the same name.

Change your service/settings.gradle to:

include "shared-library"
project(":shared-library").projectDir = new File("/projects/shared-library")

include "shared-library:module2"
project('shared-library:module2').name = ':module2'

include "shared-library:module1"
project('shared-library:module1').name = ':module1'

Now in your project and external project refer to your modules always as :module1 and :module2. In service/build.gradle use:

compile(project(":module1"))

这篇关于Gradle - 递归地包含外部项目及其子项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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