利用Bintray上传你的应用到Maven,Jcenter

首先注册为bintray用户

注册戳这里

点击Profile

接着新建仓库

image-20190110112944441

创建仓库很简单我就不详说了,那么这一步就完成了

仓库创建完成后是以下情况

创建package

在创建好的Repository中点击add New Package

image-20190111111750350

填写好信息

这里要记住填写的信息,之后配置gradle的时候要用到

生成gpg秘钥并且添加

在个人中心profile中找到GPG signing

GPG是一种非对称加密算法,没有安装gpg的同学请看我另一篇将加密的文章

生成gpg命令

1
gpg --gen-key

查看pgp命令

1
gpg --list-keys

没有意外的话你会看到

image-20190110143018296

现在要将key上传到公共的地址才可以,这里提供了一系列key的服务

https://sks-keyservers.net/overview-of-pools.php

上传命令

1
gpg --keyserver hkps.pool.sks-keyservers.net --send-keys xxxxxxxxxx

xxxx是上图中用红色干掉的地方的十六进制后八位!!!否则各大站无法认证

之后导出公钥和私钥

1
2
gpg -a --export tototianhao@gmail.com > public_key_sender.asc
gpg -a --export-secret-key tototianhao@gmail.com > private_key_sender.asc

导出私钥的时候需要输入你在创建时候输入的密码,执行完就可以看见自己导出的公钥和私钥了

然后填入以下页面

image-20190110141319142

添加GPG签名认证

找到刚才新建的项目点击edit

image-20190110150448551

然后勾线gpg认证

image-20190110150702748

其实就是和github的ssh一个意思,非对称加密上传文件

注册maven仓库

https://issues.sonatype.org/首先注册一个账号

注册好后点击左上角的 create,创建新的项目

image-20190110152302060

申请好后会给你发个邮件说明创建成功了

DNS添加记录值

过两分钟后你会发现在你的JIRA中有一条新的评论

1
2
3
4
5
6
7
8
9
central-ossrh Central OSSRH added a comment - 6 minutes ago
Do you own the domain tianhao.info? If so, please verify ownership via one of the following methods:

Add a TXT record to your DNS referencing this JIRA ticket: OSSRH-45456 (Fastest)
Setup a redirect to your Github page (if it does not already exist)
Send an email to central@sonatype.com referencing this issue from a tianhao.info email address
If you do not own this domain, please read:
http://central.sonatype.org/pages/choosing-your-coordinates.html
You may also choose a groupId that reflects your project hosting, in this case, something like io.github.thtianhao or com.github.thtianhao

你申请的groupid必须进行认证,那么在CDN中增加一条Text作为记录值就可以了

记得设置完DNS解析后重新提交哦

添加sonatype用户名密码

然后在我们的bintray中加入sonatype的用户名和密码

image-20190110152813084

完成

Android Stuido配置篇

首先献上bintray提供的android上传的的github

设置插件 项目级gradle

gradel中插入插件

1
2
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.4'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'

然后在gradle中插入你的bintray的用户名和APIKEYAPIKEYbintray的个人信息中可以找到

这里我是写在model下的local.properties中插入(没有的话创建一个)

1
2
bintray.username=xxxxxxxxx
bintray.apikey=xxxxxxxxxx

设置上传文件信息

然后插入你的仓库信息,同样的为了保密,我再local.properties中设置了这些信息

1
2
3
4
5
6
7
8
9
10
11
bintray {
user = bintray.username
key = bintray.apikey
pkg {
repo = bintray.pkg.repo
name = bintray.pkg.name
userOrg = bintray.pkg.userOrg
licenses = bintray.pkg.licenses
vcsUrl = bintray.pkg.vcsUrl
}
}

说下这些参数吧

  • repo :是你bintray的仓库的名称
  • name:是你的包名
  • userOrg:组织 可选项
  • license:执照你的包如果你的包不存在的话就必须设置
  • vcsUrl:你的版本控制url你的包如果你的包不存在的话就必须设置

增加bintray的配置信息

在pkg中下面的闭包中增加version

1
2
3
4
5
6
7
8
9
pkg {
version {
name = '1.0-Final'
desc = 'Gradle Bintray Plugin 1.0 final'
released = new Date()
vcsTag = '1.3.0'
attributes = ['gradle-plugin': 'com.use.less:com.use.less.gradle:gradle-useless-plugin']
}
}
  • name:版本号
  • desc :版本描述
  • released:发布日期
  • vcs:版本控制的tag
  • attributes:可以增加属性

