Gradle + GitHub actions automatically publish project to Maven warehouse

Walking on the equator 2020-11-11 11:47:16
gradle github actions automatically publish


Abstract

The core of the article is to use Gradle Integrate GitHub Actions Automated release Java Project to Maven Warehouse . The article is to publish the project to sonatype In the warehouse provided . If you need to publish automatically , Or you need to synchronize the project to Maven Central warehouse, please read it carefully .

Preparation

  1. Can be synchronized Maven Tickets to the central warehouse , stay Issues Sonatype Last application ;
  2. OpenPGP certificate , Need to synchronize to a public server ;
  3. One Java Project;
  4. GitHub.

Please refer to the section on synchronized tickets link , The article mainly describes the content of the certificate .

OpenPGP certificate

Because I use Windows The operating system uses Gpg4win Tool generates synchronization certificate . If you're using Mac perhaps Linux The operating system can use other GPG Tools such as GnuPG.

install Gpg4win

Download address Gpg4win.

Generate Certificate

$ gpg --full-generate-key

Run the above command to generate RSA Certificate .

C:\Users\admin>gpg --full-generate-key
gpg (GnuPG) 2.2.23; Copyright (C) 2020 Free Software Foundation, Inc.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
... Omit the part ...
public and secret key created and signed.
pub rsa3072 2020-11-10 [SC] [expires: 2021-11-10]
449B75F00B2DA482AB8D03E8493DA88E2B89E19F
uid kk70-blog (blog) <kevinz@weghst.com>
sub rsa3072 2020-11-10 [E] [expires: 2021-11-10]

Browse for certificates

have access to gpg --list-secret-keys --keyid-format SHORT Command to query the certificate list .

C:\Users\admin>gpg --list-secret-keys --keyid-format SHORT
C:/Users/admin/AppData/Roaming/gnupg/pubring.kbx
------------------------------------------------
sec rsa3072/2B89E19F 2020-11-10 [SC] [expires: 2021-11-10]
449B75F00B2DA482AB8D03E8493DA88E2B89E19F
uid [ultimate] kk70-blog (blog) <kevinz@weghst.com>
ssb rsa3072/6B7BF2DA 2020-11-10 [E] [expires: 2021-11-10]

Issue certificate

gpg --keyserver hkps.pool.sks-keyservers.net --send-keys 2B89E19F

Use gpg Command to publish the certificate to the public server 2B89E19F Replace with your own certificate keyid. Can be found in http://keys.gnupg.net/ Websites search for their own certificates .

Gradle Project configuration

To configure Gradle plug-in unit

plugins {
id "maven-publish"
id "signing"
}

To use Gradle Publishing depends on Maven Warehouse you need at least maven-publish plug-in unit . If you need to publish dependencies to Maven The central warehouse also needs to use signing The plug-in verifies the signature of the dependent file .

Gradle Publish To configure

ext {
isReleasedVersion = !project.version.endsWith("-SNAPSHOT")
}
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
artifact sourcesJar
artifact javadocJar
pom {
description = "Yein Chaos Core"
scm {
connection = "scm:git:git@github.com:kevin70/chaos.git"
developerConnection = "scm:git:ssh://github.com/kevin70/chaos.git"
url = "https://github.com/kevin70/chaos"
}
}
}
}
repositories {
maven {
credentials {
username findProperty("ossrhUsername") ?: System.getenv("OSSRH_USERNAME")
password findProperty("ossrhPassword") ?: System.getenv("OSSRH_PASSWORD")
}
if (!isReleasedVersion) {
url "https://oss.sonatype.org/content/repositories/snapshots"
} else {
url "https://oss.sonatype.org/service/local/staging/deploy/maven2"
}
}
}
}
  • isReleasedVersion Commit the snapshot version separately from the official version to a different Maven Warehouse ;
  • publishing.publications.mavenJava Dependent resources that need to be synchronized ;

    • artifact sourcesJar Need to release the source code jar package ;
    • artifact javadocJar Need to be released javadoc jar package ;
    • pom Customize pom.xml The contents of the document .
  • publishing.repositories Released warehouse configuration ;

    • maven.credentials Maven Authentication information of the warehouse ;
    • maven.url Maven The warehouse URL.

