Gradle优化:maven仓库配置

Project根目录build.gradle:
改造前:


buildscript {

    ext.coroutinesVersion = "1.4.2"
    
    repositories {
        maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
        maven { url 'http://maven.aliyun.com/nexus/content/repositories/jcenter' }
        maven { url 'http://maven.aliyun.com/nexus/content/repositories/google' }
        maven { url 'http://maven.aliyun.com/nexus/content/repositories/gradle-plugin' }
        maven { url "https://www.jitpack.io" }
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:4.1.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$navigationVersion"
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
        maven { url 'http://maven.aliyun.com/nexus/content/repositories/jcenter' }
        maven { url 'http://maven.aliyun.com/nexus/content/repositories/google' }
        maven { url 'http://maven.aliyun.com/nexus/content/repositories/gradle-plugin' }
        maven { url "https://www.jitpack.io" }
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

为了gradle文件简介,新建config.gradle文件,里面包含maven仓库配置的三种方式


ext.count = "1"

//maven仓库配置文件方式1

ext.addRepos = { handler ->
    handler.maven {
        url 'http://maven.aliyun.com/nexus/content/groups/public/'
    }
    handler.maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
    handler.maven { url 'http://maven.aliyun.com/nexus/content/repositories/jcenter' }
    handler.maven { url 'http://maven.aliyun.com/nexus/content/repositories/google' }
    handler.maven { url 'http://maven.aliyun.com/nexus/content/repositories/gradle-plugin' }
    handler.maven { url "https://www.jitpack.io" }
}

//maven仓库配置文件方式2
def addRepos1(RepositoryHandler handler) {
    handler.maven {
        url 'http://maven.aliyun.com/nexus/content/groups/public/'
    }
    handler.maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
    handler.maven { url 'http://maven.aliyun.com/nexus/content/repositories/jcenter' }
    handler.maven { url 'http://maven.aliyun.com/nexus/content/repositories/google' }
    handler.maven { url 'http://maven.aliyun.com/nexus/content/repositories/gradle-plugin' }
    handler.maven { url "https://www.jitpack.io" }
}

ext.addRepos1 = this.&addRepos1

//maven仓库配置文件方式3
def addRepos3 = {
    handler ->
        handler.maven {
            url 'http://maven.aliyun.com/nexus/content/groups/public/'
        }
        handler.maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
        handler.maven { url 'http://maven.aliyun.com/nexus/content/repositories/jcenter' }
        handler.maven { url 'http://maven.aliyun.com/nexus/content/repositories/google' }
        handler.maven { url 'http://maven.aliyun.com/nexus/content/repositories/gradle-plugin' }
        handler.maven { url "https://www.jitpack.io" }
}
ext.addRepos3 = addRepos3

改造后:


buildscript {
//导入config.gradle
    apply from: "config.gradle"

//config.gradle中maven仓储配置调用
    addRepos(repositories)
    addRepos1(repositories)
    addRepos2(repositories)
    dependencies {
        classpath 'com.android.tools.build:gradle:4.1.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$navigationVersion"
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    addRepos(repositories)
}

task clean(type: Delete) {
    delete rootProject.buildDir
}


相比于以前代码更加精简。

推荐文章:
Gradle 系列 (一)、Gradle相关概念理解,Groovy基础
Gradle 系列(二)、技术探索
Gradle 系列 (三)、Gradle 插件开发
Gradle 系列 (四)、Gradle 插件实战应用

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值