我正在尝试建立多模块项目。
RootProject设置
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')
}在Catalog项目中,它期望再次定义spring依赖项,但在这里,我可以访问java静态类
请帮帮忙
发布于 2022-08-22 18:43:56
看起来您的catalog-common是一种“库”模块,由其他子项目(catalog和其他项目)使用。如果是这样的话,您应该使用可以用于此目的的Java库插件。然后,您将需要使用api配置而不是implementation来配置您想要在使用者项目中“继承”的所有依赖项。在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'
}请注意,在这个公共库中配置一些像actuator这样的Spring依赖项似乎有点奇怪:这应该只在主“应用程序”项目中声明(在您的例子中是catalog ),除非您希望在catalog-common模块中实现一些依赖于actuator的通用代码。
https://stackoverflow.com/questions/73448952
复制相似问题