当前位置:   article > 正文

Android Studio发布项目到Maven仓库_install.repositories.maveninstaller

install.repositories.maveninstaller

转载仅供本人存档及后续研究使用,请尊重原创。

转载自:https://blog.csdn.net/H_Zhang/article/details/51558800

 

为了方便别人使用我们开发的Android Library,一般我们都会把Android Library打成aar包,并将aar包发布到Maven仓库当中。如果是开源项目就可以把包发布到Maven Central仓库或者JCenter仓库中;如果是公司内部使用,一般公司内部会自己搭建私有Maven仓库,就把包发布到私有Maven仓库当中,以方便别人直接使用。

本文将介绍如何使用Android Studio将项目发布到Maven私有仓库(使用Nexus搭建)以及JCenter中央仓库。


准备工作

首先得有一个Android项目。我以自己GitHub上的一个项目customprogressbar作说明。
项目结构图:


这里写图片描述

 

这个项目有两个module:customprogressbar和example。

  • customprogressbar
    这个module是Android Library类型的,提供自定义进度条的功能

  • example
    这个module使用了customprogressbar提供的功能

此时,由于两个module在同一project下面,example要使用customprogressbar,只需要在example的build.gradle添加如下依赖即可。

example/build.gradle

compile project(':customprogressbar')

下面,我们看如何把customprogressbar发布到Maven仓库当中。首先是使用Nexus搭建的Maven私有仓库。


发布到私有Maven仓库

我们使用Nexus在本地搭建了一个Maven仓库。Nexus使用方法不介绍了,大家自己Google。

启动Nexus,在浏览器输入:http://localhost:8081/nexus/ 即可看到Maven仓库管理界面。


这里写图片描述

 

下面贴上customprogressbar的build.gradle的脚本代码

customprogressbar/build.gradle

  1. apply plugin: 'com.android.library'
  2. android {
  3. compileSdkVersion 23
  4. buildToolsVersion "23.0.2"
  5. defaultConfig {
  6. minSdkVersion 15
  7. targetSdkVersion 23
  8. versionCode 1
  9. versionName "1.0"
  10. }
  11. buildTypes {
  12. release {
  13. minifyEnabled false
  14. proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
  15. }
  16. }
  17. }
  18. dependencies {
  19. compile fileTree(dir: 'libs', include: ['*.jar'])
  20. testCompile 'junit:junit:4.12'
  21. compile 'com.android.support:appcompat-v7:23.3.0'
  22. }
  23. apply from: './nexus-push.gradle'

好,可以看到,基本上都是Android Studio自动生成的代码,只有最后一句不是。最后一句引用了另一个gradle脚本文件,我们的项目发布代码都写在这个脚本文件中。

customprogressbar/nexus-push.gradle

  1. apply plugin: 'maven'
  2. task androidJavadocs(type: Javadoc) {
  3. source = android.sourceSets.main.java.srcDirs
  4. classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
  5. }
  6. task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
  7. classifier = 'javadoc'
  8. from androidJavadocs.destinationDir
  9. }
  10. task androidSourcesJar(type: Jar) {
  11. classifier = 'sources'
  12. from android.sourceSets.main.java.srcDirs
  13. }
  14. artifacts {
  15. archives androidSourcesJar
  16. archives androidJavadocsJar
  17. }
  18. uploadArchives {
  19. repositories {
  20. mavenDeployer {
  21. repository(url: "http://localhost:8081/nexus/content/repositories/releases/") {
  22. authentication(userName: "deployment", password: "deployment123")
  23. }
  24. pom.groupId = 'com.hebut.czh'
  25. pom.artifactId = 'customprogressbar'
  26. pom.version = '0.0.1'
  27. pom.project {
  28. licenses {
  29. license {
  30. name 'The Apache Software License, Version 2.0'
  31. url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
  32. }
  33. }
  34. }
  35. }
  36. }
  37. }

首先需要引用maven插件;然后定义了2个任务:androidSourcesJar和androidJavadocsJar,这两个任务分别用于对Java sources打包和Java doc进行打包;接着我们对uploadArchives.repositories闭包进行一些配置,包括仓库的url地址,上传所需的用户名和密码,以及pom属性。

OK, 脚本编写完之后,在Android Studio的Terminal面板执行如下命令

gradlew uploadArchives

如图所示:


这里写图片描述

 

发布成功后就可以在nexus查看已发布的项目:
这里写图片描述

 

好的,接下来看Example如何使用这个Maven仓库中的aar包。
首先在项目根目录下的build.gradle文件添加仓库url地址。

build.gradle

  1. allprojects {
  2. repositories {
  3. jcenter()
  4. maven {
  5. url "http://localhost:8081/nexus/content/repositories/releases/"
  6. }
  7. }
  8. }

接下来在example的build.gradle中添加依赖即可使用。

/example/build.gradle

compile 'com.hebut.czh:customprogressbar:0.0.1'

 

发布到JCenter仓库

JCenter仓库是由bintray提供并维护,这个仓库是类似Maven中央仓库。只要你能连上Internet,你就可以通过Gradle或者Maven去下载仓库中的依赖包到你自己的项目中。

当然,我们也可以通过Android Studio把aar包发布到JCenter当中,这样就可以让更多的开发者使用我们提供的aar包。

