Search code examples
javagradledependencies

Why gradle removes dependency when I build a project


I have no idea why gradle is doing this to me. I have a multiproject that looks like this:

|Parent
 |client
 |common
 |service

Parent is just an empty project and client, common and service are gradle java projects: I have some classes that are used in both client and service, therefore I wanted to create a common project and build a jar that I would later use in both service and client. I can build the common jar, but whenever i try to do 'add dependency on common' and then try to 'refresh gradle', it removes the dependency and fails to build!
This is what I do: enter image description here

Then I press this because I want to build it: enter image description here

And it just removes the dependency!!!

enter image description here

This is build.gradle file from client project:

plugins {
id 'org.springframework.boot' version '2.2.5.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
}

group = 'sot.rest'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'org.springframework', name: 'spring-web', version: '5.2.4.RELEASE'
    implementation 'org.springframework.boot:spring-boot-starter'
    // https://mvnrepository.com/artifact/com.google.code.gson/gson
    compile group: 'com.google.code.gson', name: 'gson', version: '2.8.6'
}

Please help, Im desperate!!


Solution

  • Check out my answer on your previous question. It should give you an idea how to structure and declare the multi-module gradle projects.

    I think that when you add the dependency on module with IntelliJ, it just adds it to the project structure through project settings in IntelliJ. And later, when you hit refresh, IntelliJ configures the project based on Gradle files.

    To make it working, the Parent project should also be a Gradle project. If it isn't just add build.gradle and settings.gradle under the Parent directory.
    Then in settings.gradle add the subprojects like:

    rootProject.name = 'Parend'
    include('common') //this adds the module
    include('client')
    include('service')
    

    And later, in build.gradle files of client and service modules add the common module as dependency with:

    dependencies {
        implementation project(':common')
        //...
    }
    

    If you are going to work more with Gradle, you could take a look at this article about overall insight on Gradle.

    Edit:
    (I understood that when you use implementation it doesn't throw errors)

    To work with multimodule project with Gradle, the root project also needs to be a Gradle project. The root might or might not contain any source code, but it needs to have its own Gradle files.
    So if your project structure needs to be like:

    Root project 'parent'
    +--- Project ':client'
    +--- Project ':common'
    \--- Project ':service'
    

    Then the parent and submodule projects need to be set as Gradle projects. To make it the parent project needs to have at least a settings.gradle file and declared includes for submodules, similarly to:

    rootProject.name = 'parent' 
    include 'common'
    include 'client'
    include 'service'
    

    Each of modules (client,common,service) must have a build.gradle files under its directory. Submodules using common, so the service and client must add the common as dependency in their own build.gradle files, like:

    dependencies {
        implementation project(':common')
        //and rest of required dependencies
        //testCompile group: 'junit', name: 'junit', version: '4.12'
    }
    

    Then you should be able to import public classes from common in those submodules and rebuild or reimport project without error.

    Since the parent project doesn't contain any source code, then it doesn't need its own build script, but then build file of all of the submodules needs to declare the java plugin on top of the build file:

    plugins {
        id 'java'
    }
    

    Since you are working with IntelliJ and your project could have different structure previously, then the project structure in IntelliJ setting could be messed up now.
    To fix it you could go to File->Project Structure->Modules and remove/reimport the parent module again.

    If you don't have many classes now, I'd recommend you to create a new project. In the "New Project" window pick Gradle and uncheck Java in "Additional Libraries and Frameworks". This will create blank Gradle project.
    After the project is generated, do a right mouse click on parent, and select New->Module. In new module window again pick Gradle and leave the Java checked (since the submodules will contain source code).
    With that, the IntelliJ will automatically include created module to the settings.gradle file of root/parent project and the build file of that module will contain the basic configuration (e.g. the java plugin).
    But you still add the dependency of one module in another in the build.gradle file of that module.