Gradle发布项目到JCenter仓库

作为一名程序员,可以把自己写的代码共享给其他开发者,那是一件多么自豪的事情。看完下面的内容,你也会很轻松的拥有该项技能。

申请Bintray账号

需要在Bintray注册一个账号。

生成项目的JavaDoc和source JARs

1.上传到JCenter仓库需要android-maven-plugin插件,需要我们在自己项目中配置,在最外层build.gradle构建依赖

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.0.0-beta5'

        classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'
        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
    }
}

如图所示:

cm1

2.在需要发布的那个module的build.gradle里配置如下内容:

apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'

version = "2.0.2"

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"
    resourcePrefix "circle_"    //这个随便填

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 23
        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:23.1.1'
}

def siteUrl = 'https://github.com/HpWens/CircleMenu' // 项目的主页
def gitUrl = 'https://github.com/HpWens/CircleMenu.git' // Git仓库的url
group = "com.github.ws.circlemenu" // Maven Group ID for the artifact,一般填你唯一的包名

install {
    repositories.mavenInstaller {
        // This generates POM.xml with proper paramters
        pom {
            project {
                packaging 'aar'

                //添加项目描述
                name 'CircleMenuLayout for Android'
                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 'wens'
                        name 'ws'
                        email '182*****675@163.com'
                    }
                }

                scm {
                    connection gitUrl
                    developerConnection gitUrl
                    url siteUrl
                }
            }
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.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))
    options.encoding = "UTF-8"
}

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相关信息
bintray {
    //读取Bintray帐号和密码。
    //一般的为了保密和安全性,在项目的local.properties文件中添加两行句话即可:
    //bintray.user=username
    //bintray.apikey=apikey
    user = properties.getProperty("bintray.user")
    key = properties.getProperty("bintray.apikey")

    configurations = ['archives']

    pkg {
        repo = "maven"//上传的中央仓库名称
        name = "CircleMenuLayout"//上传的项目的名字
        websiteUrl = siteUrl
        vcsUrl = gitUrl
        labels = ['aar', 'android', 'CircleMenuLayout']
        licenses = ["Apache-2.0"]
        publish = true  //是否发布
    }
}

上述的local.properties文件在项目的根目录,用于配置bintray账号信息,这个文件主要是防止泄露bintray账号信息。

bintray.apikey=******
bintray.user=******

apikey的值在:

cm2

上述文件配置完成后,先Rebuild Project一下项目,然后在Terminal运行 gradlew install 命令。 此时你会发现在项目的.idea文件夹下有一个gradle.xml文件,此文件就是该项目相应的gradle格式配置,同时在所要发布的xxx-module/build/outputs/aar文件夹下有已生成aar文件以及在xxx-module/build/poms文件夹下的pom-default.xml(对应maven格式)。

上传到Bintray

同样在Terminal在运行gradlew bintrayUpload 命令上将项目上传到Bintray平台上。

发布到JCenter

进入到你的bintray,在左下角会看到你刚刚提交的项目,点击进入

cm3

点击【Add to JCenter】,然后等待审核。审核成功后就可以在你的项目中使用:

dependencies {
    compile 'com.github.ws.circlemenu:circlemenu:2.0.2'
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值