This article is the author original, reproduced please indicate the source: http://www.cnblogs.com/gzdaijie/p/5296624.html
Table of Contents
1. Adding a dependency package name
1.1 Dependency Types
1.2 Declaration dependent
1.3 Adding Java Dependencies
1.4 Finding a dependent package name
1.5 Complete examples
2. Adding a dependent warehouse
3. Dependency FAQs
3.1 Dependency transitivity
3.2 Version Conflicts
3.3 Dynamic Dependencies
3.4 More Settings
Development of any software, how to manage dependencies is a bypass, software development process, we tend to use such a third-party library, this time, a good dependency on management is particularly important. As an automated build, Gradle is well-supported for dependency management.
Usually we use the IDE (Eclipse, Idea, Android Studio) To develop Java projects, the IDE automatically created a gradle file for us, adding a few lines of dependency, but also simple line of code, this essay will gradually explain the Gradle dependency management method, We hope to help you.
If you have any mistake, please don't hesitate to point out, thank you very much! If this article is helpful to you, click on the lower right corner of the recommendation Bar ~
1. Add a dependent Package name 1.1 dependency type
1.2 Declaration dependent
- The declaration relies on using the following closures
?
123 |
dependencies {
<configuration name> <dependencies> } |
1.3 Adding Java Dependencies
Here, we build Java projects as an example, to build a Java project first need to apply java插件
, plug-in use can refer to the previous essay Java Gradle plug-in management
Java plug-in for different operations, the dependency is divided into 10 classes (see Java plugin 45.5), the following describes the common 5 classes
(1)
compile: Source code (SRC/MAIN/JAVA) compile-time dependency, most commonly used
(2)
runtime: Source code (Src/main/java) dependent upon execution
(3)
testcompile: Test Code (src/main/test) dependency at compile time
(4)
Testruntime: Dependency of Test Code (SRC/MAIN/JAVA) execution
(5) Archives: Dependencies in Project packaging (E.g.jar)
Typically, a jar dependency needs to include the JAR filegroup (group/namespace), the jar file name (name), the jar version (versions), and, in special cases, the JDK version. Adding a dependency can be in the following ways:
?
123456789101112131415 |
/* 单个依赖 */
compile group:
‘log4j‘
, name:
‘log4j‘
, version:
‘1.2.17‘
// 简写 => compile ‘log4j:log4j:1.2.17‘
/* 以数组形式添加多个依赖*/
compile
‘joda-time:joda-time:2.9.2‘
,
‘log4j:log4j:1.2.17‘
/* 闭包形式,以添加额外配置*/
compile (group:
‘log4j‘
, name:
‘log4j‘
, version:
‘1.2.17‘
){
// ... 额外配置
}
/* 等价于 */
compile (
‘log4j:log4j:1.2.17‘
){
// ... 额外配置
}
|
1.4 Finding a dependent package name
- Click Maven website
- Search for packages that need to be imported, for example
gson
, click on the corresponding version, for example2.6.2
- Select Gradle, and there will be
‘com.google.code.gson:gson:2.6.2‘
- Copy & Paste
1.5 Complete examples?
1234567891011121314 |
// Sourcecompatibility = 1.8 is a Java version and defaults to the current JVM version apply plugin: ' java ' sourcecompatibility = 1.8 version = ' 1.0 ' repositories { mavencentral () } dependencies { compile ' org.springframework:spring-context:4.2.1.release ' COMPILE&NBSP; ' log4j:log4j : 1.2.17 ' |
- Repositories{.} is the warehouse where these packages are placed, and the following describes
- Sourcecompatibility, version is just some of the Java plugin properties, see Java plugin 45.8
2. Adding a dependent warehouse
You may wonder where these dependencies are found, and where they have been declared. repositories
defines a repository for download dependencies
?
12345678910111213 |
/* Maven Central Respoitory */ repositories { mavencentral () } /* Maven jcenter respoitory */ repositories { jcenter () } /* Maven local respoitory */ /* local repository is a previously downloaded dependency, cached on Local disk */ repositories { Mavenlocal () } |
- No need to remember the address of the warehouse, directly use, multiple warehouses can be used at the same time, usually we will use the remote warehouse with the local warehouse, because the cache of files on the local disk faster, do not need to repeat the download.
- About
jcenter
and mavenCentral
the difference, recommended to see StackOverflow's answer
- Of course, foreign warehouses in the domestic use of the speed may be slow, gradle support custom address, such as the company's warehouse address, domestic warehouse mirror address.
?
123456789101112131415161718192021 |
repositories {
mavenLocal()
/* 指定本地仓库地址 */
maven { url
"file://E:/githubrepo/releases"
}
/* 指定的远程仓库 */
maven { url
"http://mvnrepository.com/"
}
/*
* 公司仓库,可能需要验证
* 不推荐直接将用户名密码写在build.gradle中
* 可以写在~/.gradle/gradle.properties中,再使用
*/
maven {
url
"<you_company_resp_url>"
credentials {
username
‘your_username‘
password
‘your_password‘
}
}
// 支持ivy仓库
ivy { url
"<ivy_resp_url>"
}
}
|
- Sometimes we need to call ourselves to compile the implementation
*.jar
, we can also map the folder containing these files (different from mavenlocal) to a warehouse, but it is generally not recommended
?
123 |
repositories {
flatDir { dirs ‘libs1/java/...‘ , ‘libs2‘ } } |
3. Dependency FAQ 3.1 Dependency transitivity
- Many libraries rely on other libraries, such as A.jar dependent B.jar, in Gradle, just add A.jar, and Gradle will automatically download all the libraries a relies on.
- But sometimes you don't want gradle to do it automatically, like you want to know clearly which libraries to add, configure
transitive
the implementation, compile the times wrong, and you'll know what additional libraries you need to add.
?
1234 |
dependencies {
// transitive 属性默认为 true
compile group: ‘log4j‘ ,name: ‘log4j‘ ,version: ‘1.2.17‘ ,transitive: false } |
- Another scenario is that relying on delivery can cause version conflicts, that is, libraries that rely on pass-through downloads may conflict with another version of the library that the project relies on, in which case some libraries can be excluded and all other dependent libraries, which are selective exclusions, are downloaded.
?
1234567 |
dependencies {
compile ( ‘commons-httpclient:commons-httpclient:3.1‘ ){
exclude group: ‘commons-codec‘ //排除该group的依赖
// exclude group:‘commons-codec‘,module:‘commons-codec‘
// group是必选项,module可选
} } |
3.2 Version Conflicts
- Version conflicts are very common, such as the following example
?
12345 |
// 库 a 传递性依赖库 b-1.2,与添加的b-1.1冲突 dependencies {
compile ‘a:a:1.0‘
compile ‘b:b:1.1‘ } |
?
12345678 |
/* 冲突失败策略设置*/ configurations.all {
resolutionStrategy { failOnVersionConflict() } } /* 强制指定版本策略设置*/ dependencies {
compile group: ‘b‘ ,name: ‘b‘ ,version: ‘1.1‘ ,force: true } |
3.3 Dynamic Dependencies
- Dynamic dependency increases the flexibility of library version dependencies
?
123456 |
dependencies {
/* 选择1以上任意一个版本,这使发生版本冲突的几率变小*/
compile group: ‘b‘ ,name: ‘b‘ ,version: ‘1.+‘
/* 选择最新的版本,避免直接指定版本号 */
compile group: ‘a‘ ,name: ‘a‘ ,version: ‘latest.integration‘ } |
3.4 More Settings
- Specifying library file types
?
12 |
// ext 默认jar,可选属性为war、zip compile group: ‘b‘ ,name: ‘b‘ ,version: ‘1.1‘ ,ext: ‘war‘ |
- Using classifiers (classifiers)
?
12 |
// 例如提供了2种包,a-1.0-dev.war, a-1.0-dev.jar compile group: ‘b‘ ,name: ‘b‘ ,version: ‘1.1‘ ,classifier: ‘dev‘ ,ext: ‘war‘ |
- Replace a delivery dependent version
?
123 |
compile group: ‘a‘ ,name: ‘a‘ ,version: ‘l.0‘ {
dependencies ‘b:b:1.1‘ } |
(1) 查看所有依赖库gradle dependencies(2) 查看指定配置(详见 1.3)的依赖库gradle dependencies -configuration <configuration>例 gradle dependencies -configuration compile => 查看编译时依赖例 gradle dependencies -configuration runtime => 查看运行时依赖
Http://www.cnblogs.com/gzdaijie/p/5296624.html
Java Gradle Getting Started Guide dependency management (add dependencies, warehouses, version conflicts) (GO)