Gradle上传依赖包到私有仓库

一、背景

gradle脚本管理项目的过程中,我们会有很多依赖包需要管理。采用自搭建Maven仓库的形式是一个很好的选择,我们可以把第三方包,或者自己开发的包上传仓库,提供给其他项目使用。

由于每次把.aar放在项目中,管理和依赖都不统一,上传maven,统一依赖方式,更容易清楚的保留依赖记录。

创建单独的.gradle脚本文件,在需要的项目中进行引入和配置参数,很方便做到统一管理。

二、脚本

第三方.aar上传

复制代码
plugins {
    id 'maven-publish'
}

def aarFile = file('nuisdk-release.aar')

publishing {
    publications {
        release(MavenPublication) {
            groupId = "cn.aihongbo.third" // 组 ID
            artifactId = "nui" // 项目名称
            version = "1.0.0" // 版本号

            artifact(aarFile) {
                extension 'aar'
            }
        }
    }
    repositories {
        maven {
            url = "https://localhost/nexus/repository/android-releases/"  // 替换为私有仓库URL
            allowInsecureProtocol = true    // 新版本gradle默认强制使用https,这里需要设置允许使用http
            credentials {
                username = "【账号】"          // 仓库认证用户名
                password = "【密码】"       // 仓库认证密码
            }
        }
    }
}

项目模块上传

复制代码
apply plugin: 'maven-publish'

afterEvaluate {
    publishing {
        publications {
            release(MavenPublication) {
                groupId project.ext.groupid
                artifactId project.ext.name
                version project.ext.version
                afterEvaluate {
                    from components.release
                }
            }
        }
        repositories {
            maven {
                def releasesRepoUrl = "https://localhost/nexus/repository/android-releases/"
                def snapshotsRepoUrl = "https://localhost/nexus/repository/android-snapshots/"
                url publications.release.version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
                allowInsecureProtocol true
                credentials {
                    username = "【账号】"          // 仓库认证用户名
                    password = "【密码】"       // 仓库认证密码
                }
            }
        }
    }
}

自有项目使用

项目模块的build.gradle中添加

复制代码
apply from: '../../../../config/upload.gradle'
ext {
    groupid = "com.aihongbo.component"
    name = "nui"
    version = "1.0.3"
}
相关推荐
尚学教辅学习资料2 天前
SpringBoot3.x入门到精通系列:1.2 开发环境搭建
spring boot·gradle·maven·intellij idea·jdk17·开发环境
真夜2 天前
关于rn下载gradle依赖速度慢的问题
react native·gradle·android studio
小墙程序员3 天前
一文了解 AGP8 的使用
android·gradle
南玖yy7 天前
Linux 桌面市场份额突破 5%:开源生态的里程碑与未来启示
linux·运维·服务器·汇编·科技·开源·gradle
yzpyzp8 天前
目前市面上arm64-v8a、armeabi-v7a设备的市占率有多少?为什么x86架构的手机越来越少?
android·gradle·cpu·ndk
yzpyzp8 天前
ndk { setAbiFilters([‘armeabi-v7a‘, “arm64-v8a“]) }
android·gradle·ndk
yzpyzp11 天前
targetSdk作用
android·gradle
cherishSpring13 天前
gradle微服务依赖模版
微服务·gradle
一线大码21 天前
Gradle 高级篇之构建多模块项目的方法
spring boot·gradle·intellij idea
SUNxuetian24 天前
【Android Studio】升级AGP-8.6.1,Find Usage对Method失效的处理方法!
android·ide·gradle·android studio·安卓