上传到Bintray

提供了三种凡是去上传到Bintry

我相信既然是上传,肯定和仓库相关,那么用maven的方式最好,自动发布到jcenter和mavenCenter,但是也是最麻烦的,需要自定义pom文件,但是用configuration的的方式可以在Bintray中网页添加到Jcenter和MavenCenter就很方便了

咱们暂时不研究MavenCenter,直接用Config的方式实现

利用Configuration的方式上传

首先引用插件

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

配置artifacts信息

这里的version是与你bintray对应的version,直接在gradle中插入

1
version = '1.0.0'

下面的group对应你在JIRA中创建仓库的maven group

1
group = 'info.tianhao'

bintray闭包引用archives

然后在bintray闭包中key = 'bintray_api_key之下添加一行代码

1
2
3
4
5
bintray {
user = 'bintray_user'
key = 'bintray_api_key'
configurations = ['archives']
}

pom信息配置

最后来maven生成pom的代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
install {
repositories.mavenInstaller {
pom {
project {
packaging 'aar'
groupId maven_groupId
artifactId maven_artifactId

// Add your description here
name maven_artifactId
description desc
url gitUrl

// Set your license
licenses {
license {
name maven_licenses_name
url maven_licenses_url
}
}

developers {
developer {
id maven_developers_id
name myName
email myEmail
}
}

scm {
connection gitUrl
developerConnection gitUrl
url myUrl
}
}
}
}
}

生成jar和doc的代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
task sourcesJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier = 'sources'
}

task javadoc(type: Javadoc) {
source = android.sourceSets.main.java.srcDirs
options.encoding = "UTF-8"
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
}

task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}

artifacts {
archives sourcesJar
archives javadocJar
}

至此所有的配置都完成了附上我配置的代码bintray.gradle

我这里是新建了一个名为bintray.gradle的脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
apply plugin: 'com.jfrog.bintray'
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'org.jetbrains.dokka-android'

version ='2.3.6'
group = 'info.tianhao'

Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())

ext {
bintray_pkg_licenses = ["Apache-2.0"]
bintray_pkg_version_name = '1.0.3'
bintray_pkg_version_vcsTag = '1.0.3'
// bintray_pkg_version_attributes = 'Provide Unity3d payment SDK for Android and IOS'

maven_groupId = 'info.tianhao'
maven_artifactId = 'unitybuy'
maven_name = 'UnityBuy'
maven_licenses_name = 'The Apache Software License, Version 2.0'
maven_licenses_url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
maven_developers_id = 'toto000888'
reponame ='UnityPay'
packagename ='gamepay'
myName = 'tianhao'
myEmail = '356831631@gmail.com'
desc = 'Provide Unity3d payment SDK for Android and IOS'
myUrl = 'https://www.tianhao.info'
gitUrl = 'https://github.com/THtianhao'
}

bintray {
user = properties.getProperty("bintray.username")
key = properties.getProperty("bintray.apikey")
configurations = ['archives']
pkg {
repo = reponame //Bintray上面repo的名字
name = packagename //Bintray上面新建的包名字
licenses = bintray_pkg_licenses
websiteUrl = myUrl
vcsUrl = gitUrl
publish = true
publicDownloadNumbers = true
version {
name = bintray_pkg_version_name //这个改变了会自动上传到bintray,称为仓库的新版本
desc = desc
released = new Date()
vcsTag = bintray_pkg_version_vcsTag
gpg {
sign = true
passphrase = '54th55845'
}
}
}
}

install {
repositories.mavenInstaller {
pom {
project {
packaging 'aar'
groupId maven_groupId
artifactId maven_artifactId

// Add your description here
name maven_artifactId
description desc
url gitUrl

// Set your license
licenses {
license {
name maven_licenses_name
url maven_licenses_url
}
}

developers {
developer {
id maven_developers_id
name myName
email myEmail
}
}

scm {
connection gitUrl
developerConnection gitUrl
url myUrl
}
}
}
}
}
task sourcesJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier = 'sources'
}

task javadoc(type: Javadoc) {
source = android.sourceSets.main.java.srcDirs
options.encoding = "UTF-8"
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
}

task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}

artifacts {
archives sourcesJar
archives javadocJar
}

在model中gradle中引用脚本

在modle的最后一行加入脚本

