首页 > 解决方案 > Gradle 子项目依赖项不起作用,但相同的依赖项在主 gradle 中工作正常

问题描述

我一直在为 spring boot devtools 开发多模块 gradle 项目。这是 github 存储库 - GitHub 存储库

-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

这就是我的 build.gradle(main gradle) 的样子: -

            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")
                }

            }

因此,您可以看到我是否将我的 Spring Boot 应用程序放入端口 8000 并继续compile ("org.springframework.boot:spring-boot-starter-web")运行project(':spring-boot-dev-tools')

但是当我在里面移动以下 gradle 脚本时遇到问题spring-boot-dev-tools.gradle,然后我的 spring boot 应用程序就像普通的 spring boot 应用程序一样启动和关闭。

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")
                }

spring-boot-starter-web因此,如果我在子模块中移动 spring和 依赖项时总结了我的问题spring-boot-devtools,spring boot 在端口:8000 上不起作用/运行,而是像普通的 spring boot 应用程序一样启动和关闭。

标签: javaspringspring-bootgradle

解决方案


您是否有理由在每个单独的 Java 包中定义主类?

我最近做了一个模块化单体示例,它可能会对您有所帮助: 模块化单体示例

还有一些提示要考虑:

  • 定义一个通用的 gradle 配置而不是 "allprojects" 和 "subprojects" 关键字。这两者之间的区别归结为组合而不是继承

  • 使用关键字implementation而不是compile。这样,您的依赖项就不会再泄漏到消费者的编译类路径中了。否则使用关键字api


推荐阅读