Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle - Recursively include external project and its subprojects

Tags:

gradle

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.

like image 630
Zach Thacker Avatar asked Feb 27 '15 16:02

Zach Thacker


1 Answers

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 compile the external entities they fail to resolve with the expected names.

I found that you can rename the external projects in your main project so that they match the names of the external projects. That way your main project and the external projects 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"))
like image 130
Emmanuel Rodriguez Avatar answered Oct 17 '22 06:10

Emmanuel Rodriguez