使用Gradle发布项目到JCenter仓库

原文:使用Gradle发布项目到JCenter仓库 

这篇文章介绍通过Gradle把开源项目发布到公共仓库JCenter中,方便你我他的事情,我们都是很懒的嘛。JCenter现在是Android Studio中repositories的默认节点了,之前是Maven的,不过JCenter是兼容Maven的,所以放心使用。步骤基本是按Publishing Gradle Android Library to jCenter Repository这里来的,英文能看的直接看这篇也行。下面我的步骤正式开始,发布到JCenter仓库的是我的项目:BounceProgressBar

申请Bintray账号

Bintray的基本功能类似于Maven Central,一样的我们需要一个账号,Bintray传送门,注册完成后第一步算完成了。

生成项目的JavaDoc和source JARs

简单的说生成的这两样东西就是我们在下一步中上传到远程仓库JCenter上的文件了。这一步需要android-maven-plugin插件,所以我们需要在项目的build.gradle(Top-level build file,项目最外层的build.gradle文件)中添加这个构建依赖,如下:

 
  1. buildscript {
  2.     repositories {
  3.         jcenter()
  4.     }
  5.     dependencies {
  6.         classpath 'com.android.tools.build:gradle:1.0.0'
  7.         classpath 'com.github.dcendents:android-maven-plugin:1.2'
  8.         // NOTE: Do not place your application dependencies here; they belong
  9.         // in the individual module build.gradle files
  10.     }
  11. }
  12. allprojects {
  13.     repositories {
  14.         jcenter()
  15.     }
  16. }

然后在你需要发布的那个module(我这里的即是library)的build.gradle里配置如下内容:

 
  1. apply plugin: 'com.android.library'
  2. apply plugin: 'com.github.dcendents.android-maven'
  3. apply plugin: 'com.jfrog.bintray'
  4. // This is the library version used when deploying the artifact
  5. version = "1.0.0"
  6. android {
  7.     compileSdkVersion 21
  8.     buildToolsVersion "21.1.2"
  9.     resourcePrefix "bounceprogressbar__"    //这个随便填
  10.     defaultConfig {
  11.         minSdkVersion 9
  12.         targetSdkVersion 21
  13.         versionCode 1
  14.         versionName version
  15.     }
  16.     buildTypes {
  17.         release {
  18.             minifyEnabled false
  19.             proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
  20.         }
  21.     }
  22. }
  23. dependencies {
  24.     compile fileTree(dir: 'libs', include: ['*.jar'])
  25.     compile 'com.nineoldandroids:library:2.4.0+'
  26. }
  27. def siteUrl = 'https://github.com/zhengxiaopeng/BounceProgressBar' // 项目的主页
  28. def gitUrl = 'https://github.com/zhengxiaopeng/BounceProgressBar.git' // Git仓库的url
  29. group = "org.rocko.bpb" // Maven Group ID for the artifact,一般填你唯一的包名
  30. install {
  31.     repositories.mavenInstaller {
  32.         // This generates POM.xml with proper parameters
  33.         pom {
  34.         project {
  35.         packaging 'aar'
  36.         // Add your description here
  37.         name 'Android BounceProgressBar Widget' //项目描述
  38.         url siteUrl
  39.         // Set your license
  40.         licenses {
  41.             license {
  42.             name 'The Apache Software License, Version 2.0'
  43.             url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
  44.             }
  45.         }
  46.         developers {
  47.         developer {
  48.         id 'zhengxiaopeng'    //填写的一些基本信息
  49.         name 'Rocko'
  50.         email '[email protected]'
  51.         }
  52.         }
  53.         scm {
  54.         connection gitUrl
  55.         developerConnection gitUrl
  56.         url siteUrl
  57.         }
  58.         }
  59.         }
  60.     }
  61. }
  62. task sourcesJar(type: Jar) {
  63.     from android.sourceSets.main.java.srcDirs
  64.     classifier = 'sources'
  65. }
  66. task javadoc(type: Javadoc) {
  67.     source = android.sourceSets.main.java.srcDirs
  68.     classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
  69. }
  70. task javadocJar(type: Jar, dependsOn: javadoc) {
  71.     classifier = 'javadoc'
  72.     from javadoc.destinationDir
  73. }
  74. artifacts {
  75.     archives javadocJar
  76.     archives sourcesJar
  77. }
  78. Properties properties = new Properties()
  79. properties.load(project.rootProject.file('local.properties').newDataInputStream())
  80. bintray {
  81.     user = properties.getProperty("bintray.user")
  82.     key = properties.getProperty("bintray.apikey")
  83.     configurations = ['archives']
  84.     pkg {
  85.         repo = "maven"    
  86.         name = "BounceProgressBar"    //发布到JCenter上的项目名字
  87.         websiteUrl = siteUrl
  88.         vcsUrl = gitUrl
  89.         licenses = ["Apache-2.0"]
  90.         publish = true
  91.     }
  92. }

配置好上述后需要在你的项目的根目录上的local.properties文件里(一般这文件需gitignore,防止泄露账户信息)配置你的bintray账号信息,your_user_name为你的用户名,your_apikey为你的账户的apikey,可以点击进入你的账户信息里再点击Edit即有查看API Key的选项,把他复制下来。

 
  1. bintray.user=your_user_name
  2. bintray.apikey=your_apikey

Rebuild一下项目,顺利的话,就可以在module里的build文件夹里生成相关文件了。这一步为止,就可以把你项目生成到本地的仓库中了,Android Studio中默认即在Android\sdk\extras\android\m2repository这里,所以我们可以通过如下命令(Windows中,可能还需要下载一遍Gradle,之后就不需要了)执行生成:

 
  1. gradlew install

上传到Bintray

上传到Bintray需要gradle-bintray-plugin的支持,所以在最外层的build.gradle里添加构建依赖:

 
  1. buildscript {
  2.     repositories {
  3.         jcenter()
  4.     }
  5.     dependencies {
  6.         classpath 'com.android.tools.build:gradle:1.0.0'
  7.         classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.0'
  8.         classpath 'com.github.dcendents:android-maven-plugin:1.2'
  9.         // NOTE: Do not place your application dependencies here; they belong
  10.         // in the individual module build.gradle files
  11.     }
  12. }
  13. allprojects {
  14.     repositories {
  15.         jcenter()
  16.     }
  17. }

Rebuild一下,然后执行如下命令(Windows中)完成上传:

 
  1. gradlew bintrayUpload

上传完成即可在Bintray网站上找到你的Repo,我们需要完成最后一步工作,申请你的Repo添加到JCenter。可以进入这个页面,输入你的项目名字点击匹配到的项目,然后写一写Comments再send即可,然后就等管理员批准了,我是大概等了40分钟,然后网站上会给你一条通过信息,然后就OK了,大功告成。

成功后就可以在其它项目里方便的使用你发布的项目了:

 
  1. dependencies {
  2.     compile 'org.rocko.bpb:library:1.0.0'
  3. }

End

不明白的可以再看看这两篇文章或者留言。

相关文章:
使用Gradle发布Android开源项目到JCenter
Publishing Gradle Android Library to jCenter Repository

猜你喜欢

转载自blog.csdn.net/qq_36838191/article/details/82659410