多個(gè)開源項(xiàng)目Bintray一鍵發(fā)布環(huán)境部署
我們發(fā)布到Bintray上共享的一般是一些庫(kù)大州,而不是完整的App,而這些庫(kù)是依附在我的主項(xiàng)目之中,如果我們主項(xiàng)目只維護(hù)一個(gè)共享庫(kù),那沒(méi)什么問(wèn)題峰弹,但維護(hù)多個(gè)開源庫(kù)呢?不規(guī)劃一下打包發(fā)布的流程芜果,那么就會(huì)浪費(fèi)我更很多的時(shí)間在打包發(fā)布上鞠呈。截至至撰文時(shí),筆者的ProjectX主項(xiàng)目已經(jīng)管理維護(hù)者16個(gè)開源庫(kù)右钾,不規(guī)劃一套打包方案蚁吝,那么妥妥的能把筆者累死。
基礎(chǔ)Plugin載入
需要實(shí)現(xiàn)自動(dòng)化發(fā)包舀射,就必須載入gradle-bintray-plugin與android-maven-gradle-plugin(點(diǎn)擊鏈接查看最新版本號(hào)窘茁,使用最新版本插件)。載入方式有兩種:
- 傳統(tǒng)方式
dependencies {
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.1'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
}
- 新型方式(Gradle 2.1)
plugins {
id "com.jfrog.bintray" version "1.7.1"
id "com.github.dcendents.android-maven" version "1.5"
}
使用新型方式導(dǎo)入的gradle-bintray-plugin會(huì)提交不成功脆烟,不知AS更新以后是否解決山林,但是筆者出錯(cuò)的版本是1.7.1,新版本沒(méi)出來(lái)前gradle-bintray-plugin還是建議使用傳統(tǒng)方式邢羔,android-maven-gradle-plugin可以選擇新型方式驼抹。
部署方案
- 在庫(kù)根目錄(不是項(xiàng)目根目錄)創(chuàng)建bintray.gradle文件,文件內(nèi)容(可以直接拷貝給其他項(xiàng)目使用):
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'
// load properties
Properties properties = new Properties()
File localPropertiesFile = project.file("local.properties");
if (localPropertiesFile.exists()) {
properties.load(localPropertiesFile.newDataInputStream())
}
File projectPropertiesFile = project.file("project.properties");
if (projectPropertiesFile.exists()) {
properties.load(projectPropertiesFile.newDataInputStream())
}
// read properties
def projectName = properties.getProperty("project.name")
def projectDesc = properties.getProperty("project.desc")
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 projectIssueTrackerUrl = properties.getProperty("project.issueTrackerUrl")
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")
// This generates POM.xml with proper parameters
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
}
}
}
}
task androidJavadocs(type: Javadoc) {
source = android.sourceSets.main.java.source
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
options {
encoding "UTF-8"
charSet 'UTF-8'
author true
version projectVersionName
links "http://docs.oracle.com/javase/7/docs/api"
title projectName
}
}
task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
from androidJavadocs.destinationDir
classifier = 'javadoc'
}
task androidSourcesJar(type: Jar) {
from android.sourceSets.main.java.source
classifier = 'sources'
}
if (JavaVersion.current().isJava8Compatible()) {
allprojects {
tasks.withType(Javadoc) {
options.addStringOption('Xdoclint:none', '-quiet')
}
}
}
artifacts {
archives androidSourcesJar
archives androidJavadocsJar
}
// bintray configuration
bintray {
user = bintrayUser
key = bintrayApikey
group = projectGroupId
configurations = ['archives']
pkg {
repo = "maven"
name = projectName
websiteUrl = projectSiteUrl
vcsUrl = projectGitUrl
desc = projectDesc
issueTrackerUrl = projectIssueTrackerUrl
licenses = ["Apache-2.0"]
publish = true
publicDownloadNumbers = true
}
}
- 在庫(kù)根目錄創(chuàng)建project.properties用于配置項(xiàng)目信息(不同項(xiàng)目需要配置不同值):
#project
project.name=BaseTabStrip
project.groupId=am.widget
project.artifactId=basetabstrip
project.packaging=aar
project.desc=項(xiàng)目描述
project.siteUrl=https://github.com/AlexMofer/ProjectX/tree/master/basetabstrip
project.gitUrl=https://github.com/AlexMofer/ProjectX.git
project.issueTrackerUrl=https://github.com/AlexMofer/ProjectX/issues
- 在庫(kù)根目錄創(chuàng)建local.properties用于配置bintray登錄信息(可以直接拷貝給其他項(xiàng)目使用拜鹤,需要加入git忽略列表):
##必須Git忽略此文件框冀,其包含隱私信息
#bintray
bintray.user=你的bintray賬戶
bintray.apikey=API Key
#developer
developer.id=*******
developer.name=***
developer.email=*******@****.com
- git添加bintray.gradle與project.properties,忽略local.properties :
# Local configuration file (sdk path, etc)
local.properties
- 在庫(kù)的build.gradle最后加入:
//apply from: "bintray.gradle"
發(fā)布
將要提交的開源庫(kù)的build.gradle中的 apply from: "bintray.gradle" 去掉注釋署惯,保證 versionName 是你想要發(fā)布的左驾,那么控制臺(tái)輸入gradlew bintrayUpload就可以了,成功以后再將 apply from: "bintray.gradle" 注釋掉就不會(huì)干擾其他開源項(xiàng)目的提交了极谊。
注意
- Git一定要忽略掉local.properties文件
- 發(fā)布完畢以后诡右,build.gradle中的 apply from: "bintray.gradle" 要記得注釋掉,否則會(huì)干擾其他開源項(xiàng)目提交
- bintray.gradle不單單是發(fā)布處理轻猖,還包括中文注釋亂碼處理