Gradle notes-dependency management basics

Source: Internet
Author: User
Tags maven central
1. what is dependency management can be divided into two parts: one is dependency, that is, some files required for project construction or runtime; the other is release, that is, the files are uploaded to a certain place after the build is complete.
1.1 depending on most projects, third-party library files or project files are required. These files are the dependencies of the project. For example, JDBC jar package and junit jar package. Gradle needs you to tell it what the project depends on, where it can be found, and then it helps you add the build. In dependency, you may need to download files from a remote repository, such as maven or Ivy, local repository, or even another project. This process is called dependency solution. In addition, the files on which we depend may also be dependent. When Gradle is built, it will also find these dependencies. This process is called dependency transfer.
1.2 The main purpose of releasing most projects is to generate files used outside of the project. For example, generate a jar package, including documents and source code, and then package and release it. These files constitute the output content of the project, and you decide to do it yourself. For example, copy to a directory and upload it to maven or Ivy repository for use in other projects. These can be called Publishing.
2. Dependency declaration the following is an example of dependency declaration:
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. + '}

Here, the content in repositories declares the maven Central Repository, which is where Gradle looks for dependencies. Then, two dependencies are defined. One is the hibernate-core.3.6.7Final.jar required by the ground. The other is the junit version required for testing. The version is later than 4.0. Here we will give a brief explanation, and the subsequent notes will be described in more detail.
3. Configure Gradle as an organization. Each configuration is only a set of specified dependencies, which are called dependency configurations. We can use them to declare external dependencies and release projects. The Java Plug-In defines many standard configurations, which also represent the classpath used by the java plug-in ). For example:
  • Compile: compile the dependencies required by the project code.
  • Runtime: the dependency required during running. By default, it contains dependencies during the compilation period.
  • TestCompile: the dependency required to compile the test code. By default, it contains the class files generated during compilation and the dependencies required during compilation.
  • TestRuntime: Test the dependencies during running. By default, the dependency of the preceding three periods is included.
Different plug-ins provide different standard configurations, which can be defined by ourselves. For more information, see Chapter 1 dependency management.
4. There are many types of external dependency, one of which is called external dependency. It refers to files that are dependent on external building, stored in maven-like repositories, or stored in a directory of the local file system. Define the external dependency to include the group, name, and version attributes. The group and version may not be required depending on the selected repository.
Dependencies {compile group: 'org. hibernate ', name: 'hibernate-core', version: '3. 6.7.final '}

In addition to this writing method, there is also a more concise writing method, which is to concatenate the values of these three attributes and write them as follows: "group: name: version ". As follows:
Dependencies {compile 'org. hibernate: hibernate-core: 3.6.7.final '}

5. The repository Gradle searches for external dependencies in a repository. The repository stores files according to the group, name, and version rules. Gradle supports different repository formats, such as maven and Ivy. It also provides multiple methods to access the repository, such as using a local file system or HTTP. The following is an example of using maven Central Repository:
Repositories {mavenCentral ()}

There are also examples of using maven remote repository:
Repositories {maven {url "http://repo.mycompany.com/maven2 "}}

Use the remote Ivy repository:
Repositories {ivy {url "http://repo.mycompany.com/repo "}}

Use the local Ivy repository:
Repositories {ivy {// URL can refer to a local directory url ".../local-repo "}}

Multiple repositories can be used in a project. Gradle searches in sequence and stops searching after it is found.
6. Package and release dependency configurations are also used for release files, which are called package and release. Plug-Ins provide perfect support for packaging, so we only need to tell Gradle where to release it. In this case, you need to add a repository in the uploadArchives task. The following is an example of publishing to the Ivy repository:
UploadArchives {repositories {ivy {credentials {username "username" password "pw"} url "http://repo.mycompany.com "}}}

Execute gradle uploadArchives. Gradle will build and upload your jar package, and generate an ivy. xml file and upload it together.
The maven plug-in is required for publishing to the maven repository. Gradle also generates pom. xml and uploads it to the target repository together. The following is an example of a maven Repository:
Pply plugin: 'maven 'uploadArchives {repositories {mavenDeployer {repository (url: "file: // localhost/tmp/myRepo /")}}}

This chapter ends with notes.
As Gradle and Android Studio are updated rapidly, some of my projects are gradually upgraded to version 2.1. So 1.2 of the notes should not be completely written. If you have any questions, please refer to the document. In addition, my Gradle translation is in progress. If you are interested in this, please follow the project: https://github.com/msdx/gradledoc. This document is original. The sample code is from the Gradle document.

Gradle notes-dependency management basics

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.