Gradle buildscript dependencies

£可爱£侵袭症+ 提交于 2019-11-26 07:56:09

问题


What is the difference between declaring repositories in the buildScript section of the gradle build or in the root level of the build.

Option 1:

build.gradle:

buildScript {
    repositories {
        mavenCentral();
    }
}

or

build.gradle:

repositories {
    mavenCentral();
}

回答1:


The repositories in the buildScript block are used to fetch the dependencies of your buildScript dependencies. These are the dependencies that are put on the classpath of your build and that you can refer to from your build file. For instance extra plugins that exist on the internet.

The repositories on the root level are used to fetch the dependencies that your project depends on. So all the dependencies you need to compile your project.




回答2:


The build script (i.e. build.gradle) may have some dependencies for the execution of the build script itself. You would enclose those dependencies within the buildScript block. Chapter 4 of Gradle Beyond the Basics describes it in detail.




回答3:


I want to give you clear conception. For this reason, I am attaching build.grade snapshot code for better understanding.

buildscript dependencies:

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'
    }
}

root level/core dependencies:

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'
}

So, First I want to clarify in single word that

i) buildscript dependencies jar file will be downloaded from buildscript repositories.[Project external dependency]

ii) root level dependencies jar file will be downloaded from root level repositories.[For project dependency]

Here,

The “buildscript” block only controls dependencies for the buildscript process itself, not for the application code. As various gradle plugin like gradle-cobertura-plugin, gradle-lint-plugin are found from buildscript repos. Those plugins would not be referenced as dependencies for the application code.

But for project compilation and test running jar files like groovy all jar, junit and testng jar will be found from root level repositories.

And another thing, maven { url("https://plugins.gradle.org/m2/") } portion can be used in both blocks. Because they are used for different dependencies.

Resource Link: Difference between dependencies within buildscript closure and core



来源:https://stackoverflow.com/questions/13923766/gradle-buildscript-dependencies

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!