gradle - build.gradle buildscript 依赖与依赖?

标签 gradle dependencies classpath build-script

有人可以向我解释 build.gradle 文件中“buildscript”中列出的依赖项与依赖项 block {} 中列出的常规依赖项有何不同?以及为什么必须使用语法“实现”列出它们?我已经用谷歌搜索了这个并且回复说构建脚本中的依赖关系并用于“构建项目”但我不明白这个?谁能给个更清楚的图和答案?

构建脚本:

buildscript
        {
            repositories
                    {
                        maven {
                            url 'myMavenFeed'
                            credentials {
                                username "myUsername"
                                password myPassword
                            }
                        }
                        mavenCentral()
                        jcenter()
                    }
            dependencies
                    {
                        classpath "com.microsoft.azure.sdk.iot:iot-device-client:1.14.1"
                    }
        }

依赖 block :

dependencies
        {
            compile group: 'com.microsoft.azure.sdk.iot', name: 'iot-device-client', version: '1.16.0'
}

最佳答案

Can someone explain to me how depedencies listed in the "buildscript" in the build.gradle file are different than regular dependencies listed in the dependencies block { } ?

buildscript { } block 中定义的依赖项是用于构建项目的依赖项。这些依赖项可用于您的 Gradle 构建文件(build.gradlebuild.gradle.kts)

dependencies{} 中定义的依赖项用于您的应用程序代码。

因此,对于您问题中的示例,Gradle(构建系统)在其类路径中包含 iot-device-client 是否有意义?为什么构建系统需要在其类路径上使用 iot-device-client 来构建您的项目?它没有意义,因此应将其删除。

现在假设您正在开发一个应用程序,它需要来自 iot-device-client 的某些功能或类。您需要一种方法将此库添加到应用程序的代码/类路径中。然后像上面那样将其声明为依赖项:

dependencies {
    implementation("com.microsoft.azure.sdk.iot:iot-device-client:1.16.0")
}

引用资料:

and why they have to be listed with the syntax "implementation"?

实现 被称为 configuration : Configuration 表示一组工件及其依赖项

根据您应用于项目的插件,还有更多配置。例如,如果您应用 Java plugin :

plugins {
    id("java")
}

以下配置可供使用:

  • 实现
  • 只编译
  • 编译类路径
  • ...还有更多

每个都有自己的含义/用法,我强烈建议阅读它们 here .

关于gradle - build.gradle buildscript 依赖与依赖?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60967940/

相关文章:

gradle - 如何使用不同的参数多次运行 Spock 规范?

java - 编译前更改版本常量

angularjs - 在 AngularJS 模块依赖项中包含全局配置

java - 如何从 Cygwin bash shell 启动在 Eclipse 中开发的 java CLI 程序?

clojure - 在当前 .jar 文件中获取 Clojure 命名空间

java - 从 tomcat 访问 jar 文件

android - react native 运行 android 失败

eclipse - 如何让 Eclipse 显示 groovy 类的红色编译错误?

android - Android Studio 中的多个项目

java - 如何在 Maven 中指定某些后缀文件之间的依赖规则?