发布开源代码到jcenter

栏目: 软件资讯 · 发布时间: 4年前

内容简介:在平时的开发中,经常会写一些通用的代码,为了方便自己后续使用,也为了方便更多的人,大都会选择发布到github或者jcenter等一些开源库。这篇文章从头开始介绍怎么把开源代码发布到jcenter,并在项目中使用。包括注册账号,发布,发布后使用以及发布中一些常见的问题。为了方便介绍,选择通过一款早期开源的项目来介绍。

在平时的开发中,经常会写一些通用的代码,为了方便自己后续使用,也为了方便更多的人,大都会选择发布到github或者jcenter等一些开源库。

这篇文章从头开始介绍怎么把开源代码发布到jcenter,并在项目中使用。包括注册账号,发布,发布后使用以及发布中一些常见的问题。

代码准备

为了方便介绍,选择通过一款早期开源的项目来介绍。

  • 项目地址:

    https://github.com/bihe0832/Android-HttpURLConnection

  • 文件结构

    ├── README.md
      │
      ├── advanced:com.bihe0832:http-advanced 源码
      │
      ├── app:Demo 源码
      │
      ├── common:com.bihe0832:http-common 源码
      │
      └── utils:com.bihe0832:utils 源码
  • 补充说明

    接下来我们将会介绍怎么把 utilscommonadvanced 发布到jcenter并在Demo中引用

注册账号

官网地址:

注册账号并创建仓库:

  • 注册账号

    打开官网首页,点击下图中红框中的 Sign Up Here

    发布开源代码到jcenter

    填写邮箱等信息,创建账号

    发布开源代码到jcenter

    账号创建以后,会自动给使用的邮箱发送一封确认邮件,前往邮箱验证账号。

  • 创建仓库

    由于目前我们发布的大部分开源项目都是maven格式的,因此我们先新建一个maven的源码仓库。点击下图中的 Add New Repository

    发布开源代码到jcenter

    如下图填写仓库信息以后,点击create以后,仓库即可创建成功,这里name选择 maven 是因为我们后面发布脚本使用了第三方插件,需要按照他的规范设置。

    发布开源代码到jcenter

  • 获取API Key

    API Key 主要是在发布代码时使用,因此我们提前获取好,按下图提示点击右上角的用户名,然后点击 Edit Profile

    发布开源代码到jcenter

    进入编辑页以后,按照下图提示,复制API Key到剪贴板

    发布开源代码到jcenter

发布代码库并使用

配置发布插件

  • 在根目录添加发布插件的相关依赖

    buildscript {  
          repositories {  
              jcenter()  
          }  
          dependencies {  
              classpath 'com.novoda:bintray-release:0.9.1'  
          }  
      }   
    
      allprojects {  
          repositories {  
              jcenter()  
          }  
      }
  • 在需要发布的module添加开源库相关的配置

    //应用发布使用的插件
      apply plugin: 'com.novoda.bintray-release' 
    
      //添加发布相关的配置 
      publish {  
        userOrg = 'bihe0832' //用户名  
        groupId = 'com.bihe0832.test'//代码库路径  
        artifactId = 'utils-demo'//项目名称  
        publishVersion = '1.0.1'//版本号  
        desc = 'the base utils for zixie'//项目描述
        website = 'https://github.com/bihe0832/Android-HttpURLConnection'//项目对应网站 
      }

    按照上面的配置发布的插件,在最终被引用时,形式为: api 'com.bihe0832.test:utils-demo:1.0.0'

提交代码

在项目根目录运行下面的命令:

./gradlew clean build bintrayUpload -PbintrayUser=USERNAME -PbintrayKey=API_KEY  -PdryRun=false

其中:USERNAME为登录用用户名,API_KEY为上面提到的API Key,具体事例如下:

➜  Android-HttpURLConnection git:(master)  ./gradlew clean build bintrayUpload -PbintrayUser=bihe0832 -PbintrayKey=d***d  -PdryRun=false
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=1g; support was removed in 8.0
Starting a Gradle Daemon (subsequent builds will be faster)

> Configure project :utils

> Task :utils:lint
Ran lint on variant release: 0 issues found
Ran lint on variant debug: 0 issues found

……

> Task :utils:bintrayUpload
Uploading to https://api.bintray.com/content/bihe0832/maven/utils-sdsd/1.0.0/com/bihe0832/test/utils-demo/1.0.0/utils-demo-1.0.0-sources.jar...
Uploaded to 'https://api.bintray.com/content/bihe0832/maven/utils-sdsd/1.0.0/com/bihe0832/test/utils-demo/1.0.0/utils-demo-1.0.0-sources.jar'.
Uploading to https://api.bintray.com/content/bihe0832/maven/utils-sdsd/1.0.0/com/bihe0832/test/utils-demo/1.0.0/utils-demo-1.0.0-javadoc.jar...
Uploaded to 'https://api.bintray.com/content/bihe0832/maven/utils-sdsd/1.0.0/com/bihe0832/test/utils-demo/1.0.0/utils-demo-1.0.0-javadoc.jar'.
Uploading to https://api.bintray.com/content/bihe0832/maven/utils-sdsd/1.0.0/com/bihe0832/test/utils-demo/1.0.0/utils-demo-1.0.0.aar...
Uploaded to 'https://api.bintray.com/content/bihe0832/maven/utils-sdsd/1.0.0/com/bihe0832/test/utils-demo/1.0.0/utils-demo-1.0.0.aar'.
Uploading to https://api.bintray.com/content/bihe0832/maven/utils-sdsd/1.0.0/com/bihe0832/test/utils-demo/1.0.0/utils-demo-1.0.0.pom...
Uploaded to 'https://api.bintray.com/content/bihe0832/maven/utils-sdsd/1.0.0/com/bihe0832/test/utils-demo/1.0.0/utils-demo-1.0.0.pom'.

Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/4.10.1/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 22s
56 actionable tasks: 53 executed, 3 up-to-date
➜  Android-HttpURLConnection git:(master) ✗

