ChatGPT解决这个技术问题 Extra ChatGPT

Gradle buildscript 依赖项

在 gradle 构建的 buildscript 部分或构建的根级别声明存储库有什么区别。

buildscript {
    repositories {
        mavenCentral();
    }
}

相对

repositories {
    mavenCentral();
}

D
Dave Jarvis

buildscript 块中的存储库用于获取 buildscript 依赖项的依赖项。这些是放在构建的类路径中的依赖项,您可以从构建文件中引用它们。例如互联网上存在的额外插件。

根级别的存储库用于获取项目所依赖的依赖项。因此,编译项目所需的所有依赖项。


如果我的 buildscript 和我的项目都需要 maven Central,我需要声明两次吗?
是的,您确实需要指定两次。
例如,除了通常的“compile”和“testCompile”关键字之外,可以将 Spring propdeps 插件添加到 buildscript 以启用依赖项的“provided”和“optional”关键字。注意:war 插件已经提供了 "provided" 关键字,你只需要 propdeps 用于将部署在 war 中的 jar 项目。
S
SkyWalker

我想给你一个清晰的概念。出于这个原因,我附上了 build.grade 快照代码以便更好地理解。

构建脚本依赖项:

buildscript {
    repositories {
        maven { url("https://plugins.gradle.org/m2/") }
    }

    dependencies {
        classpath 'net.saliman:gradle-cobertura-plugin:2.3.2'
        classpath 'com.netflix.nebula:gradle-lint-plugin:latest.release'
    }
}

根级别/核心依赖项:

repositories{
    mavenLocal()
    maven { url("https://plugins.gradle.org/m2/") }
    maven { url "https://repo.spring.io/snapshot" }
}

dependencies {
        //Groovy
        compile group: 'org.codehaus.groovy', name: 'groovy-all', version: '2.3.10'

        //Spock Test
        compile group: 'org.spockframework', name: 'spock-core', version: '1.0-groovy-2.3'

        //Test
        testCompile group: 'junit', name: 'junit', version: '4.10'
        testCompile group: 'org.testng', name: 'testng', version: '6.8.5'
}

所以,首先我想用一个词来澄清

i) buildscript 依赖项 jar 文件将从 buildscript 存储库下载。[项目外部依赖项] ii) 根级依赖项 jar 文件将从根级存储库下载。[对于项目依赖项]

这里,

“buildscript” 块只控制 buildscript 进程本身的依赖关系,而不是应用程序代码。作为各种 gradle 插件,如 gradle-cobertura-plugingradle-lint-plugin 可以从 buildscript repos 中找到。这些插件不会被引用为应用程序代码的依赖项。

但是对于项目编译和测试运行的 jar 文件,如 groovy all jar, junit and testng jar 将从根级存储库中找到。

另外一点maven { url("https://plugins.gradle.org/m2/") } 部分可以在两个块中使用。因为它们用于不同的依赖项。

资源链接: Difference between dependencies within buildscript closure and core