找不到Gradle Spring Boot依赖项多模块项目



我正在尝试设置多模块项目

根项目设置

rootProject.name = 'Abc'
include 'catalog'
include 'catalog-common'

根项目Abc/build.gradle

plugins {
id 'org.springframework.boot' version '2.7.3' apply false
id 'io.spring.dependency-management' version '1.0.13.RELEASE'
id 'java'
}
subprojects {
group = 'com.abc'
apply plugin: 'java'
apply plugin: 'io.spring.dependency-management'
sourceCompatibility = 1.8
targetCompatibility = 1.8
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}

dependencyManagement {
imports {
mavenBom org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES
mavenBom "org.springframework.cloud:spring-cloud-dependencies:2021.0.3"
}
}
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
repositories {
mavenLocal()
mavenCentral()
}

dependencies {
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
}
}

模块目录通用

dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation('org.springframework.boot:spring-boot-starter-validation')
implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

模块目录

plugins {
id 'org.springframework.boot' version '2.7.3'
}
dependencies {
implementation project(':catalog-common')
}

在目录项目中,它希望再次定义spring依赖项,但由于我能够访问java静态类

请帮助

看起来您的catalog-common是一种"图书馆";模块,由其他子项目(catalog和可能的其他子项目(消耗。如果是这样的话,您应该使用可以用于此目的的Java库插件。然后,您将需要配置您想要"的所有依赖项;inherit";在使用CCD_ 3配置而不是CCD_。implementation中声明的依赖项不会泄漏到使用者项目中,这是预期的Gradle行为。

在您的示例中,catalog-common构建脚本应该如下所示:

plugins {
id("java-library")
}
dependencies {
// choose between api or implementation, depending on the scope you want for each dependency
api 'org.springframework.boot:spring-boot-starter'
api 'org.springframework.boot:spring-boot-starter-actuator'
api 'org.springframework.boot:spring-boot-starter-web'
implementation('org.springframework.boot:spring-boot-starter-validation')
implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'
implementation 'org.springframework.boot:spring-boot-starter-test'
}

请注意,在这个公共库中配置一些Spring依赖项(如actuator(似乎有点奇怪:这应该只在main";应用程序";项目(在您的情况下为catalog(,除非您想在catalog-common模块中实现一些依赖于actuator的公共代码。

最新更新