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 {
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]&nbs