简体   繁体   English

Gradle子项目dependecies不起作用,但相同的依赖项在main gradle中正常工作

[英]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. 我一直在为spring boot devtools开发多模块gradle项目。 Here is the github repo - GitHub Repo 这是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

This how my build.gradle(main gradle)looks like : - 这是我的build.gradle(主要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")
                }

            }

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 因此,您可以看到我是否将compile ("org.springframework.boot:spring-boot-starter-web")放入project(':spring-boot-dev-tools')我的Spring启动应用程序从端口8000开始并保持赛跑

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. 但是当我在spring-boot-dev-tools.gradle移动gradle脚本时,我遇到问题,然后我的spring启动应用程序就像普通的spring启动应用程序一样启动和关闭。

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. 因此,如果我在子模块中移动spring spring-boot-starter-webspring-boot-devtools依赖项时总结了我的问题,则spring boot无法在port:8000上运行/运行,而是像普通的spring boot一样启动和关闭应用。

Is there a reason why you have defined main class in every separate Java package? 你有没有在每个单独的Java包中定义主类的原因?

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. 定义一个公共的gradle配置,而不是“allprojects”和“subprojects”关键字。 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 否则使用关键字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) 我将子项目/模块依赖项移到了自己的gradle文件中,并将其从build.gradle中删除(主项目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' } } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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