文章参考:一步一步教你在JCenter发布开源库
本文不包含注册Bintray的环节。
首先,请移步github查看maven和Jfrog Bintray的最新版本:
Maven:https://github.com/dcendents/android-maven-gradle-plugin
Jfrog Bintray:https://github.com/bintray/gradle-bintray-plugin
在项目根目录下的build.gradle中配置依赖插件:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'
/**
* 下面两项建议按照github上的配置
*/
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
mavenCentral() //添加maven库支持
}
}
然后,在需要开源的module中,添加下面两行:
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'
并配置task(直接搬运的开头博客的示例):
//项目主页
def siteUrl = 'https://github.com/backkomyoung/CircleView'
//项目的git地址
def gitUrl = 'https://github.com/backkomyoung/CircleView.git'
//发布到JCenter上的项目名字
def libName = "CircleView"
//发布到组织名称名字,必须填写
group = "me.songning.CircleView"
// 版本号,下次更新是只需要更改版本号即可
version = "1.0.0"
//上面配置后上传至JCenter后的编译路径是这样的: compile 'me.songning.CircleView:library:1.0.0'
//生成源文件
task sourcesJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier = 'sources'
}
//生成Javadoc文档
task javadoc(type: Javadoc) {
source = android.sourceSets.main.java.srcDirs
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
}
//文档打包成jar
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
//拷贝javadoc文件
task copyDoc(type: Copy) {
from "${buildDir}/docs/"
into "docs"
}
//上传到JCenter所需要的源码文件
artifacts {
archives javadocJar
archives sourcesJar
}
// 配置maven库,生成POM.xml文件
install {
repositories.mavenInstaller {
// This generates POM.xml with proper parameters
pom {
project {
packaging 'aar'
//项目描述,随意填
name 'A colorful circle view with text.'
url siteUrl
licenses {
license {
//开源协议
name 'The Apache Software License, Version 2.0'
url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
developers {
developer {
//开发者的个人信息
id 'backkomyoung'
name 'SongNing'
email 'backkomyoung@gmail.com'
}
}
scm {
connection gitUrl
developerConnection gitUrl
url siteUrl
}
}
}
}
}
//上传到JCenter
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
bintray {
user = properties.getProperty("bintray.user") //读取 local.properties 文件里面的 bintray.user
key = properties.getProperty("bintray.apikey") //读取 local.properties 文件里面的 bintray.apikey
configurations = ['archives']
pkg {
//这里的repo值必须要和你创建Maven仓库的时候的名字一样
repo = "maven"
//发布到JCenter上的项目名字
name = libName
//项目描述
desc = 'A colorful circle view with text.'
websiteUrl = siteUrl
vcsUrl = gitUrl
licenses = ["Apache-2.0"]
publish = true
}
}
javadoc {
options{
//如果你的项目里面有中文注释的话,必须将格式设置为UTF-8,不然会出现乱码
encoding "UTF-8"
charSet 'UTF-8'
author true
version true
links "http://docs.oracle.com/javase/7/docs/api"
}
}
接下来在local.properties文件中添加bintray的账号信息:
//uer和key
bintray.user=username
bintray.apikey=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
最后,在Android Studio提供的命令行工具中依次键入下面的命令:
gradlew clean
gradlew install
gradlew bintrayUpload
当每一步都执行成功后,即完成开源库的上传。在bintray的repo中,可以找到我们创建的开源库。点击 Add to JCenter,并在接下的页面中直接submit,等待数小时后,我们的库就开源了。
最后再分享一个遇到的问题:
在执行javadoc的task时,出现了错误,原因是使用UTF8编码和未知标记(在注释的时候,使用了自定义的标记),解决方案可以点击参考。