Gradle 4.x API 而不是实现 - 我如何使用 gradle API 功能来公开内部依赖项

标签 gradle

我正在使用 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 and implementation. The api configuration should be used to declare dependencies which are exported by the library API, whereas the implementation configuration should be used to declare dependencies which are internal to the component.



所以只需更改 implementationapimoduleA :
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/

相关文章:

java - 如何将jcompilo作为gradle依赖项添加到android studio项目

gradle - 了解 Gradle 任务依赖关系 (dependsOn)

grails - Gradle返回任务 'grails-create-controller'找不到

Gradle 'apply from' 导入导致 'No such property' 问题

java - Bintray gradle 插件不上传 Artifact

android - 为 Android 构建应用程序时任务 ':mergeF0F1F2F3DebugResources' 执行失败

java - Gradle jar : order of dependency jars and actual classes

selenium - 出现异常:org.openqa.selenium.WebDriverException:未知错误:net::ERR_NAME_NOT_RESOLVED

css - Assets 管道最小化为CSS header 添加问号

android - 如何制作 "free"和 "paid"风格的应用程序,购买付费版本会删除免费版本