Gradle Singing To configure

signing {
sign publishing.publications.mavenJava
}

Will need to publish content signature https://docs.gradle.org/current/userguide/signing_plugin.html.

By default Signatory credentials Signature authentication method .

Github Actions To configure

First, in the GitHub Project > Settings > Secrets Configure some necessary parameters in the .

Secret Parameters

All of the above parameters will be followed by GitHub Workflow Used in .

  • OSSRH_USERNAME yes sonatype Login user name of ;
  • OSSRH_PASSWORD yes sonatype Login password for ;
  • SIGNING_KEY_ID yes GPG Certificate ID;
  • SIGNING_SECRET_KEY_RING_FILE yes GPG The secret key of the certificate is BASE64 code .

obtain SIGNING_KEY_ID

Use gpg --list-secret-keys --keyid-format SHORT Command acquisition SIGNING_KEY_ID.

$ gpg --list-secret-keys --keyid-format SHORT
C:\Users\admin>gpg --list-secret-keys --keyid-format SHORT
C:/Users/admin/AppData/Roaming/gnupg/pubring.kbx
------------------------------------------------
sec rsa3072/2B89E19F 2020-11-10 [SC] [expires: 2021-11-10]
449B75F00B2DA482AB8D03E8493DA88E2B89E19F
uid [ultimate] kk70-blog (blog) <kevinz@weghst.com>
ssb rsa3072/6B7BF2DA 2020-11-10 [E] [expires: 2021-11-10]

among 2B89E19F by SIGNING_KEY_ID.

obtain SIGNING_SECRET_KEY_RING_FILE

Export the secret key to a file secring.gpg.

$ gpg --export-secret-keys 2B89E19F > secring.gpg

To make the secret key file binary, we need to encode the content as BASE64. Because I used Windows So the following command I'm in Git Bash Running in , If you use it Mac or Linux You can also run the following commands directly .

$ base64 secring.gpg > secring.gpg.b64

Will encode the file secring.gpg.b64 The content in is set in Secrets in .

️ Because the certificate I generated does not have a password set , So there's a lack of passphrase To configure , If your certificate has a password, it needs to be added passphrase Configuration of .

GitHub Secrets Setup completed , Next write GitHub Action Workflow.

GitHub Action Workflow

name: Chaos CI with Gradle
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
# function JDK To configure
- name: Set up JDK 11
uses: actions/setup-java@v1
with:
java-version: 11
# Gradle Cache configuration
- name: Cache Gradle packages
uses: actions/cache@v2
with:
path: ~/.gradle/caches
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle') }}
restore-keys: ${{ runner.os }}-gradle
# to gradlew Document Authorization
- name: Grant execute permission for gradlew
run: chmod +x gradlew
# Build the project
- name: Build with Gradle
run: ./gradlew build
# Decode the secret key and place the file ~/.gradle/secring.gpg
- name: Decode
run: |
echo "${{secrets.SIGNING_SECRET_KEY_RING_FILE}}" > ~/.gradle/secring.gpg.b64
base64 -d ~/.gradle/secring.gpg.b64 > ~/.gradle/secring.gpg
# Publish the project
- name: Publish
run: ./gradlew publish -Psigning.keyId=${{secrets.SIGNING_KEY_ID}} -Psigning.password= -Psigning.secretKeyRingFile=$(echo ~/.gradle/secring.gpg)
env:
OSSRH_USERNAME: ${{secrets.OSSRH_USERNAME}}
OSSRH_PASSWORD: ${{secrets.OSSRH_PASSWORD}}

When the task is finished, go to Maven Confirm whether the release has been successful in the warehouse .


Project links

https://github.com/kevin70/chaos

kk70 Personal blog

版权声明
本文为[Walking on the equator]所创,转载请带上原文链接,感谢

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云