轉(zhuǎn)載請標(biāo)明出處
本文出自HCY的博客
概述
將自己寫的庫上傳到Jcenter或者M(jìn)aven提供給自己或者別人使用悬嗓,在構(gòu)建項(xiàng)目的時(shí)候只要寫上一行如下類似的引用代碼即可引用自己的庫。是不是很酷的一件事情,本文一步一步教你如何將自己的代碼發(fā)布到JCenter上罢荡,帶你裝逼帶你飛。
compile 'com.android.support:recyclerview-v7:23.3.0'
本人使用的軟件環(huán)境
軟件名稱 | 版本號 |
---|---|
操作系統(tǒng) | Windows 7 |
Android Studio | 2.2 |
Gradle | 2.14.1 |
com.android.tools.build:gradle | 1.5.0 |
com.github.dcendents:android-maven-gradle-plugin | 1.3.0 |
com.jfrog.bintray.gradle:gradle-bintray-plugin | 1.6.0 |
上傳到代碼到Jcenter
1.到Bintray官網(wǎng)注冊賬號
到Bintray官網(wǎng)注冊賬號煤伟,填寫相關(guān)信息瞭吃,填寫的Organization Id很重要,一定要記起來到時(shí)候上傳代碼的時(shí)候要用杜跷。另外郵箱也不要填寫比如qq.com舰绘、163.com的郵箱,會無法注冊的葱椭。最好是填寫GMail郵箱地址捂寿。注冊完成之后到你填寫的郵箱里面去激活Bintray賬號即可。
2.創(chuàng)建代碼倉庫
點(diǎn)擊上圖所示的“Add NewRepository”按鈕孵运,添加代碼倉庫秦陋,點(diǎn)擊后就會跳轉(zhuǎn)到下圖的界面。
在上圖的Name輸入框中輸入代碼倉庫的名稱治笨,名字可以隨便寫驳概,但是一定要記得,因?yàn)楹竺嫔蟼鞔a的時(shí)候還要用旷赖,Type選擇Maven即可顺又。點(diǎn)擊Create即可創(chuàng)建代碼倉庫,若創(chuàng)建成功則會跳轉(zhuǎn)到下面的頁面等孵。
如上圖紅色框框所示代碼倉庫的路徑就是Organization Id/Repository Name稚照,本人創(chuàng)建的代碼倉庫即為(coolcode/maven),另外要記住的信息是上圖右上角的Bintray User(Bintray的用戶名),因?yàn)楹竺嫔蟼鞔a要用到果录。
3.獲取賬號對應(yīng)的API KEY
點(diǎn)擊個(gè)人頭像進(jìn)入到個(gè)人信息主界面上枕,點(diǎn)擊Edit按鈕即可進(jìn)入到上圖所示的界面。點(diǎn)擊“API KEY”弱恒,輸入Bintray本賬號的登陸密碼辨萍,即可查看到本賬號的API KEY。到此為止我們已經(jīng)獲取到了如下四條重要的信息返弹,請牢記:
- Organization Id(組織ID)
- Repository Name(倉庫名稱)
- Bintray User(Bintray賬號的用戶名)
- Bintray API Key(Bintray賬號的API KEY)
4.在Project的build.gradle添加如下信息
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.5.0'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3' //用于打包Maven所需文件
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.6' //用于上傳Maven生成的文件到Bintray
}
}
allprojects {
repositories {
jcenter()
}
}
5.在Module的根目錄下創(chuàng)建bintrayUpload.gradle文件
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'
//加載屬性文件
Properties properties = new Properties()
File localPropertiesFile = project.file("bintray.properties");
if (localPropertiesFile.exists()) {
properties.load(localPropertiesFile.newDataInputStream())
}
File projectPropertiesFile = project.file("project.properties");
if (projectPropertiesFile.exists()) {
properties.load(projectPropertiesFile.newDataInputStream())
}
//讀取屬性
def projectRepositoryName = properties.getProperty("project.repositoryName")
def projectName = properties.getProperty("project.name")
def projectGroupId = properties.getProperty("project.groupId")
def projectArtifactId = properties.getProperty("project.artifactId")
def projectVersionName = android.defaultConfig.versionName
def projectPackaging = properties.getProperty("project.packaging")
def projectSiteUrl = properties.getProperty("project.siteUrl")
def projectGitUrl = properties.getProperty("project.gitUrl")
def developerId = properties.getProperty("developer.id")
def developerName = properties.getProperty("developer.name")
def developerEmail = properties.getProperty("developer.email")
def bintrayUser = properties.getProperty("bintray.user")
def bintrayApikey = properties.getProperty("bintray.apiKey")
def bintrayOrganizationId = properties.getProperty("bintray.organizationId");
def javadocName = properties.getProperty("javadoc.name")
/*
*這句代碼一定要加否則會出現(xiàn)如下錯誤
* Could not upload to 'https://api.bintray.com/content/coolcode/maven/LibUiBase/1.0.0/CommonLibrary/LibUiBase/1.0.0/LibUiBase-1.0.0.pom': HTTP/1.1 400 Bad Request [
message:Unable to upload files: Maven group, artifact or version defined in the pom file do not match the file path 'CommonLibrary/LibUiBase/1.0.0/LibUiBase-1.0.0.p
om']
* */
group = projectGroupId
// 配置生成POM.xml文件的參數(shù)
install {
repositories.mavenInstaller {
pom {
project {
name projectName
groupId projectGroupId
artifactId projectArtifactId
version projectVersionName
packaging projectPackaging
url projectSiteUrl
licenses {
license {
name 'The Apache Software License, Version 2.0'
url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
developers {
developer {
id developerId
name developerName
email developerEmail
}
}
scm {
connection projectGitUrl
developerConnection projectGitUrl
url projectSiteUrl
}
}
}
}
}
//生成sources.jar
task sourcesJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier = 'sources'
}
task javadoc(type: Javadoc) {
source = android.sourceSets.main.java.srcDirs
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
}
//生成javadoc.jar
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
artifacts {
archives javadocJar
archives sourcesJar
}
//javadoc的配置
javadoc {
options {
encoding "UTF-8"
charSet 'UTF-8'
author true
version projectVersionName
links "http://docs.oracle.com/javase/7/docs/api"
title javadocName
}
}
/*
*userOrg為bintray賬號信息里面的Organization Id
*repo為你創(chuàng)建的倉庫名稱
* 如果上述兩個(gè)字段寫錯了锈玉,則會出現(xiàn)下面類似的錯誤
*Could not create package 'huangxuanheng/maven/fragmentstack': HTTP/1.1 404 Not Found [message:Repo 'maven' was not found]
*
*
* */
bintray {
user = bintrayUser
key = bintrayApikey
configurations = ['archives']
pkg {
userOrg = bintrayOrganizationId
repo = projectRepositoryName
name = projectName
websiteUrl = projectSiteUrl
vcsUrl = projectGitUrl
licenses = ["Apache-2.0"]
publish = true
}
}
6.在Module的build.gradle中應(yīng)用上面創(chuàng)建的bintrayUpload.gradle文件,添加如下代碼
apply from: "bintrayUpload.gradle"
7.在Module的根目錄下創(chuàng)建第5步要讀取的配置文件
創(chuàng)建bintray.properties用于配置bintray和開發(fā)者信息
#配置bintray賬號相關(guān)信息
#bintray用戶名,不是登陸郵箱,是個(gè)人中心右上角顯示的名字
bintray.user=huangcaiyuan5678
#bintray的ApiKey
bintray.apiKey=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
#bintray的Organization Id
bintray.organizationId=coolcode
#配置開發(fā)者信息
#昵稱
developer.id=huangcaiyuan
#姓名
developer.name=huangcaiyuan
#郵箱
developer.email=huangcaiyuan1234@gmail.com
創(chuàng)建project.properties用于配置項(xiàng)目信息
#project
#倉庫名稱义起,就是在bintray官網(wǎng)建立的倉庫的名稱
project.repositoryName=maven
#項(xiàng)目名稱
project.name=LibUiBase
#項(xiàng)目組id
project.groupId=com.hcy.library
#項(xiàng)目id,一般同project.name
project.artifactId=LibUiBase
#打包類型
project.packaging=aar
#項(xiàng)目官方網(wǎng)站地址
project.siteUrl=https://github.com/Money888
#項(xiàng)目git地址
project.gitUrl=https://github.com/Money888
#生成的javadoc名稱
javadoc.name=LibUiBase
8.在Terminal窗口下收入如下指令上傳到Bintray
gradlew install
gradlew bintrayUpload
若出現(xiàn)BUILD SUCCESSFUL則說明成功上傳到了Bintray嘲玫。
9.添加Package到JCenter
進(jìn)入到Bintray網(wǎng)站,找到剛才上傳的項(xiàng)目并扇,點(diǎn)擊右下角的“Add To JCenter”按鈕去团。
然后填寫項(xiàng)目描述點(diǎn)擊“Send”提交審核即可,如果審核成功穷蛹,它會給你發(fā)送一封站內(nèi)信通知你土陪。
10.引用成功
//引用方式如下
compile '項(xiàng)目組ID:項(xiàng)目名稱:項(xiàng)目版本號'
//引用示例
compile 'com.android.support:recyclerview-v7:23.3.0'
常見問題及解決方案
Could not upload to 'https://api.bintray.com/content/coolcode/maven/LibUiBase/1.0.0/CommonLibrary/LibUiBase/1.0.0/LibUiBase-1.0.0.pom': HTTP/1.1 400 Bad Request [
message:Unable to upload files: Maven group, artifact or version defined in the pom file do not match the file path 'CommonLibrary/LibUiBase/1.0.0/LibUiBase-1.0.0.p
om']
解決方案:
出現(xiàn)這個(gè)問題的原因有可能是project.properties里面的project.name的屬性值與project.artifactId不一致,或者是bintrayUpload.gradle里面的group = projectGroupId腳本沒有寫肴熏,嘗試修改上面兩種可能出現(xiàn)該問題的腳本鬼雀。
Could not create package 'coolcode/maven/LibUIBase': HTTP/1.1 404 Not Found [message:Repo 'maven' was not found]
解決方案:
出現(xiàn)這個(gè)問題的原因可能是名為maven的倉庫(不一定是叫maven,名字只要和bintray網(wǎng)站建立倉庫時(shí)的名稱一致即可)沒有建立蛙吏,或者建立了倉庫源哩,但是名稱不叫maven,又或者組織名稱不叫coolcode鸦做,因?yàn)轫?xiàng)目上傳的路徑為:組織ID/倉庫名稱/項(xiàng)目名励烦,所以檢查組織ID和倉庫名稱是否正確即可解決問題。