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. 执行结果
4. 参考
- 搭建私有Jfrog artifactory仓库并上传Android Library
- com.jfrog.artifactory
- 【Android Studio Gradle】使用Artifactory构建本地仓库