saga
saga

Reputation: 2113

Difference between depends and dependencies in gradle

What's the difference between depends keyword and dependencies keywords in a gradle build file? I encountered these keywords in the following contexts:

depends:

project(':tools:bnd-module-plugin') {
  description = "bnd plugin to build moduleinfo with ${rootProject.description}"
  sourceSets.main.java.srcDirs += project(':asm').sourceSets.main.java.srcDirs
  depends = ['biz.aQute.bnd:biz.aQute.bnd:3.4.0']
}

dependencies:

subprojects {
  apply plugin: 'com.github.sherter.google-java-format'
  googleJavaFormat.toolVersion = '1.4'
  compileTestJava {
    sourceCompatibility = '1.8'
    targetCompatibility = '1.8'
  }
  dependencies {
    requires.each { projectName -> compile project(projectName) }
    depends.each { artifactName -> compile artifactName }
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.1.0',
        'org.junit.jupiter:junit-jupiter-params:5.1.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.1.0'
    testCompile project(':asm-test')
  }
}

Upvotes: 0

Views: 79

Answers (1)

Lukas Körfer
Lukas Körfer

Reputation: 14493

The dependencies keyword is provided by a method of the Project instance, which the build.gradle file is evaluated against. It allows the configuration of multiple types of dependencies via a DependencyHandler. You can add file dependencies, project dependencies and module dependencies, which can be resolved from Maven and Ivy repositories.

Since it is provided by the Project instance directly, it is available in every Gradle project.

The depends keyword does not exist in the Gradle API. However, Gradle provides a lot of possibilities to extend and program your build scripts. Via so-called extra properties it is possible to define additional properties in the scope of Gradle objects, like tasks and projects. Then these additional properties can be used like regular Gradle properties as in your example.

I found the full build script from your examples online. In the beginning of the script a subprojects closure is used to define extra properties for all subprojects:

subprojects {
    [...]
    ext.provides = []  // The provided java packages, e.g. ['org.objectweb.asm']
    ext.requires = []  // The required Gradle projects, e.g. [':asm-test']
    ext.depends = []   // The external dependencies, e.g. ['junit:junit:4.12']
    [...]
}

provides, required and depends are Groovy lists, which are then later used to store strings.

Your first code example sets a new list to the depends extra property of the :tools:bnd-module-plugin subproject.

Your second uses these lists to add module (depends) or project (requires) dependencies to all subprojects.

As you can see, depends is just part of a custom Gradle script and not part of the official Gradle API, unlike dependencies.

Upvotes: 1

Related Questions