Search code examples
gradledependenciesclasspathbuild-script

build.gradle buildscript dependencies vs. dependencies?


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 { } ? and why they have to be listed with the syntax "implementation"? I've googled this and responses say the dependencies in the buildscript and used to "build the project" but I don't understand this? can anyone give a more clear picture and answer?

buildscript:

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

Dependencies block:

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

Solution

  • 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 { } ?

    Dependencies defined in the buildscript { } block are dependencies to use to build your project. These dependencies are available to use in your Gradle build file (build.gradle or build.gradle.kts)

    Dependencies defined in the dependencies { } are for your application code.

    So for your samples in your questions, does it make sense for Gradle (the build system) to have iot-device-client on its classpath? Why does a build system need iot-device-client on its classpath to build your project? It doesn't make sense therefore it should be removed.

    Now let's say you are developing an application the requires some functionality or class from iot-device-client. You need a way to add this library to your application's code/classpath. You when then declare it as a dependency as you have done above:

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

    References:

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

    implementation is known as a configuration: A Configuration represents a group of artifacts and their dependencies

    There are many more configurations depending on the plugins you apply to your project. For example, if you apply the Java plugin:

    plugins {
        id("java")
    }
    

    The following configurations are available to use:

    • implementation
    • compileOnly
    • compileClasspath
    • ...and many more

    Each one has their own meaning/usage and I strongly suggest reading about them here.