Sharing a content root
My company maintains a monorepo with the following file structure:
monorepo/
common/
service_a/
service_b/
Each of the services (a and b) depend on common by way of listing it as a project dependency in their build.gradle files. In service_a and service_b, you'll see the following:
dependencies {
api project(path: "common", configuration: "default")
//... other dependencies
}
This works fine. Each of the services builds as expected.
When I open service_a or service_b as separate projects in intellij, all of the dependencies resolve as expected. I can jump to the definition of sources in common from both service_a or service_b code. The only problem with this approach is that I now have multiple windows open, one for service_a, service_b, and common.
What I'd like to do is just open the monorepo directory as a project, so that I can see all of my code in one window. Additionally, when I'm looking at service_a or service_b code, I'd like all dependencies to resolve, and to be able to jump to the definition of common code from my service code.
What happens now is the following: I import monorepo as an existing project from sources. IntelliJ spins for a while indexing, and eventually leaves me with a bunch of unresolved dependencies in each of the services, save for one. The one service that the dependencies are resolved for is (typically) service_a (the first service listed alphabetically) - within code for service_a, common code is resolved. I can jump to definitions, see sources, etc. For the rest of the services, I get a bunch of "Unresolved reference" errors in IntelliJ.
When I open my project structure, I see that the common module listed among the modules in the project. I've tried going to each of the services and adding common as a dependency, but then I get the error "Two modules in a project cannot share the same content root".
How can I achieve what I want? Namely, being able to have multiple projects in the same window, all of which share a content root, and having all of the project dependencies resolve without issue?
Please sign in to leave a comment.
You should use Gradle multi-project with the common build.gradle in the monorepo root that you will import, see https://intellij-support.jetbrains.com/hc/en-us/community/posts/360003318419-Muliti-Project-or-Module-guide for the related discussion.
This is an interesting idea, but the examples don't make it clear how a service would reference a common module. For example, the repository linked to in that example only contains downstream dependencies from the common build.gradle.
Please see https://stackoverflow.com/a/38878290/104891 for the sample configuration. It's really common to Gradle and is not IDE specific.