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.
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'
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
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With