0

This is working gradle build script code:

dependencies {
    compile files("dir/libs", "dir/libs2")
    compile fileTree(dir: "libs", include: "*.jar")
}

As far as I understand it,

dependencies is a method that takes a closure, sets it's delegate property to DependencyHandler object and execute it.

compile is a Configuration object belonging to project

But what are files and fileTree?

Jerry Lundegaard
  • 366
  • 3
  • 16
  • 1
    https://docs.gradle.org/current/javadoc/org/gradle/api/Project.html#fileTree-java.util.Map-, https://docs.gradle.org/current/javadoc/org/gradle/api/Project.html#files-java.lang.Object...- – JB Nizet Nov 20 '18 at 22:52
  • additional link: see Gradle Project DSL documentation here : https://docs.gradle.org/current/dsl/org.gradle.api.Project.html . – M.Ricciuti Nov 20 '18 at 23:00
  • Buy how is the collection -- (ConfigurableFileCollection) --, that is returned by files, applied to compile object? It does't look like an assignment nor method call. – Jerry Lundegaard Nov 21 '18 at 08:35

1 Answers1

1

The first thing to know is that Gradle uses AST for the dependecies closure syntax this answer here does a good job of explaining what AST is.

I'll talk about the above code snippet, you are right about the block of code being delegated to the DependencyHandler, the line is defined here as

To declare a specific dependency for a configuration you can use the following syntax:

dependencies {
     configurationName dependencyNotation1, dependencyNotation2, ...
 }

The important thing to note here is the above code is syntax provided by Gradle to make code clean and terse. You could as well use the method DependencyHandler.add(String,Object) For example

dependencies {
          add('compile', fileTree(dir: "libs", include: "*.jar"))
  }

As compared to the above syntax, the top syntax allows you to add multiple dependencies in a single statement and is a lot more cleaner.

fileTree(), files() etc are methods defined in Project. From DependencyHandler.add(String,Object), the second argument is of type Object, as such the method as well as the dependency syntax can accept a number of type of dependencies like ProjectDependency, other Configurations and FileCollection and its sub types as you mentioned the ConfigurableFileCollection.

Community
  • 1
  • 1
Ryotsu
  • 786
  • 6
  • 16