Gradle tutorial description Chapter 1 Dependency management basics, gradle User Guide

Source: Internet
Author: User
Tags maven central

Gradle tutorial description Chapter 1 Dependency management basics, gradle User Guide
8.1 what is dependency management?

Dependency management is roughly divided into two parts:

· Build depends on something

· Build release something


8.2 declare your dependencies. Let's take a look at some dependency declarations. This is a basic build script:

For example, declare dependency
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. +'
}

This build script says several things about the project. First, it points out that the core 3.6.7.Final of Hibernate needs to be involved in the compilation project.

This means that the core of Hibernate needs to be dependent at runtime.

The build script also stipulates that junit> = 4.0 is required to compile the project for testing.

It also tells Gradle to find any dependencies in the Maven repository.


8.3 dependency Configuration

One configuration is the dependency of a simple nameset. We will configure them as dependencies.

You can use them to define the project's external dependencies. As we will see later, they also define the release of projects.


The Java Plug-In defines some standard configurations. These configurations represent the class paths used by the Java Plug-in. Some of the following are listed:

Compile
Compile the dependencies required by the source code.

Runtime
The dependency between classes that must be generated during running. By default, it also includes dependencies during compilation.

TestCompile
Compile the dependencies required for the test source code of the project. By default, it also includes the dependencies generated during the compilation of classes and.

TestRuntime
Dependencies required for running the test. By default, compilation, running, and testing are also included.


8.4 external dependency

You can define multiple types of dependencies.

External Dependency: Depends on some files outside the current build project and is stored in the repository, such as the Maven center, enterprise Maven Or Ivy library, or a directory in the local file system.


For example, definitions of external dependencies
Build. gradle:
Dependencies {
Compile group: 'org. hibernate ', name: 'hibernate-core', version: '3. 6.7.final'
}
External Dependency. Use the ID group, name, and version attributes. The dependency repository, group, and version are optional.
The shortcut for defining external dependencies looks like "group: Name: version ".

Example: Quick definition of external dependencies
Build. gradle:
Dependencies {
Compile 'org. hibernate: hibernate-core: 3.6.7.final'
}

8.5 warehouse

Gradle looks for external dependencies in a library. A repository is only a collection of files, including group, name, and version.

Gradle understands several different repository formats, such as Maven and Ivy, and several different methods for accessing the repository, such as using a local file system or HTTP.


By default, Gradle does not define any resource library. You must define at least one to use external dependencies.


For example, use of Maven central repository
Build. gradle:
Repositories {
MavenCentral ()
}

Example: Use of remote Maven Repository
Build. gradle:
Repositories {
Maven {
Url "http://repo.mycompany.com/maven2"
}
}

For example, remote Ivy directory usage
Build. gradle:
Repositories {
Ivy {
Url "http://repo.mycompany.com/repo"
}

}


You can also create a repository on the local file system. This applies to Maven and Ivy libraries.


For example, 8.7. Use of the local Ivy directory
Build. gradle
Repositories {
Ivy {
// URL can refer to a local directory
Url "../local-repo"
}
}
A project can have multiple repositories. Gradle searches for dependencies in each database in the specified order, and stops when it is first found in a database.



8.6 release

The dependency configuration is also used to publish files.

Generally, you do not need to tell Gradle what needs to be released. However, you need to tell Gradle where to publish it. You can connect to the uploadArchives task of the library.


Here is an example of publishing to the remote Ivy Library:

For example, publish to the Ivy Library

Build. gradle:
UploadArchives {
Repositories {
Ivy {
Credentials {
Username "username"
Password "pw"
}
Url "http://repo.mycompany.com"
}
}
}
Now, when you run gradle uploadArchives, Gradle will create and upload your Jar file. Gradle also generates and uploads ivy. xml.

You can also publish it to the Maven repository. The syntax is slightly different. Please note that you also need to use the Maven plug-in to publish it to the Maven repository.

Then, Gradle generates and uploads pom. xml.


For example, publish to the Maven Repository
Build. gradle:
Apply plugin: 'maven'

UploadArchives {
Repositories {
MavenDeployer {
Repository (url: "file: // localhost/tmp/myRepo /")
}
}
}

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.