Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include gradle multi-project repository as git submodule in another gradle project

I'm trying to import a Gradle multi-project repository into another Gradle project as a git submodule.

This is the structure of the multi-project repo:

MyLibrary
- lib-api
  - api-config
  - api-repository
- lib-impl
  - impl-config
  - impl-repository

Within the Gradle project MyLibrary, there are several modules that depend on other modules within that project. For example, lib-api:api-repository has the following dependency:

implementation project(':lib-api:api-config')

(the modules under lib-impl also include their respective api as a dependency)

So far everything is ok, I can build, run and test the projects under lib-impl.

Now I try to import the project into another project

I use git submodule add to add MyLibrary to MyProject. This is now the structure:

MyProject
- MyLibrary
  - lib-api
    - api-config
    - api-repository
  - lib-impl
    - impl-config
    - impl-repository
- api
- impl

In order to be able to use the modules under MyLibrary, I edit the settings.gradle file in MyProject to look something like this:

rootProject.name = 'MyProject'
include 'api'
include 'impl'
include 'mylibrary'
include 'mylibrary:lib-api'
include 'mylibrary:lib-impl'
include 'mylibrary:lib-api:api-config'
include 'mylibrary:lib-api:api-repository'
include 'mylibrary:lib-impl:impl-config'
include 'mylibrary:lib-impl:impl-repository'

The problem

Now that the modules under MyLibrary belong to the root project MyProject, all the internal dependencies within MyLibrary need to be prefixed with :mylibrary. But this is only the case if MyLibrary is currently a submodule of another project.

In order to build MyProject, I need to change the dependencies inside MyLibrary from this:

implementation project(':lib-api:api-config')
implementation project(':lib-api:api-repository')

to this:

implementation project(':mylibrary:lib-api:api-config')
implementation project(':mylibrary:lib-api:api-repository')

However, :mylibrary needs to NOT be present for MyLibrary to compile successfully by itself (i.e. if MyLibrary is the root project). But this change IS required for MyLibrary to work as a submodule.

This used to not be a big problem as I could always pull the submodule MyLibrary and edit the necessary build.gradle files. However, I have now started trying to set up continuous integration and I get errors about those imports being invalid inside MyLibrary whenever I try to build MyProject as CI will always get the code directly from the git repo.

I feel like if the following statement was valid, it could fix my problem:

implementation project(':mylibrary:lib-api:api-config').ifNotPresent(':lib-api:api-config')

Thanks in advance for any help

like image 464
Alex Avatar asked Oct 22 '25 10:10

Alex


1 Answers

After a brilliant revelation, I realized that java code was allowed inside of the build.gradle file so naturally I did this:

dependencies {
    if (rootProject.getName().equals("MyLibrary")) {
        implementation project(':lib-api:api-config')
    } else {
        implementation project(':mylibrary:lib-api:api-config')
    }
}

And it works!

like image 67
Alex Avatar answered Oct 25 '25 13:10

Alex