Chapter 7. Dependency Management Basics 依赖管理基础
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 group
, name
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 group
, name
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 依赖管理基础的更多相关文章
- 【转载】Gradle学习 第八章:依赖管理基础
转载地址:http://ask.android-studio.org/?/article/10 This chapter introduces some of the basics of depend ...
- Gradle笔记——依赖管理基础
1. 什么是依赖管理 依赖管理可以分为两部分:一是依赖,即项目构建或运行时所需要的一些文件:二是发布,即构建完成后上传到某个地方. 1.1 依赖 大部分的项目都需要第三方库类或项目文件,这些文件就是项 ...
- Gradle系列教程之依赖管理(转)
转自Lippi-浮生志 :http://ezlippi.com/blog/2015/05/gradle-dependency-management.html 这一章我将介绍Gradle对依赖管理的强大 ...
- Gradle实战教程之依赖管理
这是从我个人网站中复制过来的,原文地址:http://coolshell.info/blog/2015/05/gradle-dependency-management.html,转载请注明出处. 简要 ...
- Gradle系列教程之依赖管理
这一章我将介绍Gradle对依赖管理的强大支持,学习依赖分组和定位不同类型仓库.依赖管理看起来很容易,但是当出现依赖解析冲突时就会很棘手,复杂的依赖关系可能导致构建中依赖一个库的多个版本.Gradle ...
- Spring mvc 4系列教程(二)——依赖管理(Dependency Management)和命名规范(Naming Conventions)
依赖管理(Dependency Management)和命名规范(Naming Conventions) 依赖管理和依赖注入(dependency injection)是有区别的.为了将Spring的 ...
- Gradle用户指南(章8:依赖关系管理基础)
章8:依赖关系管理基础 本章将介绍一些gradle依赖关系管理的基础 什么是依赖关系管理? 简略的说,依赖管理是由两部分组成的.首先,gradle需要知道你要构建或者运行的项目,以便找到它们.我们将这 ...
- 着重基础之—构建工具—Maven的依赖管理
着重基础之—构建工具—Maven的依赖管理 项目构建利器Maven给我们开发人员带来了极大的便利,从繁琐的jar包管理中脱身的程序员终于可以有时间再进入另一个坑了. 我今天要给大家分享的内容是我在实际 ...
- 廖雪峰Java12maven基础-1maven入门-2依赖管理
maven 如果我们的项目依赖第三方的jar包: Commons Logging发布的jar包在那里下载? 使用Log4j需要哪些jar包 其他依赖:junit,Javamail,MySQL驱动... ...
随机推荐
- SCXML和QScxml使用总结
最近接触了SCXML这个状态描述文本,简单来讲就是描述了整个状态的变迁过程的一种XML格式的表格.Qt labs中有一个项目就是QScxml,它基于QStateMachine上层制作,可以直接读取SC ...
- winform批量查询单号剔除重复
//查询分单函数 private string GetQueryInSubbillNo() { string strSubbillNO = " ...
- java-二分查找法
package search; public class BinarySearch { public static void main(String[] args) { , , , , , , , , ...
- python模块中的特殊变量
37.模块的特殊变量: 显示模块中的变量 import s1 print(vars(s1)) 1.__doc__:打印注释信息. #!/usr/bin/env python # _ ...
- c程序代码的内存布局(学好C的基础)
一个程序本质上都是由 BSS 段.data段.text段三个组成的.这样的概念在当前的计算机程序设计中是很重要的一个基本概念,而且在嵌入式系统的设计中也非常重要,牵涉到嵌入式系统运行时的内存大小分配, ...
- MVC中的模型注解
authour: chenboyi updatetime: 2015-04-26 21:28:42 friendly link: 目录: 1,思维导图 2,内容解析 3,CodeSimple 1, ...
- Web Service相关工具的配置
近期在学习Web Service Testing,使用到了soapUI这个工具,但是在学习之前,需要搭建Web Service环境,其中有关数据库的连接问题花费了我好多时间,主要还是自己对于很多配置不 ...
- 重构后的程序:通过rsync命令抓取日志文件
push.sh #!/bin/bash function push() { local ip=$ local user=$ local password=$ local path=$ local lo ...
- Redis系列(1)之安装
Redis系列(1)之安装 由于项目的需要,最近需要研究下Redis.Redis是个很轻量级的NoSql内存数据库,它有多轻量级的呢,用C写的,源码只有3万行,空的数据库只占1M内存.它的功能很丰富, ...
- golang protobuf
1. proto文件编写的时候,如果用uint32或uint64类型,那么不能用required,必须用optional. 如果用错了,会出现错误:unmarshaling error: proto: ...