This chapter introduces some of the basics of dependency management in Gradle.

7.1. What is dependency management?

Very roughly, dependency management is made up of two pieces. Firstly, Gradle needs to know about the things that your project needs to build or run, in order to find them. We call these incoming files the dependencies of the project. Secondly, Gradle needs to build and upload the things that your project produces. We call these outgoing files the publications of the project. Let's look at these two pieces in more detail:

Most projects are not completely self-contained. They need files built by other projects in order to be compiled or tested and so on. For example, in order to use Hibernate in my project, I need to include some Hibernate jars in the classpath when I compile my source. To run my tests, I might also need to include some additional jars in the test classpath, such as a particular JDBC driver or the Ehcache jars.

These incoming files form the dependencies of the project. Gradle allows you to tell it what the dependencies of your project are, so that it can take care of finding these dependencies, and making them available in your build. The dependencies might need to be downloaded from a remote Maven or Ivy repository, or located in a local directory, or may need to be built by another project in the same multi-project build. We call this process dependency resolution.

Note that this feature provides a major advantage over Ant. With Ant, you only have the ability to specify absolute or relative paths to specific jars to load. With Gradle, you simply declare the “names” of your dependencies, and other layers determine where to get those dependencies from. You can get similar behavior from Ant by adding Apache Ivy, but Gradle does it better.

//注意,这个特性提供了优于ant的重要的优势,使用ant,你只有一个办法就是通过指定绝对或者相对路径来指定要加载的jar包,使用gradle,你只需要简单的声明依赖的名字,或者其他的可以得到依赖的地方。这个可以通过在ant上添加Apache Ivy来实现,但是gradle做的更好

Often, the dependencies of a project will themselves have dependencies. For example, Hibernate core requires several other libraries to be present on the classpath with it runs. So, when Gradle runs the tests for your project, it also needs to find these dependencies and make them available. We call these transitive dependencies.

The main purpose of most projects is to build some files that are to be used outside the project. For example, if your project produces a Java library, you need to build a jar, and maybe a source jar and some documentation, and publish them somewhere.

These outgoing files form the publications of the project. Gradle also takes care of this important work for you. You declare the publications of your project, and Gradle take care of building them and publishing them somewhere. Exactly what “publishing” means depends on what you want to do. You might want to copy the files to a local directory, or upload them to a remote Maven or Ivy repository. Or you might use the files in another project in the same multi-project build. We call this processpublication.

7.2. Declaring your dependencies

Let's look at some dependency declarations. Here's a basic build script:

//下面是一个基本的构建脚本

Example 7.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.+'
}

What's going on here? This build script says a few things about the project. Firstly, it states that Hibernate core 3.6.7.Final is required to compile the project's production source. By implication, Hibernate core and its dependencies are also required at runtime. The build script also states that any junit >= 4.0 is required to compile the project's tests. It also tells Gradle to look in the Maven central repository for any dependencies that are required. The following sections go into the details.

7.3. Dependency configurations

In Gradle dependencies are grouped into configurations. A configuration is simply a named set of dependencies. We will refer to them as dependency configurations.

//gradle中依赖以组的形式配置,一个配置就是一个简单的依赖的名字的集合。我们称它依赖配置

You can use them to declare the external dependencies of your project. As we will see later, they are also used to declare the publications of your project.

//我们可以使用它们声明外部依赖,后面我们也会看到,它们也可以用来声明项目的发布

The Java plugin defines a number of standard configurations. These configurations represent the classpaths that the Java plugin uses. Some are listed below, and you

can find more details in Table 45.5, “Java plugin - dependency configurations”.

//java插件定义了一些标准配置,部分如下

compile

The dependencies required to compile the production source of the project.

//用于编译项目源代码的依赖

runtime

The dependencies required by the production classes at runtime. By default, also includes the compile time dependencies.

//

testCompile

The dependencies required to compile the test source of the project. By default, also includes the compiled production classes and the compile time dependencies.

testRuntime

The dependencies required to run the tests. By default, also includes the compile, runtime and test compile dependencies.

Various plugins add further standard configurations. You can also define your own custom configurations to use in your build. Please see Section 23.3, “Dependency configurations” for the details of defining and customizing dependency configurations.

7.4. External dependencies

There are various types of dependencies that you can declare. One such type is an external dependency. This is a dependency on some files built outside the current build, and stored in a repository of some kind, such as Maven central, or a corporate Maven or Ivy repository, or a directory in the local file system.

