zoukankan      html  css  js  c++  java
  • Chapter 7. Dependency Management Basics 依赖管理基础

    This chapter introduces some of the basics of dependency management in Gradle.

    7.1. What is dependency management?

    Very roughly, dependency management is made up of two pieces. Firstly, Gradle needs to know about the things that your project needs to build or run, in order to find them. We call these incoming files the dependencies of the project. Secondly, Gradle needs to build and upload the things that your project produces. We call these outgoing files the publications of the project. Let's look at these two pieces in more detail:

    Most projects are not completely self-contained. They need files built by other projects in order to be compiled or tested and so on. For example, in order to use Hibernate in my project, I need to include some Hibernate jars in the classpath when I compile my source. To run my tests, I might also need to include some additional jars in the test classpath, such as a particular JDBC driver or the Ehcache jars.

    These incoming files form the dependencies of the project. Gradle allows you to tell it what the dependencies of your project are, so that it can take care of finding these dependencies, and making them available in your build. The dependencies might need to be downloaded from a remote Maven or Ivy repository, or located in a local directory, or may need to be built by another project in the same multi-project build. We call this process dependency resolution.

    Note that this feature provides a major advantage over Ant. With Ant, you only have the ability to specify absolute or relative paths to specific jars to load. With Gradle, you simply declare the “names” of your dependencies, and other layers determine where to get those dependencies from. You can get similar behavior from Ant by adding Apache Ivy, but Gradle does it better.

    //注意,这个特性提供了优于ant的重要的优势,使用ant,你只有一个办法就是通过指定绝对或者相对路径来指定要加载的jar包,使用gradle,你只需要简单的声明依赖的名字,或者其他的可以得到依赖的地方。这个可以通过在ant上添加Apache Ivy来实现,但是gradle做的更好

    Often, the dependencies of a project will themselves have dependencies. For example, Hibernate core requires several other libraries to be present on the classpath with it runs. So, when Gradle runs the tests for your project, it also needs to find these dependencies and make them available. We call these transitive dependencies.

    The main purpose of most projects is to build some files that are to be used outside the project. For example, if your project produces a Java library, you need to build a jar, and maybe a source jar and some documentation, and publish them somewhere.

    These outgoing files form the publications of the project. Gradle also takes care of this important work for you. You declare the publications of your project, and Gradle take care of building them and publishing them somewhere. Exactly what “publishing” means depends on what you want to do. You might want to copy the files to a local directory, or upload them to a remote Maven or Ivy repository. Or you might use the files in another project in the same multi-project build. We call this processpublication.

    7.2. Declaring your dependencies

    Let's look at some dependency declarations. Here's a basic build script:

    //下面是一个基本的构建脚本

    Example 7.1. Declaring dependencies

    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.+'
    }
    

    What's going on here? This build script says a few things about the project. Firstly, it states that Hibernate core 3.6.7.Final is required to compile the project's production source. By implication, Hibernate core and its dependencies are also required at runtime. The build script also states that any junit >= 4.0 is required to compile the project's tests. It also tells Gradle to look in the Maven central repository for any dependencies that are required. The following sections go into the details.

    7.3. Dependency configurations

    In Gradle dependencies are grouped into configurations. A configuration is simply a named set of dependencies. We will refer to them as dependency configurations.

    //gradle中依赖以组的形式配置,一个配置就是一个简单的依赖的名字的集合。我们称它依赖配置

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

    //我们可以使用它们声明外部依赖,后面我们也会看到,它们也可以用来声明项目的发布

    The Java plugin defines a number of standard configurations. These configurations represent the classpaths that the Java plugin uses. Some are listed below, and you 

    can find more details in Table 45.5, “Java plugin - dependency configurations”.

    //java插件定义了一些标准配置,部分如下

    compile

    The dependencies required to compile the production source of the project.

    //用于编译项目源代码的依赖

    runtime

    The dependencies required by the production classes at runtime. By default, also includes the compile time dependencies.

    //

    testCompile

    The dependencies required to compile the test source of the project. By default, also includes the compiled production classes and the compile time dependencies.

    testRuntime

    The dependencies required to run the tests. By default, also includes the compile, runtime and test compile dependencies.

    Various plugins add further standard configurations. You can also define your own custom configurations to use in your build. Please see Section 23.3, “Dependency configurations” for the details of defining and customizing dependency configurations.

    7.4. External dependencies

    There are various types of dependencies that you can declare. One such type is an external dependency. This is a dependency on some files built outside the current build, and stored in a repository of some kind, such as Maven central, or a corporate Maven or Ivy repository, or a directory in the local file system.

    To define an external dependency, you add it to a dependency configuration:

    //为了定义外部依赖,你需要把它添加到依赖配置中

    Example 7.2. Definition of an external dependency

    build.gradle

    dependencies {
        compile group: 'org.hibernate', name: 'hibernate-core', version: '3.6.7.Final'
    }
    

    An external dependency is identified using groupname and version attributes. Depending on which kind of repository you are using, group and version may be optional.

    //外部依赖使用group name和version属性来生命,group和version是可选的

    The shortcut form for declaring external dependencies looks like “group:name:version”.

    //生命外部依赖的简洁写法为group:name:version

    Example 7.3. Shortcut definition of an external dependency

    build.gradle

    dependencies {
        compile 'org.hibernate:hibernate-core:3.6.7.Final'
    }
    

    To find out more about defining and working with dependencies, have a look at Section 23.4, “How to declare your dependencies”.

    7.5. Repositories

    How does Gradle find the files for external dependencies? Gradle looks for them in a repository. A repository is really just a collection of files,

    organized by groupname andversion. Gradle understands several different repository formats, such as Maven and Ivy, and several different ways of accessing the repository, such as using the local file system or HTTP.

    //gradle是如何找到这些外部依赖的呢,gradle会在repository(仓库)中寻找。一个仓库是一个真正存储的文件的存储集合,按照group,name,version的类型组织起来。gradle了解几种不同类型的仓库形式,例如maven,ivy,gradle也了解一些不同的访问仓库的方法,例如使用本地文件系统或者http

    By default, Gradle does not define any repositories. You need to define at least one before you can use external dependencies. One option is use the Maven central repository:

    //默认情况gradle不会定义任何仓库,你需要在使用外部依赖前至少定义一个。一种选择是使用Maven central 仓库

    Example 7.4. Usage of Maven central repository

    build.gradle

    repositories {
        mavenCentral()
    }
    

    Or Bintray's JCenter:

    Example 7.5. Usage of JCenter repository

    build.gradle

    repositories {
        jcenter()
    }
    

    Or a any other remote Maven repository:

    Example 7.6. Usage of a remote Maven repository

    build.gradle

    repositories {
        maven {
            url "http://repo.mycompany.com/maven2"
        }
    }
    

    Or a remote Ivy repository:

    Example 7.7. Usage of a remote Ivy directory

    build.gradle

    repositories {
        ivy {
            url "http://repo.mycompany.com/repo"
        }
    }
    

    You can also have repositories on the local file system. This works for both Maven and Ivy repositories.

    //你也可以使用本地文件系统的仓库,对于maven和ivy都可以这样用。

    Example 7.8. Usage of a 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 a dependency in each repository in the order they are specified, stopping at the first repository that contains the requested module.

    //一个项目可以有多个仓库,gradle会按照仓库定义的顺序依次在每个仓库中寻找,找到第一个包含这个请求模块的仓库后就停止寻找。

    To find out more about defining and working with repositories, have a look at Section 23.6, “Repositories”.

    7.6. Publishing artifacts

    Dependency configurations are also used to publish files.[2] We call these files publication artifacts, or usually just artifacts.

    //我们称发布的文件为publication artifacts或artifacts

    The plugins do a pretty good job of defining the artifacts of a project, so you usually don't need to do anything special to tell Gradle what needs to be published. However, you do need to tell Gradle where to publish the artifacts. You do this by attaching repositories to the uploadArchives task. Here's an example of publishing to a remote Ivy repository:

    //通常你不需要做任何指定来告诉gradle要发布什么,但是你需要告诉gradle要把artifacts发布在哪。

    Example 7.9. Publishing to an Ivy repository

    build.gradle

    uploadArchives {
        repositories {
            ivy {
                credentials {
                    username "username"
                    password "pw"
                }
                url "http://repo.mycompany.com"
            }
        }
    }
    

    Now, when you run gradle uploadArchives, Gradle will build and upload your Jar. Gradle will also generate and upload an ivy.xml as well.

    //当你运行gradle uploadArchives命令时,gradle将会构建和上传jar包。gradle也会生成ivy.xml文件并上传。

    You can also publish to Maven repositories. The syntax is slightly different.[3] Note that you also need to apply the Maven plugin in order to publish to a Maven repository. when this is in place, Gradle will generate and upload a pom.xml.

    //你也可以发布到maven仓库中,语法稍微不同。maven也会生成pom.xml文件并上传

    Example 7.10. Publishing to a Maven repository

    build.gradle

    apply plugin: 'maven'
    
    uploadArchives {
        repositories {
            mavenDeployer {
                repository(url: "file://localhost/tmp/myRepo/")
            }
        }
    }
    

    To find out more about publication, have a look at Chapter 30, Publishing artifacts.

    7.7. Where to next?

    For all the details of dependency resolution, see Chapter 23, Dependency Management, and for artifact publication see Chapter 30, Publishing artifacts.

    If you are interested in the DSL elements mentioned here, have a look at Project.configurations{}Project.repositories{} and Project.dependencies{}.

    Otherwise, continue on to some of the other tutorials.

    ----------- Do not start just casually, and do not end just casually. -----------
  • 相关阅读:
    运维常用python库&模块
    find&正则表达式
    博客园背景粒子连线代码
    xshell几款绝佳配色方案
    解决ssh连接超时(ssh timeout)的方法
    Java-计划存储
    @Repository、@Service、@Controller 和 @Component
    帮助对@Repository注解的理解
    Struts 2学习笔记——拦截器相关
    JAVA UUID 生成
  • 原文地址:https://www.cnblogs.com/yexiant/p/5532087.html
Copyright © 2011-2022 走看看