本文主要介紹如何一個由gradle構建的項目部署到Maven Central.
網上大部分都是介紹如何將由maven構建的項目部署到Maven Central。與Gradle相關的比較少。
申請賬號
前往 sonatype申請賬號。
申請完,Create Issue。
按照這個模板填。
這一塊比較簡單,網上教程也比較多。
Create Issue結束后,官方會需要你證明你擁有相對應的domain。
證明有以下3個途徑:
- Add a TXT record to your DNS referencing this JIRA ticket: OSSRH-44681 (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 ... email address
證明完畢之后,你就可以發布包了。
你就可以做下面幾件事了:
- Deploy snapshot artifacts into repository https://oss.sonatype.org/cont...
- Deploy release artifacts into the staging repository https://oss.sonatype.org/serv...
- Promote staged artifacts into repository 'Releases'
- Download snapshot and release artifacts from group https://oss.sonatype.org/cont...
- Download snapshot, release and staged artifacts from staging group https://oss.sonatype.org/cont...
構建Gradle
下面主要內容基於 官方英文教程,加上一些個人構建時候的一些收獲。
build.gralde 文件修改
引入plugin
apply plugin: 'maven'
apply plugin: 'signing'
```
task javadocJar(type: Jar) {
classifier = 'javadoc'
from javadoc
}
task sourcesJar(type: Jar) {
classifier = 'sources'
from sourceSets.main.allSource
}
artifacts {
archives javadocJar, sourcesJar
}
```
引入UploadArchives task
引入UploadArchives
這個task,記住更改里面的個人相關信息。
其中有ossrhUsername
和ossrhPassword
這兩個變量是定義在gradle.properties
中的。
uploadArchives {
repositories {
mavenDeployer {
beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }
repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") {
authentication(userName: ossrhUsername, password: ossrhPassword)
}
snapshotRepository(url: "https://oss.sonatype.org/content/repositories/snapshots/") {
authentication(userName: ossrhUsername, password: ossrhPassword)
}
pom.project {
name 'Example Application'
packaging 'jar'
// optionally artifactId can be defined here
description 'A application used as an example on how to set up
pushing its components to the Central Repository . '
url 'http://www.example.com/example-application'
scm {
connection 'scm:git:git@github.com:username/project.git'
developerConnection 'scm:git:git@github.com:username/project.git'
url 'https://github.com/username/project'
}
licenses {
license {
name 'The Apache License, Version 2.0'
url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
developers {
developer {
id 'manfred'
name 'Manfred Moser'
email 'manfred@sonatype.com'
}
}
}
}
}
}
```
編寫gradle.properties
主要是將一些認證信息填在這里。(這些信息不要加入到版本管理中)。
```
以下3個信息怎么來下一章節來講
signing.keyId=YourKeyId
signing.password=YourPublicKeyPassword
signing.secretKeyRingFile=PathToYourKeyRingFile
ossrhUsername=your-jira-id 你在sonatype申請的賬號的用戶名
ossrhPassword=your-jira-password 你在sonatype申請的賬號的密碼
```
生成GPG加密信息
windows中可以安裝gpg4win來生成相關信息。但是我個人在windows10中並沒有能夠打開。
所以我使用了WSL來生成相關信息。如果你的系統是Linux也可以。
- 執行
gpg --gen-key
, 按照提示的信息填入密碼,用戶名等信息,這些信息記錄下來。這里填入的密碼就是上面gradle.properties
中的signing.password
。
-
執行gpg --list-keys
, 可以看到
``` /root/.gnupg/pubring.gpg pub 2048R/B98765 2018-12-08 uid sub 2048R/A123456 ```
- 第一行便是對應的公鑰文件位置,
pug
后面的B98765
便是public key Id,這個id也就是上面gradle.properties
中的signing.keyId
- 執行
gpg --keyserver hkp://pool.sks-keyservers.net --recv-keys B98765
將公鑰發送到hkp://pool.sks-keyservers.net
。
- 記錄下
/root/.gnupg/
中secring.png
的位置,這個位置便是上面gradle.properties
中的signing.secretKeyRingFile
的值。
發布過程
當上述步驟全部完成時,可以直接執行gradle uploadArchives
。
發布Snapshot版本
如果你的版本是snapshot
的,你可以直接在https://oss.sonatype.org/content/repositories/snapshots
中看到你的包。
發布Release版本
如果你的版本是release
版本。
登錄https://oss.sonatype.org/#welcome
,選擇Staging Repositories
,然后在右邊用groupId
去搜索。
這樣會找到你的項目。選中你的項目close然后confirm。過一會再來尋找一次該構建,點擊Release
在Confirm。過一會就應該能在https://oss.sonatype.org/content/groups/public
中看到你的項目了。
注意點
這里Close可能觸發一些錯誤,可以點擊下方的Activity來查看錯誤的原因。一般來說,可能是並沒有javadoc和source導致的。
原文地址: