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

问题描述

我有一个名为 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 - 递归地包含外部项目及其子项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-30 13:15