此时我们的代码就已经成功提交到了 bintray.com ,在maven的页面已经可以查到,此时其实已经可以在代码中使用:

引用项目

此时其实我们的项目已经可以在代码中使用

发布开源代码到jcenter

在根目录添加开源库仓库的相关依赖,对应的url在上图的右上角:

buildscript {  
    repositories {  
        maven { url "https://dl.bintray.com/bihe0832/maven" }
    }  
}   

allprojects {  
    repositories {  
        maven { url "https://dl.bintray.com/bihe0832/maven" }
    }  
}

在对应项目添加依赖,具体的依赖方法可以参考上图左下角:

compile 'com.bihe0832.test:utils-demo:1.0.0'

发布到jcenter

可以看到上面虽然我们已经可以使用了,但是其实是发布到一个自己的私有仓库,但是日常开发中我们使用的库都是发布到jcenter,接下来就把我们的仓库同步到jcenter

如下图,进入项目以后,点击右上角的actions,然后选择 Add to Jcenter

发布开源代码到jcenter

在弹框中输入简单的项目介绍即可,接下来就是短暂的等待,一般几个小时内就可以收到jcenter的回复。之后就可以将上面的引用开源库仓库的相关依赖的地址修改为jcenter了,如下:

buildscript {  
    repositories {  
        maven { url "https://dl.bintray.com/bihe0832/maven" }
    }  
}   

allprojects {  
    repositories {  
        maven { url "https://dl.bintray.com/bihe0832/maven" }
    }  
}

常见问题:

  1. 界面不一致:

    很多人表示看到的教程等等上面的图都是下面的版本,发现和自己登录以后并不一致,这是因为bintray改版了,可以通过点击下面红框中的按钮在新旧两种风格中切换。

    发布开源代码到jcenter

  2. 提交代码报错,提示:HTTP/1.1 401 Unauthorized [message:This resource requires authentication]

    请检查执行 ./gradlew clean build bintrayUpload -PbintrayUser=USERNAME -PbintrayKey=API_KEY -PdryRun=false 时使用的用户名和API Key是否正确,一般是因为这个原因。

  3. 提交代码报错,提示:HTTP/1.1 409 Conflict [message:Unable to upload files: An artifact with the path ‘com/bihe0832/test/utils-demo/1.0.0/utils-demo-1.0.0-sources.jar’ already exists]

    错误事例:

    * What went wrong:
     Execution failed for task ':utils:bintrayUpload'.
     > Could not upload to 'https://api.bintray.com/content/bihe0832/maven/utils-demo/1.0.0/com/bihe0832/test/utils-demo/1.0.0/utils-demo-1.0.0-sources.jar': HTTP/1.1 409 Conflict [message:Unable to upload files: An artifact with the path 'com/bihe0832/test/utils-demo/1.0.0/utils-demo-1.0.0-sources.jar' already exists]
    
     * Try:
     Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
    
     * Get more help at https://help.gradle.org
    
     Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.
     Use '--warning-mode all' to show the individual deprecation warnings.
     See https://docs.gradle.org/4.10.1/userguide/command_line_interface.html#sec:command_line_warnings
    
     BUILD FAILED in 7s
     56 actionable tasks: 53 executed, 3 up-to-date

    这种问题很明显,远程已经存在当前版本,升级版本号以后重试即可

  4. 发布到jcenter失败,提示:The version control XXX returns 404.

    这种问题一般是因为不是自动创建的代码库,而是在提交代码之前已经手动创建了代码库,然后VCS填写错误,如下图:

    发布开源代码到jcenter

    修改为项目对应的正确的版本控制地址即可。


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

C语言程序开发范例宝典

C语言程序开发范例宝典

2010-1 / 59.00元

《C语言程序开发范例宝典》全面介绍了应用C语言进行开发的各种技术和技巧,全书共分12章,内容包括基础知识、指针、数据结构、算法、数学应用、文件操作、库函数应用、图形图像、系统调用、加解密与安全性、游戏、综合应用等。全书共提供300个实例,每个实例都突出了其实用性。 《C语言程序开发范例宝典》既可作为C程序的初学者学习用书,也可作为程序开发人员、相关培训机构老师和学生的参考用书。一起来看看 《C语言程序开发范例宝典》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

在线进制转换器
在线进制转换器

各进制数互转换器

MD5 加密
MD5 加密

MD5 加密工具