通俗來講,依賴管理由如下兩部分組成.首先,Gradle需要知道項目構建或運行所需要的一些文件,以便於找到這些需要的文件. 我們稱這些輸入的文件爲項目的依賴.其次,你可能需要構建完成後自動上傳到某個地方.
我們稱這些輸出爲發佈.下面來仔細介紹一下這兩部分:
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 dependenciesof
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:
大部分工程都不太可能完全自給自足,一般你都會用到其他工程的文件. 比如我工程需要Hibernate就得把它的類庫加進來,比如測試的時候可能需要某些額外jar包,例如JDBC驅動或Ehcache之類的Jar包.
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.
這些文件就是工程的依賴.Gradle需要你告訴它工程的依賴是什麼,它們在哪,然後幫你加入構建中. 依賴可能需要去遠程庫下載,比如Maven 或者 Ivy 庫.也可以是本地庫,甚至可能是另一個工程. 我們稱這個過程叫依賴解決
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.
通常,依賴的自身也有依賴.例如,Hibernate 核心類庫就依賴於一些其他的類庫.所以,當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.
大部分工程構建的主要目的是脫離工程使用.例如,生成jar包,包括源代碼、文檔等,然後發佈出去.
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.
這些輸出的文件構成了項目的發佈內容.Gralde也會爲你分擔這些工作.你聲明瞭發佈到到哪,Gradle就會發布到哪. "發佈"的意思就是你想做什麼.比如,複製到某個目錄,上傳到Maven或Ivy倉庫.或者在其它項目裏使用,這些都可以稱之爲 發行.
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 process publication.
來看一下這個腳本里聲明依賴的部分:
Let's look at some dependency declarations. Here's a basic build script:
例 8.1. 聲明依賴 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.+' }
這是毛意思呢?這段腳本是這麼個意思.首先,Hibernate-core.3.6.7.final.jar這貨是編譯期必需的依賴.並且這貨相關的依賴也會一併被加載進來 該段腳本同時還聲明項目測試階段需要 4.0版本以上的Junit.同時告訴Gradle可以去Maven中央倉庫去找這些依賴.下面的章節會進行更詳細的描述.
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.
Gradle中依賴以組的形式來劃分不同的配置.
每個配置都只是一組指定的依賴. 我們稱之爲依賴配置 你也可以藉由此聲明外部依賴.後面我們會瞭解到,這也可用用來聲明項目的發佈
In Gradle dependencies are grouped into configurations.
A configuration is simply a named set of dependencies. We will refer to them as dependency
configurations. 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.
Java插件定義了許多標準配置項.這些配置項形成了插件本身的classpath. 比如下面羅列的這一些.並且你可以從Table 23.5,
“Java 插件 - 依賴配置”瞭解到更多詳細內容.
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 23.5,
“Java plugin - dependency configurations”.
- compile
-
編譯範圍依賴在所有的classpath中可用,同時它們也會被打包
The dependencies required to compile the production source of the project. - runtime
-
runtime依賴在運行和測試系統的時候需要,但在編譯的時候不需要.比如,你可能在編譯的時候只需要JDBC API JAR,而只有在運行的時候才需要JDBC驅動實現
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.
不同的插件提供了不同的標準配置,你甚至也可以定義屬於自己的配置項.具體可查看 章節 50.3,
“依賴配置”
Various plugins add further standard configurations. You can also define your own custom configurations to use in your build. Please see Section 50.3,
“Dependency configurations” for the details of defining and customizing dependency configurations.
依賴的類型有很多種,其中有一種類型稱之爲外部依賴.
這種依賴由外部構建或者在不同的倉庫中,例如Maven中央倉庫或Ivy倉庫中抑或是本地文件系統的某個目錄中.
There are various types of dependencies that you can declare. One such type is an external
dependency. This 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:
例 8.2. 定義外部依賴.
Example 8.2. Definition of an external dependency
build.gradle
dependencies { compile group: 'org.hibernate', name: 'hibernate-core', version: '3.6.7.Final' }
外部依賴包含 group
,name
和version
幾個屬性.
根據選取倉庫的不同, group
和version
也可能是可選的.
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
"
There is a shortcut form for declaring external dependencies, which uses a string of the form "
.group
:name
:version
"
例 8.3. 快速定義外部依賴
Example 8.3. Shortcut definition of an external dependency
build.gradle
dependencies {
compile 'org.hibernate:hibernate-core:3.6.7.Final'
}
更多定義依賴的方式可以參閱 章節 50.4,
“如何聲明依賴”.
To find out more about defining and working with dependencies, have a look at Section 50.4,
“How to declare your dependencies”.
Gradle是在一個被稱之爲倉庫的地方找尋所需的外部依賴.
倉庫即是一個按 group
,name
和version
規則進行存儲的一些文件.Gradle可以支持不同的倉庫存儲格式,如Maven和Ivy,並且還提供多種與倉庫進行通信的方式,如通過本地文件系統或HTTP.
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
and version
.
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沒有定義任何倉庫,你需要在使用外部依賴之前至少定義一個倉庫,例如Maven中央倉庫.
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:
例 8.4. 使用Maven中央倉庫
Example 8.4. Usage of Maven central repository
build.gradle
repositories { mavenCentral() }
或者其它遠程Maven倉庫:
Or a remote Maven repository:
例 8.5. 使用Maven遠程倉庫
Example 8.5. Usage of a remote Maven repository
build.gradle
repositories {
maven {
url "http://repo.mycompany.com/maven2"
}
}
或者採用Ivy遠程倉庫
Or a remote Ivy repository:
例 8.6. 採用Ivy遠程倉庫
Example 8.6. 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.
例 8.7. 採用本地Ivy目錄
Example 8.7. Usage of a local Ivy directory
build.gradle
repositories { ivy { // URL can refer to a local directory url "../local-repo" } }
一個項目可以採用多個庫.Gradle會按照順序從各個庫裏尋找所需的依賴文件,並且一旦找到第一個便停止搜索.
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.
瞭解更多庫相關的信息請參閱章節 50.6,
“倉庫”.
To find out more about defining and working with repositories, have a look at Section 50.6,
“Repositories”.
依賴配置也被用於發佈文件[3] 我們稱之爲打包發佈,或發佈
Dependency configurations are also used to publish files.[3] We
call these files publication
artifacts, or usually just artifacts.
插件對於打包提供了完美的支持,所以通常而言無需特別告訴Gradle需要做什麼.但是你需要告訴Gradle發佈到哪裏. 這就需要在 uploadArchives
任務中添加一個倉庫.下面的例子是如何發佈到遠程Ivy倉庫的:
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:
例 8.8. 發佈到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
一起上傳到目標倉庫.
Now, when you run gradle
uploadArchives
, Gradle will build and upload your Jar. Gradle will also generate and upload an ivy.xml
as
well.
當然你也可以發佈到Maven倉庫中.語法只需稍微一換就可以了.[4]
p.s:發佈到Maven倉庫你需要Maven插件的支持,當然,Gradle也會同時產生 pom.xml
一起上傳到目標倉庫.
You can also publish to Maven repositories. The syntax is slightly different.[4] Note
that you also need to apply the Maven plugin in order to publish to a Maven repository. In this instance, Gradle will generate and upload a pom.xml
.
例 8.9. 發佈到Maven倉庫
Example 8.9. Publishing to a Maven repository
build.gradle
apply plugin: 'maven' uploadArchives { repositories { mavenDeployer { repository(url: "file://localhost/tmp/myRepo/") } } }
瞭解更多有關發佈的內容,請參閱第 51 章, 內容發佈
artifacts.
To find out more about publication, have a look at Chapter 51, Publishing
artifacts.
瞭解更多有關依賴的問題,請參閱第 50 章, 依賴管理,
瞭解更多有關發佈的內容,請參閱第 51 章, 內容發佈
artifacts.
For all the details of dependency resolution, see Chapter 50, Dependency
Management, and for artifact publication see Chapter 51, Publishing
artifacts.
若對DSL感興趣 ,請看Project.configurations{}
, Project.repositories{}
and Project.dependencies{}
.
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.