1
apply from: "bintray.gradle"

另一种配置方法

直接在modle中新建一个extext里面的参数对照填写下面两个网页的配置

然后在modle中的gradle引用两个网址

1
2
3
gradle form `https://raw.githubusercontent.com/nuuneoi/JCenter/master/installv1.gradle`

gradle form `https://raw.githubusercontent.com/nuuneoi/JCenter/master/bintrayv1.gradle`

#上传

那么准备好了就上传吧

首先要保证gradlew有权限,如果没有的话执行

1
chorm +x gradlew

生成文档和jar

./gradlew install

1
./gradlew install

那么问题接踵而至

  • Could not find method bintray() for arguments
1
apply plugin: 'com.jfrog.bintray'
  • Could not find method project() for arguments
1
apply plugin: 'com.github.dcendents.android-maven'
  • Execution failed for task ‘:unitybuy:javadoc’.

Javadoc generation failed. Generated Javadoc options file (useful for troubleshooting): ‘/Users/toto/Project/company/unit3D/unitybuy/android/googlePaySDk/unitybuy/build/tmp/javadoc/javadoc.options’

kotlin生成doc文件时候出的错误

那么查看了kotlin的论坛发现一个插件可以对kotlin生成javadoc

dokka

首先在根目录下添加

1
classpath 'org.jetbrains.dokka:dokka-android-gradle-plugin:0.9.17'

然后在modle中添加

1
apply plugin: 'org.jetbrains.dokka-android'

改变task,将原来的

1
2
3
4
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}

改造为

1
2
3
4
5
6
7
8
9
10
task dokkaJavadoc(type: org.jetbrains.dokka.gradle.DokkaTask) {
outputFormat = "javadoc"
outputDirectory = javadoc.destinationDir
}

task generateJavadoc(type: Jar, dependsOn: dokkaJavadoc) {
group = 'jar'
classifier = 'javadoc'
from javadoc.destinationDir
}

并且从bintray.gradle中奖这些生成docjar的文件取出来放到model中,不然找不到插件

1
bintray.gradle

./gradlew bintrayUpload

在终端中执行

1
./gradlew bintrayUpload
  • 问题
1
Could not sign version '1.0.0': HTTP/1.1 401 Unauthorized [message:This resource requires authentication]

肯定是用户名或者apikey错了要不就是gpg密码错了

我的case是在modle里面有一个local.properties我以为是用的这个,其实生效的是项目目录下的local.properties

  • 问题
1
Could not sign version '1.0.0': HTTP/1.1 404 Not Found [message:Repo 'maven' was not found]

repo对应的是项目名称

  • 问题
1
Could not sign version '1.0.0': HTTP/1.1 404 Not Found [message:Package '123' was not found]

  • 问题
1
Could not sign version '1.0.0': HTTP/1.1 400 Bad Request [message:Private key is required, please supply it by using a JSON body or alternatively it can be stored in your Bintray profile]

在Bintray中,gpg私钥没有添加成功

  • 1
    Could not upload to 'https://api.bintray.com/content/toto000888/UnityPay/gamepay/1.0.0/googlePaySDk/unitypay/unspecified/unitypay-unspecified.pom': HTTP/1.1 400 Bad Request [message:Unable to upload files: Maven group, artifact or version defined in the pom file do not match the file path 'googlePaySDk/unitypay/unspecified/unitypay-unspecified.pom']

很多问题,是信息不对导致的

  1. 首先
1
version ='1.0.0'

这个东西是上传url的这个尾巴1.0.0

1
https://api.bintray.com/content/toto000888/UnityPay/gamepay/1.0.0/googlePaySDk/unitypay/2.3.6
  1. 其次
1
group = 'info.tianhao'

这个东西是这个东西是上传url的这个尾巴googlePaySDk(应该是在JIRA创建时候的graopid)

1
https://api.bintray.com/content/toto000888/UnityPay/gamepay/1.0.0/googlePaySDk
  1. 其次在pom中
1
artifactId maven_artifactId

这个东西是上传url的这个尾巴unitypay