把项目发布到JCenter中需要3步:
1. 注册bintray账户
2. 编写Gradle脚本,把项目发布到你bintray账户下的Maven仓库
3. 同步到JCenter仓库

第1步和第3步都很简单,下面先讲第2步。

同样在customprogressbar的build.gradle文件中编写代码,跟前边相比就一处不一样,这里就贴上不一样的代码。

customprogressbar/build.gradle

apply from: './jcenter-push.gradle'

还是一样,上传的主要脚本代码写在另一个文件当中。由于,bintray在Maven仓库外面又加了一个package层,使用package来对仓库进行管理,所以,上传的方式有点不同。

customprogressbar/jcenter-push.gradle

  1. apply plugin: 'com.jfrog.bintray'
  2. apply plugin: 'com.github.dcendents.android-maven'
  3. def siteUrl = 'https://github.com/codezhanghao/CustomProgressbar'
  4. def gitUrl = 'https://github.com/codezhanghao/CustomProgressbar.git'
  5. group = 'com.hebut.czh'
  6. version = '0.0.1'
  7. install {
  8. repositories.mavenInstaller {
  9. pom {
  10. project {
  11. packaging 'aar'
  12. name 'custom progressbar for android'
  13. url siteUrl
  14. licenses {
  15. license {
  16. name 'The Apache Software License, Version 2.0'
  17. url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
  18. }
  19. }
  20. developers {
  21. developer {
  22. id 'czh'
  23. name 'ZHANG.Hao'
  24. email '809026704@qq.com'
  25. }
  26. }
  27. scm {
  28. connection gitUrl
  29. developerConnection gitUrl
  30. url siteUrl
  31. }
  32. }
  33. }
  34. }
  35. }
  36. task androidJavadocs(type: Javadoc) {
  37. source = android.sourceSets.main.java.srcDirs
  38. classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
  39. }
  40. task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
  41. classifier = 'javadoc'
  42. from androidJavadocs.destinationDir
  43. }
  44. task androidSourcesJar(type: Jar) {
  45. classifier = 'sources'
  46. from android.sourceSets.main.java.srcDirs
  47. }
  48. artifacts {
  49. archives androidSourcesJar
  50. archives androidJavadocsJar
  51. }
  52. Properties properties = new Properties()
  53. properties.load(project.rootProject.file('local.properties').newDataInputStream())
  54. bintray {
  55. user = properties.getProperty("bintray.user")
  56. key = properties.getProperty("bintray.apikey")
  57. configurations = ['archives']
  58. pkg {
  59. version {
  60. name = '0.0.1'
  61. desc = 'custom progress bar 0.0.1'
  62. }
  63. repo = 'maven'
  64. name = 'custom-progressbar'
  65. licenses = ['Apache-2.0']
  66. vcsUrl = gitUrl
  67. publish = true
  68. }
  69. }

这里首先应用了两个插件:com.jfrog.bintray和com.github.dcendents.android-maven。

第一个插件是bintray官网提供的,用来配置bintray的Maven仓库的特有信息,bintray的用户名,apikey,以及提供了一个名为bintrayUpload任务用来上传项目的bintray的Maven仓库,对应代码:

  1. bintray {
  2. ...
  3. ...
  4. }

需要注意的使用,bintray的用户名和密码我放在了local.properties文件里面
local.properties:

  1. sdk.dir=D\:\\develop\\Android\\sdk
  2. bintray.user=czh //bintray的用户名
  3. bintray.apikey=********* //修改成你自己的apikey,可以在bintray官网上查到

第二个插件是我Google出来的,用来配置项目的POM信息的,对应代码:

  1. install {
  2. repositories.mavenInstaller {
  3. ...
  4. ...
  5. }
  6. }

接下来在Android Studio的Terminal面板执行命令:

gradle bintrayUpload

如图所示:


这里写图片描述

 

成功后,可以在bintray网站上的自己的Maven仓库中查看自己的发布的项目:


这里写图片描述

 

至此,第2步就结束了。

至于第3步,将你的bintray下的Maven仓库中的项目include到JCenter仓库中,就点击一个按钮即可。具体就是,点击进入你刚刚上传项目package的详细页当中,在右下角有一个Add to JCenter按钮,点击它,然后写上一些message,最后点击send按钮,就完事了。下面就是等待bintray工作人员进行审核。几个小时后,审核通过,bintray会给你发展站内消息,通知你项目已经include到JCenter当中。

如图所示:


这里写图片描述

 

OK,发布项目到JCenter即成功了。

之后,任何人任何时候,只需要添加一句依赖脚本代码就能直接使用你编写的customprogressbar中的功能。

compile 'com.hebut.czh:customprogressbar:0.0.1@aar'

然后Gradle会自动从JCenter仓库中下载对应的依赖包到本地项目中。


OK,本篇文章到这结束了,主要介绍了使用Android Studio发布项目到私有Maven仓库和JCenter中央仓库。希望可以帮助到你。谢谢观看~~~

---------------------------------------------------------------------------------------------------------------------------

早计划,早准备,早完成。 欢迎关注!交流!Star!

GitHub:https://github.com/wangyang0313

微信公众号:一个灵活的胖子MrWang

简书:https://www.jianshu.com/u/e5e733d79b96  

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/311549?site
推荐阅读
相关标签
  

闽ICP备14008679号