简体   繁体   中英

Gradle subproject dependecies does not work but same dependencies works fine in main gradle

I have been working on multi module gradle project for spring boot devtools. Here is the github repo - GitHub Repo

-spring-boot-dev-tools
-src/main
    -java/com/jhooq/springboot/devtools
    -resources
    -spring-boot-dev-tools.gradle ====- subproject gradle 
-.gitignore
-build.gradle ====- main gradle
-gradlew
-gradlew.bat
-settings.gradle

This how my build.gradle(main gradle)looks like : -

            buildscript {
            ext {
                    springBootVersion = '2.1.2.RELEASE'
                }
                repositories {
                    mavenCentral()
                }
                dependencies {
                    classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
                }
                        }
            allprojects {
                group 'com.jhooq'
                version '1.0-SNAPSHOT'
            }
            subprojects{
                repositories {
                    mavenCentral()
                }
                apply plugin: 'java'
                apply plugin: 'idea'
                apply plugin: 'org.springframework.boot'
                apply plugin: 'io.spring.dependency-management'

                sourceCompatibility = 1.8
                targetCompatibility = 1.8

                dependencies {
                    compile ("org.springframework.boot:spring-boot-starter")
                    compile ("org.springframework.boot:spring-boot-starter-test")
                }
            }
            project(':spring-boot-dev-tools'){

                configurations {
                    developmentOnly
                    runtimeClasspath {
                        extendsFrom developmentOnly
                    }
                }

                dependencies {
                    compile project(':spring-boot-app')
                    compile ("org.springframework.boot:spring-boot-starter-web")
                    developmentOnly("org.springframework.boot:spring-boot-devtools")
                }

            }

So as you can see if i put compile ("org.springframework.boot:spring-boot-starter-web") inside project(':spring-boot-dev-tools') my spring boot application starts on port 8000 and keeps running

But i face issue when i move following gradle scripts inside spring-boot-dev-tools.gradle , then my spring boot application starts and shutdown just like normal spring boot application.

project(':spring-boot-dev-tools'){

                configurations {
                    developmentOnly
                    runtimeClasspath {
                        extendsFrom developmentOnly
                    }
                }

                dependencies {
                    compile project(':spring-boot-app')
                    compile ("org.springframework.boot:spring-boot-starter-web")
                    developmentOnly("org.springframework.boot:spring-boot-devtools")
                }

So if i summarize my issue when i move spring spring-boot-starter-web and spring-boot-devtools dependencies inside submodule, spring boot doesn't work/run on port:8000 but instead it starts and shutdown like a normal spring boot application.

Is there a reason why you have defined main class in every separate Java package?

I recently made a modular monolith example which might help you: modular monolith example

Also some tips to consider:

  • define a common gradle configuration instead of "allprojects" and "subprojects" keywords. Difference between these two comes down to composition over inheritance

  • use keyword implementation instead of compile . That way your dependencies do not leak into the compile classpath of consumers anymore. Otherwise use keyword api

I managed to get it working but still the solution is apparently does not feel good to me. But anyways here is what i did -

  1. I moved sub project/module dependency to its own gradle file and removed it from build.gradle(main project gradle)
  2. Instead of "compile project" i switched to "implementation"

      configurations { developmentOnly runtimeClasspath { extendsFrom developmentOnly } } dependencies { implementation { 'org.springframework.boot:spring-boot-devtools' ':spring-boot-app' 'org.springframework.boot:spring-boot-starter-web' } } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM