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

我正在学习 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.

https://stackoverflow.com/questions/17416629/

相关文章:

xcode - 如何将构建阶段复制到 Xcode 中的另一个目标?

deployment - 什么/为什么 WinRT 随机创建新的干净布局

android - 在 Android Studio 项目中找不到参数的方法 android()

build - 在另一个项目中调用 gradle "build"任务

c++ - 如何使用 Visual Studio 2017 在 Windows 上构建 OpenSS

linux - 如何使用静态链接构建 git?

git - 您是否应该将已编译的 Assets checkin Git?

android - 带有 Phonegap Build 和 PushPlugin 的 Android

javascript - 减少 React/MERN Stack Bundle 的大小 - 主要是删

build - GulpJS : How to rev images and then update