zoukankan      html  css  js  c++  java
  • gradle 项目依赖管理 https://www.codenong.com/js59ea577da346/ 原文链接

    一、依赖管理

    1. 依赖配置
      compile:编译范围依赖在所有的 classpath 中可用,同时它们也会被打包
      runtime:runtime依赖在运行和测试系统的时候需要,但在编译的时候不需要。比如,你可能在编译的时候只需要 JDBC API JAR,而只有在运行的时候才需要 JDBC 驱动实现
      testCompile:测试期编译需要的附加依赖
      testRuntime:测试运行期需要
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    dependencies {
        // 本地项目
        compile project(':com.csii.channel.core:core-common')
        compile project(':com.csii.channel.core:core-dynamic')
      // 外部依赖
        compile group: 'commons-net', name: 'commons-net', version:'2.0'
        compile group: 'com.google.guava', name: 'guava', version:'21.0'
        compile group: 'org.apache.mina', name: 'mina-core', version:'2.0.16'
        testCompile group: 'junit', name: 'junit', version:'4.12'
    }

    二、仓库

    1. 远程仓库
      使用Maven中央仓库,maven仓库的URL为:http://repo1.maven.org/maven2/
    1
    2
    3
    repositories {
        mavenCentral()
    }

    使用Maven远程仓库

    1
    2
    3
    4
    5
    6
    repositories {
        mavenLocal()
        maven { url "http://maven.aliyun.com/nexus/content/groups/public/" }
        maven { url 'https://repo.spring.io/milestone' }
        mavenCentral()
    }
    1. 本地仓库
      配置Gradle使用maven本地仓库:
      CRADLE_USER_HOME:D:.m2
      epository


      配置Gradle使用maven本地仓库

      修改配置build.gradle:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    /**
     * 指定所使用的仓库,mavenCentral()表示使用中央仓库,
     * 此刻项目中所需要的jar包都会默认从中央仓库下载到本地指定目录
     * 配置mavenLocal()表示引入jar包的时候,先从本地仓库中找,没有再去中央仓库下载
     */
    repositories {
        mavenLocal()
        mavenCentral()
    }
    1. 修改或者添加额外的私有仓库地址
      直接修改 settings.gradle 来添加其它仓库:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    // settings.gradle
    pluginManagement {
        repositories {
            maven {
                url 'http://examle.com/maven-repo'
            }
            gradlePluginPortal()
        }
    }

    三、多项目构建

    多项目构成:allProjects = root项目+各子项目
    settings文件声明了所需的配置来实例化项目的层次结构。在默认情况下,这个文件被命名为settings.gradle,并且和根项目的build.gradle文件放在一起,该文件在初始化阶段被执行。根项目就像一个容器,子项目会迭代访问它的配置并注入到自己的配置中。

    1. 多项目构建
      多项目构建总是需要指定一个树根,树中的每一个节点代表一个项目,每一个Project对象都指定有一个表示在树中位置的路径;在设置文件中我们还可以使用一套方法来自定义构建项目树。
      settings.gradle作用就是用于多项目构建,一般像这样:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    // 父模块名称
    rootProject.name = 'iot-cloud-platform'
    /* 引入子模块 */
    include 'iot-commons'
    include 'iot-service'
    include 'iot-config'
    include 'iot-docs'
    include 'iot-gateway'
    include 'iot-quartz'
    include 'iot-monitor'
    include 'iot-register'
    include 'iot-ui'
    include 'iot-transaction'
    include 'iot-auth'
    include 'iot-service:user-center'
    findProject(':iot-service:user-center')?.name = 'user-center'
    1. subprojects子项目通用配置
      subprojects是对所有Child Project的配置:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    subprojects {
        apply plugin: 'java'
        apply plugin: 'idea'
        // spring boot 插件
        apply plugin: 'org.springframework.boot'
        // 必须添加spring依赖管理
        apply plugin: 'io.spring.dependency-management'
        repositories {
            mavenLocal()
            // 阿里云仓库
            maven { url "http://maven.aliyun.com/nexus/content/groups/public/" }
            // Spring仓库
            maven { url 'https://repo.spring.io/milestone' }
            mavenCentral()
        }
        dependencies {
            testImplementation('org.springframework.boot:spring-boot-starter-test') {
                exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
            }
        }
        dependencyManagement {
            imports {
                // SpringCloud依赖
                mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
            }
        }
        jar {
            manifest.attributes provider: 'gradle'
        }
    }
    1. buildScript
      buildScript块的repositories主要是为了Gradle脚本自身的执行,获取脚本依赖插件。
      buildscript中的声明是gradle脚本自身需要使用的资源。可以声明的资源包括依赖项、第三方插件、maven仓库地址等。
      gradle在执行脚本时,会优先执行buildscript代码块中的内容,然后才会执行剩余的build脚本。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    buildscript {
        // 定义SpringCloud版本,统一规定springboot的版本
        ext {
            set('springCloudVersion', "Hoxton.RC2")
            set('springBootVersion', "2.2.1.RELEASE")
            set('version', "1.0.0")
        }
        dependencies {
            // 用来打包
            classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        }
    }

    ext:ext是自定义属性,现在很多人都喜欢把所有关于版本的信息都利用ext放在另一个自己新建的gradle文件中集中管理。

    1. allprojects
      allprojects块的repositories用于多项目构建,为所有项目提供共同所需依赖包。而子项目可以配置自己的repositories以获取自己独需的依赖包。

    buildscript和allprojects的作用和区别:
    buildscript中的声明是gradle脚本自身需要使用的资源,就是说它自己需要的资源,跟你其它模块其实并没有什么关系。而allprojects声明的却是所有module所需要使用的资源,就是说你的每个module都需要用同一个第三库的时候,可以在allprojects里面声明。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    allprojects {
        repositories {
            mavenLocal()
            // 阿里云仓库
            maven { url "http://maven.aliyun.com/nexus/content/groups/public/" }
            // Spring仓库
            maven { url 'https://repo.spring.io/milestone' }
            mavenCentral()
        }
        // 指定编码格式
        tasks.withType(JavaCompile) {
            options.encoding = "UTF-8"
        }
    }

    四、gradle.properties

    ??gradle中的常用属性可以写在gradle.properties中。
    ??一般我们都把全局属性都编写在一个工具类中,如果是有环境的切换的话,那么我们还会定义一个标志来进行相应的变换。对于项目而言,有时候需要配置某些敏感信息。比如密码,帐号等。而这些信息需要被很多类共同使用,所以必须有一个全局的配置。当需要把项目push到git上时,我们不希望别人看到我们项目的key,token等。我们可以将这些信息设置在gradle.properties中。
    只有在Android中才可使用。

    1
    AppKey = 1234567890

    在build.gradle(module app)中进行变量的重定义,即将配置内容转化成java能够使用的形式:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    android {
        buildTypes {
            release {
                minifyEnabled true
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
                //buildConfigField用于给BuildConfig文件添加一个字段
                buildConfigField("String","KEY",""${AppKey}"")
            }
            debug{
                buildConfigField("String","KEY",""${AppKey}"")
            }
        }
    }

    五、Gradle 插件(Plugins)

    Gradle 也可以用下面的方式声明使用的插件:

    1
    2
    3
    4
    5
    plugins {
        id 'org.springframework.boot' version '2.2.1.RELEASE'
        id 'io.spring.dependency-management' version '1.0.8.RELEASE'
        id 'java'
    }

    其实是从 Gradle 官方的插件仓库https://plugins.gradle.org/m2/下载的。

    六、常见的task命令

    build:当运行gradle build命令时Gradle将会编译和测试你的代码,并且创建一个包含类和资源的JAR文件。
    clean:当运行gradle clean命令时Gradle将会删除build生成的目录和所有生成的文件。
    assemble:当运行gradle assemble命令时Gradle将会编译并打包代码,但是并不运行单元测试。
    check:当运行gradle check命令时Gradle将会编译并测试你的代码,其他的插件会加入更多的检查步骤。

    七、gradle-wrapper

    Wrapper是对Gradle的一层包装,便于在团队开发过程中统一Gradle构建的版本号,这样大家都可以使用统一的Gradle版本进行构建。

    wrapper

    1
    2
    3
    4
    5
    distributionBase=GRADLE_USER_HOME
    distributionPath=wrapper/dists
    distributionUrl=https://services.gradle.org/distributions/gradle-5.6.4-bin.zip
    zipStoreBase=GRADLE_USER_HOME
    zipStorePath=wrapper/dists

    distributionUrl是要下载的gradle的地址,使用哪个版本的gradle,就在这里修改。
    gradle的3种版本:
    gradle-xx-all.zip是完整版,包含了各种二进制文件,源代码文件,和离线的文档。
    gradle-xx-bin.zip是二进制版,只包含了二进制文件(可执行文件),没有文档和源代码。
    gradle-xx-src.zip是源码版,只包含了Gradle源代码,不能用来编译你的工程。

    zipStoreBase和zipStorePath组合在一起,是下载的gradle-3.1-bin.zip所存放的位置。
    zipStorePath是zipStoreBase指定的目录下的子目录。

    distributionBase和distributionPath组合在一起,是解压gradle-5.6.4-bin.zip之后的文件的存放位置。
    distributionPath是distributionBase指定的目录下的子目录。

    下载位置可以和解压位置不一样。
    zipStoreBase和distributionBase有两种取值:GRADLE_USER_HOME和PROJECT。
    其中,GRADLE_USER_HOME表示用户目录。
    在windows下是%USERPROFILE%/.gradle,例如C:Users.gradle。
    在linux下是$HOME/.gradle,例如~/.gradle。
    PROJECT表示工程的当前目录,即gradlew所在的目录。

    八、依赖分组

    在Gradle中,依赖都被会分配到某一个具体的configuration中,Configuration代表着一个或多个构件及构件所需依赖的一个分组。

    1
    2
    3
    4
    5
    configurations.all {
        resolutionStrategy {
            force 'org.hamcrest:hamcrest-core:1.3'
        }
    }

    九、依赖版本冲突

    Gradle对解决传递依赖提供了两种策略,使用最新版本或者直接导致构建失败。默认的策略是使用最新版本。虽然这样的策略能够解决一些问题,但是还是不够。常见的一种情况是,NoSuchMethond或者ClassNotFound。这时候,你可能需要一些特殊手段,比如排除不想要的传递依赖。
    排除传递依赖的方式有两种:
    1.直接在configuration中排除
    2.在具体的某个dependency中排除

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    configurations {
        compile.exclude module: 'commons'
        all*.exclude group: 'org.gradle.test.excludes', module: 'reports'
    }

    dependencies {
        compile("org.gradle.test.excludes:api:1.0") {
            exclude module: 'shared'
        }
    }
  • 相关阅读:
    jQuery获取鼠标事件源
    windows中MongoDB安装和环境搭建
    前端获取后台数据的方法:ajax、axios、fetch
    浅谈:easy-mock的使用
    安全篇-AES/RSA加密机制
    PHP开发api接口安全验证
    Ajax简单实现文件异步上传的多种方法
    PHP7有没有你们说的那么牛逼
    基于laravel框架构建最小内容管理系统
    redis用法分析
  • 原文地址:https://www.cnblogs.com/lykbk/p/sdssdwewewewewewewewew.html
Copyright © 2011-2022 走看看