1
https://api.bintray.com/content/toto000888/UnityPay/gamepay/1.0.0/googlePaySDk/unitypay
  1. 在bintray中

    1
    2
    version {
    name = bintray_pkg_version_name //这一条的值

这个东西是上传url的这个尾巴unitypay

1
https://api.bintray.com/content/toto000888/UnityPay/gamepay/1.0.0

至此,全部填写正确就好了

googlePaySDk/unitybuy/1.0.3/unitybuy-1.0.3.pom’.

groupId/artifactId/version/maven_artifactId-version.pom

要一一对应

有个问题搞了很长时间

上传url中

https://api.bintray.com/content/toto000888/UnityPay/gamepay/1.0.3/googlePaySDk/unitybuy/2.3.6/unitybuy-2.3.6.pom...

这个googlePaySDk是我的项目名字,一直想换成maven注册的groupid都换不了,查了很长时间在gradle的网站上

查看gradle的源码Groovy

https://github.com/bintray/gradle-bintray-plugin/blob/master/src/main/groovy/com/jfrog/bintray/gradle/tasks/BintrayUploadTask.groovy

发现image-20190111011908946

那么我们把目标转向artifac.path这个属性

最后在

https://docs.gradle.org/current/userguide/maven_plugin.html

发现这么一句话

Maven POM generation

When deploying an artifact to a Maven repository, Gradle automatically generates a POM for it. The groupId, artifactId, version and packaging elements used for the POM default to the values shown in the table below. The dependency elements are created from the project’s dependency declarations.

Table 3. Default Values for Maven POM generation

Maven Element Default Value
groupId project.group
artifactId uploadTask.repositories.mavenDeployer.pom.artifactId (if set) or archiveTask.baseName.
version project.version
packaging archiveTask.extension

其实就是用install命令的时候,Maven生成pom文件时会根据这些配置来生成对应的上传地址。。。。佩服我的毅力。。。

那么就能理解为什么要在根目录下增加这条配置了

1
version ='2.3.6'

我们再加一条

1
group = 'info.tianhao'

执行

1
./gradlew bintrayUpload

终于看到我想要的路径了

1
2
3
4
5
6
7
8
9
Uploading to https://api.bintray.com/content/toto000888/UnityPay/gamepay/1.0.3/info/tianhao/unitybuy/2.3.6/unitybuy-2.3.6.pom...
Uploaded to 'https://api.bintray.com/content/toto000888/UnityPay/gamepay/1.0.3/info/tianhao/unitybuy/2.3.6/unitybuy-2.3.6.pom'.
Uploading to https://api.bintray.com/content/toto000888/UnityPay/gamepay/1.0.3/info/tianhao/unitybuy/2.3.6/unitybuy-2.3.6-javadoc.jar...
Uploaded to 'https://api.bintray.com/content/toto000888/UnityPay/gamepay/1.0.3/info/tianhao/unitybuy/2.3.6/unitybuy-2.3.6-javadoc.jar'.
Uploading to https://api.bintray.com/content/toto000888/UnityPay/gamepay/1.0.3/info/tianhao/unitybuy/2.3.6/unitybuy-2.3.6-sources.jar...
Uploaded to 'https://api.bintray.com/content/toto000888/UnityPay/gamepay/1.0.3/info/tianhao/unitybuy/2.3.6/unitybuy-2.3.6-sources.jar'.
Uploading to https://api.bintray.com/content/toto000888/UnityPay/gamepay/1.0.3/info/tianhao/unitybuy/2.3.6/unitybuy-2.3.6.aar...
Uploaded to 'https://api.bintray.com/content/toto000888/UnityPay/gamepay/1.0.3/info/tianhao/unitybuy/2.3.6/unitybuy-2.3.6.aar'.
BUILD SUCCESSFUL in 38s

上传后的问题

上传后发现文件里面没有包含的aar文件

已经将aar放到了lib里面了还是没有,依赖声明的是

1
implementation(name: 'billing-1.1', ext: 'aar')

换一种方式

1
implementation files('libs/billing-1.1.aar')

就可以打包进去了

同步到Jcenter

上传后我们只是上传到了Bintray,如果想在项目中直接引用的话还不可以

需要上传到Jcenter

image-20190111120128496

提交后提示

1
The version control 1.0.1 returns 404.

原因是VCS 不是一个有效的链接导致的,修改成一个链接即可

参考

bintry文档

gradle-bintray-plugin的git

一个很厉害的翻译仓库

https://github.com/Kotlin/dokka/blob/master/README.md

https://liungkejin.github.io/2016/04/12/Publish-Kotlin-Lib-To-Jcenter.html

https://blog.csdn.net/qq_30034925/article/details/79445072

bintray 插件源码

gradle官方文档

谢谢您的鼓励~