經(jīng)常上github的同學(xué)可能經(jīng)常看到j(luò)ava開源第三方庫一般都會上傳到j(luò)center埠况,從而方便使用者很方便的在gradle中使用。如
compile 'com.github.yangweigbh:volleyx:1.1.0'
本文就如何上傳本地aar到或者jar包到j(luò)center進(jìn)行介紹
1 jcenter 介紹
jcenter 是 bintray.com 建立的一個maven倉庫, 除了包含maven center所有的依賴庫以外,還包含了更多的依賴庫雏婶,是maven center的一個超集
如何生成和上傳aar
step1
在 bintray.com注冊賬號
step2
注冊完成之后,選擇maven倉庫
step3
在maven倉庫中選擇添加package
step4
填好package信息之后白指,選擇“create package”
step5
這樣你就在bintray的maven倉庫里生成了自己的package倉庫留晚,可以往里面上傳你自己的依賴庫了
2 生成發(fā)布到j(luò)center的產(chǎn)出文件
引入maven-publish
插件
apply plugin: 'maven-publish'
配置要發(fā)布的artifact和pom文件,注意告嘲,maven-publish不會在pom中自動生成aar的依賴文件配置错维,需要在gradle中自行配置
publishing {
publications {
library(MavenPublication) {
groupId "your group id"
artifactId "your artifact name"
version "your artifact version"
// 生成產(chǎn)物的配置
artifact "your artifact file location"
artifact your artifact generate task
//將aar的依賴庫添加到pom文件中
pom.withXml {
def dependenciesNode = asNode().appendNode('dependencies')
//Iterate over the compile dependencies (we don't want the test ones), adding a <dependency> node for each
configurations.compile.allDependencies.each {
if (it.group == null) return
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', it.group)
dependencyNode.appendNode('artifactId', it.name)
dependencyNode.appendNode('version', it.version)
}
}
}
}
}
bintray為gradle編寫了插件憨闰,可以方便的將本地生成的artifact和pom上傳到bintray
buildscript {
....
dependencies {
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7'
}
}
apply plugin: 'com.jfrog.bintray'
加入配置信息
bintray {
user = bintray user name
key = bintray api key //在"edit profile"的"API key"中
publications = [ 'library' ] //上一部分中MavenPublication的列表
pkg {
repo = 'maven' //保留,表示上傳bintray中的maven倉庫
group = "artifact group name"
name = "artifact name"
desc = "description"
licenses = ['Apache-2.0']
websiteUrl = 'https://github.com/yangweigbh/VolleyX'
vcsUrl = 'https://github.com/yangweigbh/VolleyX.git'
labels = ['rxjava', 'volley', 'rxandroid']
publicDownloadNumbers = true
version {
name = "版本號"
desc = "該版本的描述"
}
}
}
3 編譯產(chǎn)出文件并發(fā)布到bintray
./gradlew build bintrayUpload
進(jìn)入bintray上的package頁面需五,可以看到剛才上傳的version鹉动,點(diǎn)擊進(jìn)去后進(jìn)入files tab
可以看到剛才上傳的文件
4 同步bintray到j(luò)center
點(diǎn)擊Linked To
右邊的 add to jenter
,會出現(xiàn)提交到j(luò)enter的申請表
提交后等待一段時間宏邮,當(dāng)你的package出現(xiàn)這個標(biāo)志時泽示,說明你的庫已經(jīng)同步到j(luò)center了
5 Reference
https://inthecheesefactory.com/blog/how-to-upload-library-to-jcenter-maven-central-as-dependency/en
https://www.virag.si/2015/01/publishing-gradle-android-library-to-jcenter/
https://docs.gradle.org/current/userguide/publishing_maven.html