简介
Gradle 整合 GitHub Actions 自动化发布 Java 项目至 Maven 仓库。文章内是将项目发布至 sonatype 提供的仓库中。
证书
生成
gpg --full-generate-key
查看证书id
gpg --list-secret-keys --keyid-format SHORT
/Users/lake/.gnupg/pubring.kbx
------------------------------
sec rsa2048/7BDDF87A 2020-07-24 [SC]
xxxxxxx.....
证书发布
gpg --keyserver hkps.pool.sks-keyservers.net --send-keys 7BDDF87A
Gradle 项目配置
group 'com.dounine'
version '1.0.4'
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "com.github.jengelman.gradle.plugins:shadow:4.0.2"
}
}
apply plugin: "com.github.johnrengelman.shadow"
apply plugin: 'signing'
apply plugin: 'scala'
apply plugin: 'maven-publish'
task sourcesJar(type: Jar) {
from sourceSets.main.allJava
classifier = 'sources'
}
task javadocJar(type: Jar) {
from javadoc
classifier = 'javadoc'
}
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
artifact sourcesJar
artifact javadocJar
pom {
name = 'spark-sql-datasource'
description = 'spark sql datasource'
url = 'https://github.com/dounine/spark-sql-datasource'
licenses {
license {
name = 'The Apache License, Version 2.0'
url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
developers {
developer {
id = 'lake'
name = 'lake'
email = 'amwoqmgo@gmail.com'
}
}
scm {
connection = 'scm:git:git://github.com/dounine/spark-sql-datasource.git'
developerConnection = 'scm:git:ssh://github.com/dounine/spark-sql-datasource.git'
url = 'https://github.com/dounine/spark-sql-datasource'
}
}
}
}
repositories {
maven {
def releasesRepoUrl = "https://oss.sonatype.org/service/local/staging/deploy/maven2/"
def snapshotsRepoUrl = "https://oss.sonatype.org/content/repositories/snapshots/"
url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
credentials {
username System.getenv("MAVEN_USERNAME") ?: MAVEN_USERNAME
password System.getenv("MAVEN_PASSWORD") ?: MAVEN_PASSWORD
}
}
}
}
sourceCompatibility = 1.8
repositories {
mavenLocal()
mavenCentral()
maven { url "http://repo.hortonworks.com/content/repositories/releases/" }
}
task copyJars(type: Copy) {
from configurations.runtime
into new File('build/libs/lib')
}
compileJava.dependsOn copyJars
shadowJar {
zip64 true
}
signing {
sign publishing.publications.mavenJava
}
ext {
flinkVersion = "1.8-SNAPSHOT"
hbaseVersion = "2.0.0"
slf4jVersion = "1.7.25"
}
dependencies {
compile 'org.scala-lang:scala-library:2.12.12'
compile group: 'org.apache.spark', name: 'spark-sql_2.12', version: '3.0.0'
compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.47'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
Github Action
密钥导出
gpg --export-secret-keys 7BDDF87A > /tmp/secring.gpg
将密钥进行base64编码
base64 /tmp/secring.gpg > /tmp/secring.gpg.b64
name | value |
---|---|
MAVEN_PASSWORD | sonatype仓库密码 |
MAVEN_USERNAME | sonatype仓库用户名 |
SIGNING_KEY_ID | GPG证书ID |
SIGNING_PASSWORD | GPG密码 |
SIGNING_SECRET_KEY_RING_KEY | GPG文件密钥base64编码 |
将SIGNING_SECRET_KEY_RING_KEY值设置为上面的secring.gpg.b64文件内容
项目/.github/workflows/main.yaml
name: Publish package to the Maven Central Repository
on:
release:
types: [created]
jobs:
publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Java
uses: actions/setup-java@v1
with:
java-version: 1.8
- name: Decode gpg
run: |
echo "${{ secrets.SIGNING_SECRET_KEY_RING_KEY }}" > ~/secring.gpg.b64
base64 -d ~/secring.gpg.b64 > ~/secring.gpg
- name: Publish package
run: gradle publish -Psigning.keyId=${{ secrets.SIGNING_KEY_ID }} -Psigning.password=${{ secrets.SIGNING_PASSWORD }} -Psigning.secretKeyRingFile=$(echo ~/secring.gpg)
env:
MAVEN_USERNAME: ${{ secrets.MAVEN_USERNAME }}
MAVEN_PASSWORD: ${{ secrets.MAVEN_PASSWORD }}
在github右边创建Release版本即可自动打包上传
查看action的打包运行情况
最后登录 sonatype 把上传的项目进行close > release 即可发布