【问题标题】:gradle dependencies: what "classpath" keyword means exactly and where it is documented?gradle 依赖项:“classpath”关键字的确切含义和记录在哪里?
【发布时间】:2020-05-21 06:32:11
【问题描述】:

我已经对“gradle classpath site:stackoverflow.com”和“gradle classpath”进行了网络搜索,并且仅在以下位置找到了相关信息:
Gradle: What is the difference between classpath and compile dependencies?

余腾宝的回答是这样写的:

compile 'org.hibernate:hibernate-core:5.0.5.Final' 是一个模块 依赖声明。编译配置(现在是 被实现配置弃用。)只是一个关键字 仅用于实现依赖项。不是关键字描述 它是哪种类型的依赖项(按类型在这里我遵循三个 教程中定义的类型,即模块、文件和项目。)

据我了解,classpath 也是关键字。我试图在 gradle docs 中找到它的含义: https://docs.gradle.org/current/userguide/declaring_dependencies.html

以及其中引用的其他一些内容:
https://docs.gradle.org/current/userguide/dependency_management_for_java_projects.html https://docs.gradle.org/current/userguide/java_library_plugin.html https://docs.gradle.org/current/dsl/org.gradle.api.artifacts.Configuration.html https://docs.gradle.org/current/userguide/variant_model.html https://docs.gradle.org/current/userguide/java_plugin.html

还有: https://docs.gradle.org/current/javadoc/org/gradle/api/initialization/dsl/ScriptHandler.html https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/Classpath.html https://docs.gradle.org/current/userguide/organizing_gradle_projects.html

提到了“compileClasspath”。如果 classpath 关键字仅被弃用为 compile 一个,为什么文档中没有它?

附:我的意思是:

buildscript {
    repositories {
        jcenter()
        maven {
            url 'https://maven.google.com/'
            name 'Google'
        }
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.4.1'
   }
}

回答后添加:

在 gradle 源代码中搜索 getBuildscript():

【问题讨论】:

    标签: java gradle


    【解决方案1】:

    据我了解,classpath 也是关键字

    这是不正确的。 classpathcompileimplementationapi,还有更多都是configurations。 Gradle 中的配置是树状的意思,通常每个都从某个父级扩展而来:

    来源:https://docs.gradle.org/current/userguide/java_library_plugin.html#sec:java_library_configurations_graph

    例如,使用implementation 配置,当您在build.gradle 中执行以下操作时:

    dependencies {
        implementation("org.apache.commons:commons-lang3:3.9")
    }
    

    你实际上在做:

    project.getDependencies().add("implementation", "org.apache.commons:commons-lang3:3.9")
    

    前者使用Gradle DSL,后者直接使用Gradle API。

    如果 classpath 关键字仅被弃用为 compile 之一,为什么文档中没有它?

    compile 配置 已弃用,如 here 所述。 classpath 配置仍在使用,但通常仅与 buildscrpt { } 块一起使用。

    所以对于你的例子:

    buildscript {
        dependencies {
            classpath("com.android.tools.build:gradle:3.4.1")
        }
    }
    

    脱糖:

    project.getBuildscript().getDependencies().add("classpath", "com.android.tools.build:gradle:3.4.1")
    

    【讨论】:

    • 谢谢!我正在尝试在 gradle 源代码中找到您的最后一行 - 请参阅我的问题的更新结尾(来自 IDEA 的图片)。在图片上看到那条线的位置吗?还是我应该进行不同的搜索?
    • 搜索getBuildscript().getDependencies() 什么都不返回,getDependencies() 返回 100 多个条目,所以我希望从 getBuildscript() 条目中找到有问题的代码。
    • 你不会找到我在上面添加的 1:1 的 sn-ps。 Gradle 是高度抽象的。找到这些行的最好办法是编写一个插件来执行上述操作并测试/调试以跟踪代码路径。
    • 我也非常感谢您对 stackoverflow.com/questions/60070739/… 的帮助,请在末尾添加 2。这个问题是在我开始深入研究并写下这个问题之前。附:在您回复评论之前,我在上面写了。我猜答案(至少 ADDED 2)会是一样的吗?
    猜你喜欢
    • 2018-03-31
    • 1970-01-01
    • 1970-01-01
    • 2017-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-12
    • 2015-12-13
    相关资源
    最近更新 更多