Vincent F
Vincent F

Reputation: 7331

gradle transitive dependencies not being imported

It must be something pretty obvious, but for some reason, I am missing it after a couple of hours looking at it very closely..

I am building a multi-module Spring Boot project following hexagonal architecture with Gradle 5.1.1, so I have several adapters like this rest-adapter for which the gradle.build file is below :

import org.springframework.boot.gradle.plugin.SpringBootPlugin

apply plugin: 'java-library'
apply plugin: 'io.spring.dependency-management'

dependencyManagement {
    imports {
        mavenBom SpringBootPlugin.BOM_COORDINATES
    }
}

dependencies {
    implementation project(':domain-api')

    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
} 

I can build it, no issue.

In another module, I want to have a dependency on this adapter. So for example in my acceptance-test module :

import org.springframework.boot.gradle.plugin.SpringBootPlugin

apply plugin: 'java'
apply plugin: 'io.spring.dependency-management'

dependencyManagement {
    imports {
        mavenBom SpringBootPlugin.BOM_COORDINATES
    }
}

dependencies {

    implementation project(':domain')
    implementation project(':domain-api')
    implementation project(':rest-adapter')
}

So in acceptance-test , since I am having a dependency on rest-adapter, I would expect to get the "spring web" dependencies available - well, I don't, as I am not able to use org.springframework.http.HttpStatus :

Task :acceptance-test:compileJava FAILED

C:\Users\vf\IdeaProjects\hexagonal-spring-boot-java\acceptance-test\src\main\java\packageName\MyClass.java:3: error: package org.springframework.http does not exist import org.springframework.http.HttpStatus; ^

I had a doubt that it was due to the fact I was using a Spring Boot starter as a dependency, so I tried this in rest-adapter instead :

dependencies {
    implementation project(':domain-api')

    implementation group: 'org.springframework', name: 'spring-web'
    compile "javax.servlet:javax.servlet-api:4.0.1"
    //implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'    
}

But I am getting the same result.

I am really getting clueless here.. It's not the first time I am doing a Gradle multi-nodule project with Spring Boot, and even when I compare with my other projects, I don't see any difference.

Any idea of what could go wrong here ?

Upvotes: 1

Views: 3749

Answers (1)

paul58914080
paul58914080

Reputation: 312

I am a newbie to Gradle. So maybe there is a better way to do this, but I will try to answer

I think the issue is for the sub-project's transitive dependency to be accessed by another sub-project we need to make it api rather than implementation.

In rest-adapter for its internal dependency to be available in acceptance-test like in this case

implementation 'org.springframework.boot:spring-boot-starter-web'

change it to api i.e.

api 'org.springframework.boot:spring-boot-starter-web'

https://docs.gradle.org/current/userguide/java_library_plugin.html#sec:java_library_separation

Upvotes: 6

Related Questions