To define an external dependency, you add it to a dependency configuration:

//为了定义外部依赖,你需要把它添加到依赖配置中

Example 7.2. Definition of an external dependency

build.gradle

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

An external dependency is identified using groupname and version attributes. Depending on which kind of repository you are using, group and version may be optional.

//外部依赖使用group name和version属性来生命,group和version是可选的

The shortcut form for declaring external dependencies looks like “group:name:version”.

//生命外部依赖的简洁写法为group:name:version

Example 7.3. Shortcut definition of an external dependency

build.gradle

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

To find out more about defining and working with dependencies, have a look at Section 23.4, “How to declare your dependencies”.

7.5. Repositories

How does Gradle find the files for external dependencies? Gradle looks for them in a repository. A repository is really just a collection of files,

organized by groupname andversion. Gradle understands several different repository formats, such as Maven and Ivy, and several different ways of accessing the repository, such as using the local file system or HTTP.

//gradle是如何找到这些外部依赖的呢,gradle会在repository(仓库)中寻找。一个仓库是一个真正存储的文件的存储集合,按照group,name,version的类型组织起来。gradle了解几种不同类型的仓库形式,例如maven,ivy,gradle也了解一些不同的访问仓库的方法,例如使用本地文件系统或者http

By default, Gradle does not define any repositories. You need to define at least one before you can use external dependencies. One option is use the Maven central repository:

//默认情况gradle不会定义任何仓库,你需要在使用外部依赖前至少定义一个。一种选择是使用Maven central 仓库

Example 7.4. Usage of Maven central repository

build.gradle

repositories {
mavenCentral()
}

Or Bintray's JCenter:

Example 7.5. Usage of JCenter repository

build.gradle

repositories {
jcenter()
}

Or a any other remote Maven repository:

Example 7.6. Usage of a remote Maven repository

build.gradle

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

Or a remote Ivy repository:

Example 7.7. Usage of a remote Ivy directory

build.gradle

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

You can also have repositories on the local file system. This works for both Maven and Ivy repositories.

//你也可以使用本地文件系统的仓库,对于maven和ivy都可以这样用。

Example 7.8. Usage of a local Ivy directory

build.gradle

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

A project can have multiple repositories. Gradle will look for a dependency in each repository in the order they are specified, stopping at the first repository that contains the requested module.

//一个项目可以有多个仓库,gradle会按照仓库定义的顺序依次在每个仓库中寻找,找到第一个包含这个请求模块的仓库后就停止寻找。

To find out more about defining and working with repositories, have a look at Section 23.6, “Repositories”.

7.6. Publishing artifacts

Dependency configurations are also used to publish files.[2] We call these files publication artifacts, or usually just artifacts.

//我们称发布的文件为publication artifacts或artifacts

The plugins do a pretty good job of defining the artifacts of a project, so you usually don't need to do anything special to tell Gradle what needs to be published. However, you do need to tell Gradle where to publish the artifacts. You do this by attaching repositories to the uploadArchives task. Here's an example of publishing to a remote Ivy repository:

//通常你不需要做任何指定来告诉gradle要发布什么,但是你需要告诉gradle要把artifacts发布在哪。

Example 7.9. Publishing to an Ivy repository

build.gradle

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

Now, when you run gradle uploadArchives, Gradle will build and upload your Jar. Gradle will also generate and upload an ivy.xml as well.

//当你运行gradle uploadArchives命令时,gradle将会构建和上传jar包。gradle也会生成ivy.xml文件并上传。

You can also publish to Maven repositories. The syntax is slightly different.[3] Note that you also need to apply the Maven plugin in order to publish to a Maven repository. when this is in place, Gradle will generate and upload a pom.xml.

//你也可以发布到maven仓库中,语法稍微不同。maven也会生成pom.xml文件并上传

Example 7.10. Publishing to a Maven repository

build.gradle

apply plugin: 'maven'

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

To find out more about publication, have a look at Chapter 30, Publishing artifacts.

7.7. Where to next?

For all the details of dependency resolution, see Chapter 23, Dependency Management, and for artifact publication see Chapter 30, Publishing artifacts.

If you are interested in the DSL elements mentioned here, have a look at Project.configurations{}Project.repositories{} and Project.dependencies{}.

Otherwise, continue on to some of the other tutorials.

