我正在使用 gradle 4.x,这是我的问题。
我有两个模块 A 和 B。
模块 B 对模块 A 具有项目依赖关系。这是 gradle 文件。
模块 A 的 build.gradle
apply plugin: 'java-library'
dependencies {
implementation group: 'org.springframework.boot', name: 'spring-boot-starter'
implementation group: 'org.springframework.kafka', name: 'spring-kafka' , version: '2.2.7.RELEASE'
implementation group: 'org.springframework.cloud', name: 'spring-cloud-spring-service-connector'
implementation group: 'org.springframework.cloud', name: 'spring-cloud-cloudfoundry-connector'
testImplementation group: 'org.springframework.boot', name: 'spring-boot-starter-test'
testImplementation group: 'io.github.benas', name: 'random-beans', version: '3.7.0'
}
task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}
artifacts {
archives sourcesJar
}
这是模块 B 的 build.gradle
dependencies {
implementation project(':moduleA')
implementation group: 'org.springframework.boot', name: 'spring-boot-starter'
implementation group: 'org.springframework.kafka', name: 'spring-kafka' , version: '2.2.7.RELEASE'
implementation group: 'org.springframework.cloud', name: 'spring-cloud-spring-service-connector'
implementation group: 'org.springframework.cloud', name: 'spring-cloud-cloudfoundry-connector'
testImplementation group: 'org.springframework.boot', name: 'spring-boot-starter-test'
testImplementation group: 'io.github.benas', name: 'random-beans', version: '3.7.0' }
task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}
artifacts {
archives sourcesJar
}
如果您观察模块 B 的 build.gradle 文件,我有 spring-kafka 的依赖项,但它是重复的,因为它已经在模块 A build.gradle 中。
现在,当模块 B 的 jar 文件被另一个应用程序使用时,如何使用 gradle API 选项来公开 spring-kafka(来自模块 A)而不在模块 B 的 build.gradle 中实际提及它?
请建议。
最佳答案
这在文档 here 中有解释。 (强调我的):
The plugin exposes two configurations that can be used to declare dependencies:
api
andimplementation
. Theapi
configuration should be used to declare dependencies which are exported by the library API, whereas theimplementation
configuration should be used to declare dependencies which are internal to the component.
所以只需更改
implementation
至api
在 moduleA
:api group: 'org.springframework.kafka', name: 'spring-kafka' , version: '2.2.7.RELEASE'
然后从
moduleB
中删除条目的 Gradle 文件。
关于Gradle 4.x API 而不是实现 - 我如何使用 gradle API 功能来公开内部依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58509996/