Gradle Dependencies

2017-01-11  本文已影响0人  Luna_Lu

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:

There are some challenges for dependency management:

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:

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.

上一篇 下一篇

猜你喜欢

热点阅读