Gradle Dependency Management

当我们开发代码时,我们通常使用第三方或开源库。这些库需要在编译器【compiler】的类路径【classpath】中可用,否则我们将得到错误,构建将失败。Gradle 支持依赖管理【dependency management】,所以我们可以在构建文件【build file】中定义我们的依赖。然后,Gradle 将负责各种任务的必要配置。

在本章中,我们将讨论如何在我们的构建中使用依赖管理【dependency management】。我们将看到如何通过配置来组织依赖关系。我们还将讨论托管依赖项工件【host dependency artifacts】的存储库【repositories】、它们的依赖项以及如何处理不同的存储库布局。

然后,我们将使用 Gradle 语法为带有版本信息的模块定义依赖关系。

Dependency configuration

Java 没有真正支持将版本化库作为依赖项使用。例如,我们无法在 Java 中表示类是依赖于 lib-1.0.jar 还是 lib- 2.0.jar。有一些开源解决方案可以处理依赖关系,并允许我们表示我们的 Java 代码是依赖于 lib- 1.0.jar 还是 lib-2.0.jar。最流行的是 Maven 和 Apache Ivy。Maven是一个完备的构建工具,且具有依赖管理机制。而 Ivy 只关心依赖管理。

这两个工具都支持存储库【repositories】,其中版本库【versioned libraries】与这些库的元数据一起存储。库可以依赖于其他库,并在库的元数据中进行描述。元数据在描述符 XML 文件【descriptor XML files】中描述。Ivy 完全支持 Maven 描述符文件和存储库【repositories】;它还增加了一些额外的功能。因此,使用Ivy,您可以得到Maven 所需要的东西,甚至更多。这就是为什么 Gradle 在底层使用 Ivy API 来执行依赖管理的原因。Gradle还在 Ivy 基础上添加了一些额外的功能,因此我们可以以一种非常灵活的方式定义和使用依赖关系。

在 Gradle 构建文件中,我们将依赖项【dependencies】组合在一个配置【configuration】中。配置有名称,配置可以相互扩展。通过配置,我们可以对依赖项进行逻辑分组。例如,我们可以创建一个 javaCompile 配置来包含编译 Java 代码所需的依赖项。我们可以向构建中添加任意多的配置。我们不直接在配置中定义依赖关系。与标签一样,配置可以在定义依赖关系时使用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Learn how to use Gradle's powerful dependency management through extensive code samples, and discover how to define, customize, and deploy dependencies About This Book Be in total control of your dependencies Deploy your artifacts to repositories with Gradle Learn through code snippets and real-life examples Who This Book Is For If you work on Java projects, use Gradle as a build automation tool, and you use dependencies in your project, this is the book for you. Additionally, if you want to deploy your project artifacts as dependencies for other developers using Gradle, you've found the right book. In Detail Gradle is the next generation in build automation. It allows you to define dependencies for your project in a clear way and also customize how they are resolved to suit your needs. It offers fine-grained control over how to publish your artifacts to Maven and Ivy repositories. Gradle Dependency Management defines dependencies for your Java-based project and customizes how they are resolved. You will learn how to configure the publication of artifacts to different repositories. Packed with plenty of code samples, you will understand how to define the repositories that contain dependencies. Following this, you will learn how to customize the dependency resolution process in Gradle. Table of Contents Chapter 1. Defining Dependencies Chapter 2. Working with Repositories Chapter 3. Resolving Dependencies Chapter 4. Publishing Artifacts Chapter 5. Publishing to a Maven Repository Chapter 6. Publishing to Bintray Chapter 7. Publishing to an Ivy Repository
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值