Chapter 7. Dependency Management Basics 依赖管理基础的更多相关文章

  1. 【转载】Gradle学习 第八章:依赖管理基础

    转载地址:http://ask.android-studio.org/?/article/10 This chapter introduces some of the basics of depend ...

  2. Gradle笔记——依赖管理基础

    1. 什么是依赖管理 依赖管理可以分为两部分:一是依赖,即项目构建或运行时所需要的一些文件:二是发布,即构建完成后上传到某个地方. 1.1 依赖 大部分的项目都需要第三方库类或项目文件,这些文件就是项 ...

  3. Gradle系列教程之依赖管理(转)

    转自Lippi-浮生志 :http://ezlippi.com/blog/2015/05/gradle-dependency-management.html 这一章我将介绍Gradle对依赖管理的强大 ...

  4. Gradle实战教程之依赖管理

    这是从我个人网站中复制过来的,原文地址:http://coolshell.info/blog/2015/05/gradle-dependency-management.html,转载请注明出处. 简要 ...

  5. Gradle系列教程之依赖管理

    这一章我将介绍Gradle对依赖管理的强大支持,学习依赖分组和定位不同类型仓库.依赖管理看起来很容易,但是当出现依赖解析冲突时就会很棘手,复杂的依赖关系可能导致构建中依赖一个库的多个版本.Gradle ...

  6. Spring mvc 4系列教程(二)——依赖管理(Dependency Management)和命名规范(Naming Conventions)

    依赖管理(Dependency Management)和命名规范(Naming Conventions) 依赖管理和依赖注入(dependency injection)是有区别的.为了将Spring的 ...

  7. Gradle用户指南(章8:依赖关系管理基础)

    章8:依赖关系管理基础 本章将介绍一些gradle依赖关系管理的基础 什么是依赖关系管理? 简略的说,依赖管理是由两部分组成的.首先,gradle需要知道你要构建或者运行的项目,以便找到它们.我们将这 ...

  8. 着重基础之—构建工具—Maven的依赖管理

    着重基础之—构建工具—Maven的依赖管理 项目构建利器Maven给我们开发人员带来了极大的便利,从繁琐的jar包管理中脱身的程序员终于可以有时间再进入另一个坑了. 我今天要给大家分享的内容是我在实际 ...

  9. 廖雪峰Java12maven基础-1maven入门-2依赖管理

    maven 如果我们的项目依赖第三方的jar包: Commons Logging发布的jar包在那里下载? 使用Log4j需要哪些jar包 其他依赖:junit,Javamail,MySQL驱动... ...

随机推荐

  1. Spring依赖注入的三种方式

    看过几篇关于Spring依赖注入的文章,自己简单总结了一下,大概有三种方式: 1.自动装配 通过配置applicationContext.xml中的标签的default-autowire属性,或者标签 ...

  2. pip install robotframework-sshlibrary提示: Microsoft Visual C++ 9.0 is required

    win7下 pip install robotframework-sshlibrar时提示: error: Microsoft Visual C++ 9.0 is required (Unable t ...

  3. php 带cookie登陆

    <?php /** * @version $id */ define('SCRIPT_ROOT',dirname(__FILE__).'/'); $act = trim($_REQUEST['a ...

  4. 转:如何创建.htaccess文件

    方法1.开始-运行-键入cmd,打开cmd窗口 此时的cmd窗口路径是C:\Documents and Settings\username> 键入以下(不包括括号内信息): copy con . ...

  5. Matlab 符号运算

    root(p):多项式求根.多项式等于0时对应方程的根. 例:,则输入p=[5 4 3 2 1]; root(p) 注:多项式系数都是按幂指数递减形式的. poly([a,b,c]):求已知根为a,b ...

  6. 【HDOJ】1310 Team Rankings

    STL的应用,基本就是模拟题. /* 1410 */ #include <iostream> #include <string> #include <algorithm& ...

  7. centos7安装VLC播放器

    centos7安装VLC播放器 1.安装eple 下载地址:https://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noar ...

  8. 那些SQL语句

    根据book_id,class_id确定老师uid select user_id from lessons left join book on lessons.lesson_id = book.les ...

  9. WordPress ‘crypt_private()’方法远程拒绝服务漏洞

    漏洞名称: WordPress ‘crypt_private()’方法远程拒绝服务漏洞 CNNVD编号: CNNVD-201306-250 发布时间: 2013-06-20 更新时间: 2013-06 ...

  10. 【UIView与控件】