Gradle Tutorial Description User Guide Chapter 8th reliance on Management fundamentals

Source: Internet
Author: User

8.1 What is dependency management?

Dependency management is very roughly divided into two parts:

· Build depends on what it's from.

· Build publishes something.


8.2 Declaration of your DependenceLet's take a look at some of the dependency declarations. This is a basic build script:

example, declaring a 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 a few things about the project. First, it points out that the core 3.6.7.Final of hibernate needs to be involved in compiling projects.

This means that the core of hibernate needs to be relied upon at run time.

The build script also stipulates that the test that compiles the project requires junit> = 4.0.

It also tells Gradle to find any dependencies that are required in the MAVEN repository.


8.3 Dependency Configuration

A configuration is a simple named set of dependencies. We will use them as dependent configurations.

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


The Java plug-in defines some standard configurations. These configurations represent the classpath used by the Java plug-in. Some of the following are listed below:

Compile
The dependency required to compile the source code.

Runtime
The dependencies that are required at run time to produce classes. By default, it also includes compile-time dependencies.

Testcompile
The dependencies required to compile the test source for the project. By default, compilation is also included to generate classes and compile-time dependencies.

Testruntime
The dependencies required to run the test. By default, it also includes compiling, running, and testing compilation dependencies.


8.4 External Dependencies

There are multiple types of dependencies that you can define.

External dependencies: dependent on some files outside of the current build project, and are stored in warehouses, such as the central Maven, or the enterprise Maven or Ivy libraries, or directories in the local file system.


example, the definition of an external dependency
Build.gradle:
dependencies {
Compile group: ' Org.hibernate ', Name: ' Hibernate-core ', version: ' 3.6.7.Final '
}
external dependencies, using identity group, name, and version properties. Which warehouse to rely on, groups and versions are optional.
The shortcut form that defines external dependencies looks like "group: Name: Version".

example, the shortcut definition for external dependencies
Build.gradle:
dependencies {
Compile ' org.hibernate:hibernate-core:3.6.7.final '
}

8.5 Warehouses

Gradle look for external dependencies in a library. The storage warehouse is only a collection of files, consisting of group, name, and version.

Gradle knows several different repository formats, such as Maven and Ivy, and several different ways to access the repository, such as using a local file system or HTTP.


By default, Gradle does not have any resource libraries defined. You need to define at least one before you can use external dependencies.


example, the use of MAVEN's central repository
Build.gradle:
repositories {
Mavencentral ()
}

example, the use of remote maven repositories
Build.gradle:
repositories {
Maven {
URL "Http://repo.mycompany.com/maven2"
}
}

example, usage of the remote Ivy directory
Build.gradle:
repositories {
Ivy {
URL "Http://repo.mycompany.com/repo"
}

}


You can also store the repository on the local file system. This applies to the 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 will look for dependencies in each library in the order specified and stop first finding them in a library.



8.6 Release

Dependent configurations are also used to publish files.

There is usually no need to specifically tell gradle what needs to be published. However, you need to tell Gradle where to publish. You can connect the library by connecting the Uploadarchives task.


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


Build.gradle:
uploadarchives {
    repositories {
        Ivy {
            Credentials {
              &NBSP ; Username "username"
                password "PW"
      &NBSP ;    
            URL "http://repo.mycompany.com"
      &NBSP ;
   }
}
Now, when you run Gradle Uploadarchives,gradle will build and upload your jar file. Gradle will also generate and upload ivy.xml.

You can also publish to the Maven repository. The syntax is slightly different. Please note that you will also need to use the Maven plugin to publish to the Maven repository.

At this point, Gradle will generate and upload the pom.xml.


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

uploadarchives {
repositories {
Mavendeployer {
Repository (URL: "file://localhost/tmp/myRepo/")
}
}
}

Gradle Tutorial Description User Guide Chapter 8th reliance on Management fundamentals

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.