Gradle's DSL configuration closure makes it easier to declare dependencies and repositories. First, you define what libraries your build depends on with the dependencies script. Second, you tell Gradle the origin of these dependencies using the repositories closure.
Other Dependency Management
The Java space is mostly dominated by two projects that support declarative and auto-mated dependency management:
- Apache Ivy: a pure dependency manager that's mostly used with Ant projects
- Maven: which contains a dependency manager as part of its build infrastructure
There are some challenges for dependency management:
- Potential unavailability for centrally hosted repositories:
You can avoid this situation by configuring your own repository. - Bad metadata and missing dependencies:
Metadata is used to declare transitive dependencies for a library. However, neither the metadata nor the repository guarantees that the artifacts declared in the metadata actually exist, are defined correctly, or even needed.
Gradle allows for excluding transitive dependencies and use your own definition instead of metadata.
Dependency Configurations
Java plugin brings in a variety of standard configurations to define which lifecycle should a dependency to be applied to.(For example, dependencies required for compile production source code are added with the compile configuration.)
Firstly, we can have a look at Gradle's Configuration API.
Configuration API Representation
Every project has a ConfigurationContainer which managees configurations. And you can use the configurations provided by the plugin or declared by yourself.
Configurations are very flexible in their behavior. You can define whether the dependencies are transitive, or the resolutionStrategy(such as how to solve the conflict) and so on.
You can also think the configuration is a term of logic group.
Java plugin provides six configurations:
compile, run-time, testCompile, testRuntime, archives, and default.
Define custom configuration
configurations {
cargo {
description = 'Classpath for Ant Cargo Tasks'
visible = false
}
}
You can use configurations.getByName('cargo') to access this cargo configuration.
Declaring Dependencies
There are several dependency types of Gradle:
- External module dependency
- Project dependency
- File dependency
- Client module dependency
- Gradle runtime dependency
Dependency API Representation
Every project has a dependencyHandler, you can access it by getDependencies() method.Each of the dependency types is declared through a method of the dependency handler within the project’s dependencies configuration block. Each dependency is an instance of type Dependency.