将本身的开源项目提交到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仓库

相关文章
相关标签/搜索