【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
            
        }
    }
}

结合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 登录的密码
}

执行: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构建本地仓库
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梦否

文章对你有用?不妨打赏一毛两毛

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值