翻译:Gradle之依赖管理

 

原文地址 http://www.gradle.org/docs/current/userguide/artifact_dependencies_tutorial.html

 

8.1. What is dependency management?何谓?为何?

依赖管理大致有两块:首先Gradle需要找到你工程需要的东西,这些东西就是所谓的“依赖”。另外Gradle需要构建和上传你工程的产出,这就是所谓的发行。让我们来仔细看看它们:

大部分工程都不太可能完全自给自足,一把你都会用到其他工程的文件。比如我工程需要Hibernate就得把它的类库加进来,比如测试的时候可能需要某些额外jar包(jdbc驱动或者Ehcache jar包)。这些文件就是工程的依赖。Gradle需要你告诉它工程的依赖是什么,它们在哪,然后帮你加入构建中。依赖可能需要去远程库下载,比如Maven 或者 Ivy 库。也可以是本地库,甚至可能是另一个工程。我们称这个过程叫“依赖解决”(dependency resolution).

通常情况下,依赖本身也有依赖。比如Hibernate核心库就依赖一些其他类库。当Gradle测试你工程的时候,它也需要找到这些间接依赖。我们称之为“传递依赖”( transitive dependencies).

大部分工程构建的主要目的是脱离工程使用。比如生成jar包,包括源代码、文档等,然后发布出去。这些个玩意构成了项目的发行内容。Gradle也会替你分忧这个工作。你声明了要发行,Gradle会构建并且发行。你可能会把文件拷贝到本地某处,或者上传到远程 Maven 或 Ivy 库,甚至在其他项目里使用。整个这个都是所谓的发行。

8.2. Declaring your dependencies依赖声明

我们看一下简单的依赖声明脚本:

Example 8.1. Declaring dependencies

build.gradle

apply plugin: 'java'

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'org.hibernate', name: 'hibernate-core', version: '3.6.7.Final'
    testCompile group: 'junit', name: 'junit', version: '4.+'
}

这是在搞毛?这段脚本说的是:1,工程在编译时需要Hibernate core 3.6.7.Final。这样Hibernate核心库和它的依赖库都会引入。2,测试的时候需要4.0以上版本的junit。另外就是这些包都要从 Maven central 库里查找。接下来咱们详细看下。

8.3. Dependency configurations依赖配置

Gradle的依赖会分组为“配置”(configurations) 。一个配置就是一套具名依赖,称为“依赖配置”。你可以声明外部依赖,后面我们会看到。

Java插件定义了一些标准配置,形成了插件本身的类路径库。下面列一下,你可以自己去这看:Table 23.5, “Java plugin - dependency configurations”.

compile

该依赖对于编译发行是必须的。

runtime

该依赖对于运行时是必须的,默认包含编译时依赖。

testCompile

该依赖对于编译测试是必须的,默认包含编译产品依赖和编译时依赖。

testRuntime

该依赖对于测试是必须的,默认包含编译、运行时、测试编译依赖。

定制化配置请看: Section 50.3, “Dependency configurations”

8.4. External dependencies外部依赖

依赖类型有多种,现在说一种外部依赖(external dependency)。这种依赖的对象不在工程内,甚至是在远程库里。

定义外部依赖需要如下加入到依赖配置:

Example 8.2. Definition of an external dependency

build.gradle

dependencies {
    compile group: 'org.hibernate', name: 'hibernate-core', version: '3.6.7.Final'
}

外部依赖使用groupname 和version 属性。根据使用库的不同,group 和version可能是可选的。有一种快捷方式声明依赖:

Example 8.3. Shortcut definition of an external dependency

build.gradle

dependencies {
    compile 'org.hibernate:hibernate-core:3.6.7.Final'
}

更多信息请看 Section 50.4, “How to declare your dependencies”.

8.5. Repositories库

Gradle是咋定位外部依赖呢?答案是“库”(repository)。一个库其实就是一大堆文件,不过是用groupname 和version组织好的。Gradle支持很多库,比如 Maven 和Ivy;还只存多种方式连接到库,比如本地系统或者 HTTP.

Gradle默认没定义库。你使用前需要至少定义一个,比如Maven central 库:

Example 8.4. Usage of Maven central repository

build.gradle

repositories {
    mavenCentral()
}

或者远程Maven 库:

Example 8.5. Usage of a remote Maven repository

build.gradle

repositories {
    maven {
        url "http://repo.mycompany.com/maven2"
    }
}

或者远程 Ivy 库:

Example 8.6. Usage of a remote Ivy directory

build.gradle

repositories {
    ivy {
        url "http://repo.mycompany.com/repo"
    }
}

你可以自己建个库然后指定:

Example 8.7. Usage of a local Ivy directory

build.gradle

repositories {
    ivy {
        // URL can refer to a local directory
        url "../local-repo"
    }
}

工程里可以制定多个库,Gradle会依序查找,一旦找到就停止。更多信息请看 Section 50.6, “Repositories”.

 

8.6. Publishing artifacts发布文件

依赖配置也用来发布文件,这些文件称为“发布文件”(publication artifacts),或干脆就叫“发件”(artifacts)(不会翻译,谁来纠正一下。。)

一般的,插件会做很多发件定义工作,所以我们不用干什么。不过需要告诉Gradle发件要发布到哪里。方法是把库附在 uploadArchives 任务里。比如我们要发布到远程Ivy库:

Example 8.8. Publishing to an Ivy repository

build.gradle

uploadArchives {
    repositories {
        ivy {
            credentials {
                username "username"
                password "pw"
            }
            url "http://repo.mycompany.com"
        }
    }
}

现在你执行 gradle uploadArchives, Gradle就会构建并上次你的Jar包,同时生成并上传一个ivy.xml 文件.

可以发布到Maven 库,语法有一点点不同。记住要使用Maven插件不然发布不过去。这样Gradle会生成并上传pom.xml文件。

Example 8.9. Publishing to a Maven repository

build.gradle

apply plugin: 'maven'

uploadArchives {
    repositories {
        mavenDeployer {
            repository(url: "file://localhost/tmp/myRepo/")
        }
    }
}

更多关于发布的信息请看 Chapter 51, Publishing artifacts.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
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
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值