• 【Android Studio Gradle】发布aar到私有Artifactory仓库


    1. 前言

    【Android Studio Gradle】使用Artifactory构建本地仓库中介绍了如何利用工具配置一个maven私有库,那么在开发library的时候为了方便难免会用到需要将该库发布到这个仓库的功能。经过测试和配置,确实在Artifactory仓库中也可以通过gradlew命令执行task上传aar包。这里记录一下。

    2. 配置

    首先在自己搭建的私有库中简单生成一个gradle配置:
    在这里插入图片描述
    在这里插入图片描述
    然后进行Generate Settings。就可以看到生成了如下的配置提示:

    buildscript {
        repositories {
            maven {
                url 'http://xxxx:8082/artifactory/android_group'
                credentials {
                    username = "${artifactory_user}"
                    password = "${artifactory_password}"
                }
            }
            
        }
        dependencies {
            //Check for the latest version here: http://plugins.gradle.org/plugin/com.jfrog.artifactory
            classpath "org.jfrog.buildinfo:build-info-extractor-gradle:4+"
        }
    }
    
    allprojects {
        apply plugin: "com.jfrog.artifactory"
    }
    
    artifactory {
        contextUrl = "${artifactory_contextUrl}"   //The base Artifactory URL if not overridden by the publisher/resolver
        publish {
            repository {
                repoKey = 'android_local'
                username = "${artifactory_user}"
                password = "${artifactory_password}"
                maven = true
                
            }
        }
        resolve {
            repository {
                repoKey = 'android_group'
                username = "${artifactory_user}"
                password = "${artifactory_password}"
                maven = true
                
            }
        }
    }
    
    • 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
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    结合maven-publish的配置以及网上相关博客的配置,可以写出下面的最终配置:

    apply plugin: 'com.android.library'
    
    buildscript {
        apply from: './setting.gradle'
    
        repositories {
            mavenLocal()
            maven {
                allowInsecureProtocol true
                url "http://${maven_host}:8082/artifactory/android_group/"
            }
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:7.1.3'
            //http://plugins.gradle.org/plugin/com.jfrog.artifactory
            classpath "org.jfrog.buildinfo:build-info-extractor-gradle:4.15.2"
        }
    }
    
    allprojects {
        repositories {
            mavenLocal()
            maven {
                allowInsecureProtocol true
                url "http://${maven_host}:8082/artifactory/android_group/"
            }
        }
    }
    
    apply plugin: 'com.jfrog.artifactory'
    apply plugin: 'maven-publish'
    
    
    def GROUPID = 'com.mengfou'
    def ARTIFACTID = 'homepage'
    def VERSION = '1.0.1-SNAPSHOT'
    
    android {
        compileSdk 31
    
        defaultConfig {
            minSdk 21
            targetSdk 31
    
            testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
            consumerProguardFiles "consumer-rules.pro"
        }
    
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            }
        }
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion.VERSION_1_8
        }
    }
    
    dependencies {
    
        implementation 'androidx.appcompat:appcompat:1.3.0'
        implementation 'com.google.android.material:material:1.4.0'
        implementation 'org.jetbrains:annotations:15.0'
        implementation 'org.jetbrains:annotations:15.0'
        testImplementation 'junit:junit:4.13.2'
        androidTestImplementation 'androidx.test.ext:junit:1.1.3'
        androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    }
    
    artifactory {
        contextUrl = "http://${maven_host}:8082/artifactory"   //The base Artifactory URL if not overridden by the publisher/resolver
        publish {
            repository {
                repoKey = 'android_local'
                username = "${admin}"
                password = "${password}"
                maven = true
            }
        }
    }
    
    publishing {
        publications {
            mengfou(MavenPublication) {
                groupId = GROUPID
                artifactId = ARTIFACTID
                version = VERSION
    
                artifact("$buildDir/outputs/aar/${project.getName()}-release.aar")
    
                pom.withXml {
                    def dependencies = asNode().appendNode("dependencies")
                    configurations.implementation.allDependencies.each {
                        def dependency = dependencies.appendNode("dependency")
                        dependency.appendNode("groupId", it.group)
                        dependency.appendNode("artifactId", it.name)
                        dependency.appendNode("version", it.version)
                    }
                }
            }
        }
    }
    
    artifactoryPublish {
        dependsOn("clean")
        finalizedBy("assembleRelease")
        contextUrl = "http://${maven_host}:8082/artifactory/"
        publications ('mengfou')                            // mengfou定义在前面
    
        clientConfig.publisher.repoKey = 'android_local'    //上传到的仓库地址
        clientConfig.publisher.username = "${admin}"		//artifactory 登录的用户名
        clientConfig.publisher.password = "${password}"	    //artifactory 登录的密码
    }
    
    • 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
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115

    执行:gradlew :artifactoryPublish即可发布到私有maven仓库中。

    3. 执行结果

    在这里插入图片描述

    项目地址:https://gitee.com/weizu_cool/hello-plugin-demo.git

    4. 参考

    1. 搭建私有Jfrog artifactory仓库并上传Android Library
    2. com.jfrog.artifactory
    3. 【Android Studio Gradle】使用Artifactory构建本地仓库
  • 相关阅读:
    scipy短时傅里叶分析STFT
    【状语从句练习题】because vs so
    15条重写烂代码的经验!
    知识图谱(6)基于KG构建问答系统
    spring boot 下载resources下的静态文件为流格式
    分布式全局唯一ID生成方案(附源码)
    5.27 picker组件
    php加密解密
    JAVA 0基础 基本数据类型之间的互相转换
    Fiddler抓包工具详解
  • 原文地址:https://blog.csdn.net/qq_26460841/article/details/127948782