Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring a project (module) as a dependecy in another project(another module) but failed

I have created a gradle project named my-root-project, under which there are two other gradle projects: shared and my-service.

In my IntelliJ IDE, I have added shared and my-service as modules of my-root-project. The project structure overall looks like this:

-my-root-project

  -shared
    -build.gradle
    -settings.gradle

  -my-service
    -build.gradle
    -settings.gradle

-build.gradle
-settings.gradle
...

In the settings.gradle of my-root-project I have:

rootProject.name = 'my-root-project'
include ':shared'
include ':my-service'

In build.gradle of my-service, I would like to add shared as its project dependency:

...
dependencies {
   implementation project(':shared')
}

But I get error:

A problem occurred evaluating root project 'my-service'.
> Project with path ':shared' could not be found in root project 'my-service'.

Then, I tried including shared also in the settings.gradle of my-service. (Above, I have already included it in settings.gradle of my-root-project).

Then, build again but get a new error:

Could not resolve project :shared.
Required by:
    project :

Possible solution:
 - Declare repository providing the artifact, see the documentation at https://docs.gradle.org/current/userguide/declaring_repositories.html

Where am I wrong? I would like to have shared project be a dependency in my-service.

like image 683
user842225 Avatar asked Sep 08 '25 04:09

user842225


1 Answers

It sounds like you’re running your Gradle command from within the my-service project directory. As that project contains a settings.gradle file, Gradle considers the project to be the root project. Under this “root project”, there is no subproject called shared, so Gradle complains with the error you saw:

A problem occurred evaluating root project 'my-service'.
> Project with path ':shared' could not be found in root project 'my-service'.

I’d recommend to change two things (but either of them should get you rid of the error):

  1. There should be only a single settings.gradle file in a multi-project build – in the root project, i.e., your my-root-project. If you look at the Gradle docs for multi-project builds, then you’ll see that there is only a single settings.gradle file in each sample project hierarchy.
  2. I would run all Gradle commands from the root project directory. If you need to run a task in a subproject, then use the full path of the task, for example: ./gradlew :my-service:check
    You can find more on executing tasks in multi-project builds in the Gradle docs.
like image 83
Chriki Avatar answered Sep 09 '25 17:09

Chriki