將本身的開源項目提交到JCenter

開始

最近本身寫了一個開源控件,是有關標籤流控件的(從服務器端獲取標籤,而後將標籤自適應的放進一個容器裏)。如今控件寫完了,本身就想將它提交到Maven的中心倉庫或者是JCenter。但是本身對maven不熟,而且用Android Studio寫的工程提交到Maven的中心倉庫確實比較繁瑣,因此決定仍是用JCenter。html

申請帳號

關與項目提交到JCenter,網上有個教程寫的很好,我基本上也是參照上面來作的,可是途中也遇到了一些坑,因此以爲仍是有必要敘述一下的。首先你要作的是申請Bintray帳號,它是JCenter的託管商,註冊完成以後,你須要來到這個界面,記住你的帳戶名和API key,我通常將它放在local.properties文件中。由於咱們待會會用到,作完這些後,你的第一步差很少即完成了。java

//local.properties
sdk.dir=/Applications/ADT/sdk
bintray.user = fyales
bintray.apikey = *************************

配置工程的build.gradle文件和你要提交的Module的build.gradle文件

下面咱們來看build.gradle文件:android

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.1.0'
        classpath 'com.github.dcendents:android-maven-plugin:1.2'
        classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.0'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

這裏咱們用的倉庫是JCenter而不是MavenCentral。而且咱們須要引入官方提供的兩個插件android-maven-plugin和gradle-bintrayl-plugin,這樣便於咱們提交項目到JCenter.git

接下來就是配置Module的build.gradle文件了github

apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'
// 提交到倉庫中的版本號
version = "1.0.0"

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.2'
}

def siteUrl = 'https://github.com/fyales/tagcloud'      // 項目的主頁
def gitUrl = 'https://github.com/fyales/tagcloud.git'   // Git倉庫的url
group = "com.fyales.android"
install {
    repositories.mavenInstaller {
        pom {
            project {
                packaging 'aar'
                name 'Android TagCloud'    //項目描述
                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 'fyales'        //填寫的一些基本信息
                        name 'fyales'
                        email 'fyales@gmail.com'
                    }
                }
                scm {
                    connection gitUrl
                    developerConnection gitUrl
                    url siteUrl
                }
            }
        }
    }
}

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))
}
task javadocJar(type: Jar, dependsOn: javadoc) {
    classifier = 'javadoc'
    from javadoc.destinationDir
}
artifacts {
    archives javadocJar
    archives sourcesJar
}

Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
bintray {
    user = properties.getProperty("bintray.user")
    key = properties.getProperty("bintray.apikey")
    configurations = ['archives']
    pkg {
        repo = "maven"
        name = "TagCloud"    //發佈到JCenter上的項目名字
        websiteUrl = siteUrl
        vcsUrl = gitUrl
        licenses = ["Apache-2.0"]
        publish = true
    }
}

這串代碼主要作了三件事web

  • 生成JavaDoc
  • 生成Jar
  • 配置咱們項目的信息

在這裏我要提醒一下,生成javadoc和jar是必選選項。一開始我認爲作這些沒有必要,因此沒有寫入生成javaDoc和jar的命令,因而,我收到了bintray的官方郵件(郵件中是另一個項目):apache

Bintray (bintray) has sent you a direct message from Bintray:

Hi, 

Jcenter hosts java applications that follows maven convention.
In addition to the .aar and pom files in the path: /com/fyales/parser/1.0.0
, your version should include a sources jar, and optionally a javadoc jar. 

Your files should be under a maven path layout.
(see https://bintray.com/docs/usermanual/uploads/uploads_includingyourpackagesinjcenter.html)

Once those files are added, we'll be glad to include your package in JCenter.

Regards,
Bintray Support

在這邊還有一個坑我寫的javadoc命令一開始運行失敗,這是由於我喜歡在個人註釋這樣寫api

@author fyales
@date

由於javadoc裏面並無@date,因此會解析失敗,你們也要注意一下。。。。。。。服務器

作完這些後,就能夠運行下面的命令將你的項目提交到Bintray了app

./gradlew bintrayUpload

最後的步驟

提交完成以後,你就能夠申請你的項目到JCenter,點擊Include My Package,在彈出的界面輸入你的項目名稱肯定就能夠了,當審覈經過後,你就能夠直接用你本身項目了.

dependencies {
	compile 'com.fyales.android:library:1.0.0'
}

其餘注意點

build失敗(失敗緣由Cannot call getBootClasspath() before setTargetInfo() is called.)

解決方案:將gradle版本升級到1.2

總結

JCenter相較於Maven Central仍是很方便的,另外就是在軟件開發過程當中,有時候也要知其然,也要知其因此然。

口號:Make things interesting!

參考

使用Gradle發佈項目到JCenter倉庫

相關文章
相關標籤/搜索