Gradle - Recursively include an external project and its subprojects

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

I have a project called service , which depends on one of the shared-library modules. for example, it depends on :shared-library:module1 . I usually get this maven dependency.

Now I want to modify the shared-library and check my changes using a dependent project. Instead of modifying the shared-library , creating, deploying to maven, and then restoring my service , I would instead like the service to depend directly on the shared-library gradle project.

So, I found that you can point gradle to arbitrary project directories in the file system:

service /settings.gradle

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

But when I do this, the project does not know about the shared-library submodules. I can not do it:

service /build.gradle

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

So, I tried include directly. :shared-library:module1 depends on :shared-library:module2 , so I also included it:

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 find a project named :module2 . This is due to the fact that its dependency is configured as such:

shared library / module 1 / build.gradle

 compile( project(":module2") ) 

But if I change this to the absolute path of the project, now the shared-library cannot compile on its own:

shared library / module 1 / build.gradle

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

tl; dr , there seems to be a discrepancy between how service resolves shared-library submodule names and how shared-library does it.

+6
source share
1 answer

You are right, you can import an external project or even external subprojects and refer to them in your main project. But once you are going to compile external objects, they will not be able to resolve each other with the expected names.

I find out that you can rename external projects to your main project so that they match the names of external projects. Thus, your main project and external projects will use the same name.

Change the 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, your modules should always be : module1 and : module2 . In the service /build.gradle use:

 compile(project(":module1")) 
+2
source

Source: https://habr.com/ru/post/983115/


All Articles