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"
}
相关推荐
I'm Jie1 小时前
(五)Gradle 依赖传递与冲突处理
java·spring boot·spring·kotlin·gradle·maven
hweiyu0012 小时前
Gradle 增量构建与构建缓存:自定义 Task 如何实现 “只构建变化内容”?
gradle·1024程序员节
hweiyu001 天前
Gradle 构建脚本迁移:从 Groovy DSL 到 Kotlin DSL,语法与技巧对比
开发语言·kotlin·gradle
帧栈2 天前
开发避坑指南(66):IDEA 2025 Gradle构建安全协议警告:Maven仓库HTTPS切换或允许HTTP的配置方法
gradle·intellij-idea
宋发元3 天前
IPhone 17 Pro Max拍摄专业画质视频教程
android·gradle·iphone
それども3 天前
IDEA Gradle并行编译内存溢出问题
java·ide·gradle·intellij-idea
Touch&4 天前
Spring5.3.10源码编译和调试(IDEA+Gradle)的过程
spring·framework·gradle·idea·spring源码编译
寒山李白8 天前
关于Java项目构建/配置工具方式(Gradle-Groovy、Gradle-Kotlin、Maven)的区别于选择
java·kotlin·gradle·maven
yacolex10 天前
Mac安装使用Gradle
spring·macos·gradle
hweiyu0011 天前
用 Gradle 实现自动化测试:集成 JUnit、TestNG,生成测试报告
junit·gradle