gpt4 book ai didi

build - 为什么 Gradle 不在编译/运行时类路径中包含传递依赖项?

转载 作者:IT老高 更新时间:2023-10-28 11:21:46 25 4
gpt4 key购买 nike

我正在学习 Gradle 的工作原理,但我不明白它如何解决项目传递依赖关系。

目前,我有两个项目:

  • projectA : 对外部库有几个依赖项
  • projectB : 对 projectA 只有一个依赖项

无论我如何尝试,当我构建 projectB 时,gradle 不会在 projectB 的编译或运行时类路径中包含任何 projectA 依赖项(X 和 Y)。我只是通过在 projectB 的构建脚本中包含 projectA 的依赖项来使其工作,在我看来这没有任何意义。这些依赖项应自动附加到 projectB。我很确定我错过了什么,但我不知道是什么。

我读过“lib 依赖项”,但它似乎只适用于描述的本地项目 here ,而不是外部依赖项。

这是我在根项目(包含 projectA 和 projectB 的项目)中使用的 build.gradle:

buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.3'
}
}

subprojects {
apply plugin: 'java'
apply plugin: 'idea'

group = 'com.company'

repositories {
mavenCentral()
add(new org.apache.ivy.plugins.resolver.SshResolver()) {
name = 'customRepo'
addIvyPattern "ssh://.../repository/[organization]/[module]/[revision]/[module].xml"
addArtifactPattern "ssh://.../[organization]/[module]/[revision]/[module](-[classifier]).[ext]"
}
}

sourceSets {
main {
java {
srcDir 'src/'
}
}
}

idea.module { downloadSources = true }

// task that create sources jar
task sourceJar(type: Jar) {
from sourceSets.main.java
classifier 'sources'
}

// Publishing configuration
uploadArchives {
repositories {
add project.repositories.customRepo
}
}

artifacts {
archives(sourceJar) {
name "$name-sources"
type 'source'
builtBy sourceJar
}
}
}

这仅涉及项目A:

version = '1.0'
dependencies {
compile 'com.company:X:1.0'
compile 'com.company:B:1.0'
}

这是项目B使用的:

version = '1.0'
dependencies {
compile ('com.company:projectA:1.0') {
transitive = true
}
}

提前感谢您的帮助,请为我的英语不好向我道歉。

最佳答案

我知道这个特定版本的问题已经解决了,但是我的搜索把我带到了这里,我希望我可以为一些人省去解决这个问题的麻烦。

错误的foo/build.gradle

dependencies {
implementation 'com.example:widget:1.0.0'
}

foo/build.gradle

dependencies {
api 'com.example:widget:1.0.0'
}

bar/build.gradle

dependencies {
implementation project(path: ':foo')
}

implementation 隐藏小部件依赖。

api 使小部件依赖传递。


来自 https://stackoverflow.com/a/44493379/68086 :

From the Gradle documentation:

dependencies {
api 'commons-httpclient:commons-httpclient:3.1'
implementation 'org.apache.commons:commons-lang3:3.5'
}

Dependencies appearing in the api configurations will be transitively exposed to consumers of the library, and as such will appear on the compile classpath of consumers.

Dependencies found in the implementation configuration will, on the other hand, not be exposed to consumers, and therefore not leak into the consumers' compile classpath. This comes with several benefits:

  • dependencies do not leak into the compile classpath of consumers anymore, so you will never accidentally depend on a transitive dependency
  • faster compilation thanks to reduced classpath size
  • less recompilations when implementation dependencies change: consumers would not need to be recompiled
  • cleaner publishing: when used in conjunction with the new maven-publish plugin, Java libraries produce POM files that distinguish exactly between what is required to compile against the library and what is required to use the library at runtime (in other words, don't mix what is needed to compile the library itself and what is needed to compile against the library).

The compile configuration still exists, but should not be used as it will not offer the guarantees that the api and implementation configurations provide.

关于build - 为什么 Gradle 不在编译/运行时类路径中包含传递依赖项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17416629/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com