Java Gradle Getting Started Guide dependency management (add dependencies, warehouses, version conflicts) (GO)

Source: Internet
Author: User
Tags maven central jcenter log4j

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
    • Common dependencies consist of two types.

      (1) A class is required in the project library, including local/warehouse files and other project files (such as a multi-project project, one project depends on another project)

      (2) A class is required to achieve automated compilation, deployment and so on, including Gradle API and groovy written task, plugin, etc., this kind of dependency in the first 2 essays are mentioned and used

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. repositoriesdefines 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 属性默认为 truecompile 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‘}
    • Gradle There are several ways to resolve conflicts:

      (1) Recent version policy (default): The previous example ignores b-1.1 and downloads b-1.2
      (2) Conflict failure policy: compilation fails when a conflict occurs (some newer versions of the library are not compatible, so this library allows developers to make a choice)

      (3) Enforce specified version policy: When a conflict occurs, use the version specified by the developer

?
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、zipcompile group:‘b‘,name:‘b‘,version:‘1.1‘,ext:‘war‘
    • Using classifiers (classifiers)
?
12 // 例如提供了2种包,a-1.0-dev.war, a-1.0-dev.jarcompile 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‘}
    • Common commands
(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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.