Introduction
本指南介绍如何创建将 Java 包发布到 GitHub Packages 和 Maven Central 仓库的工作流程。 通过单个工作流程,您可以将包发布到一个或多个仓库。
警告
本指南中使用的示例指旧版 OSSRH 服务。 请参阅 Maven 中央存储库文档中“发布”。
Prerequisites
We recommend that you have a basic understanding of workflow files and configuration options. For more information, see Writing workflows.
For more information about creating a CI workflow for your Java project with Gradle, see Building and testing Java with Gradle.
You may also find it helpful to have a basic understanding of the following:
- 使用 Apache Maven 注册表
- Store information in variables
- Using secrets in GitHub Actions
- Automatic token authentication
About package configuration
The groupId
and artifactId
fields in the MavenPublication
section of the build.gradle file create a unique identifier for your package that registries use to link your package to a registry. This is similar to the groupId
and artifactId
fields of the Maven pom.xml file. For more information, see the Maven Publish Plugin in the Gradle documentation.
The build.gradle file also contains configuration for the distribution management repositories that Gradle will publish packages to. Each repository must have a name, a deployment URL, and credentials for authentication.
Publishing packages to the Maven Central Repository
Each time you create a new release, you can trigger a workflow to publish your package. The workflow in the example below runs when the release
event triggers with type created
. The workflow publishes the package to the Maven Central Repository if CI tests pass. For more information on the release
event, see 触发工作流的事件.
You can define a new Maven repository in the publishing block of your build.gradle file that points to your package repository. For example, if you were deploying to the Maven Central Repository through the OSSRH hosting project, your build.gradle could specify a repository with the name "OSSRH"
.
plugins { ... id 'maven-publish' } publishing { ... repositories { maven { name = "OSSRH" url = "https://oss.sonatype.org/service/local/staging/deploy/maven2/" credentials { username = System.getenv("MAVEN_USERNAME") password = System.getenv("MAVEN_PASSWORD") } } } }
plugins {
...
id 'maven-publish'
}
publishing {
...
repositories {
maven {
name = "OSSRH"
url = "https://oss.sonatype.org/service/local/staging/deploy/maven2/"
credentials {
username = System.getenv("MAVEN_USERNAME")
password = System.getenv("MAVEN_PASSWORD")
}
}
}
}
With this configuration, you can create a workflow that publishes your package to the Maven Central Repository by running the gradle publish
command. In the deploy step, you’ll need to set environment variables for the username and password or token that you use to authenticate to the Maven repository. For more information, see Using secrets in GitHub Actions.
# 此工作流使用未经 GitHub 认证的操作。 # 它们由第三方提供,并受 # 单独的服务条款、隐私政策和支持 # 文档。 # GitHub 建议将操作固定到提交 SHA。 # 若要获取较新版本,需要更新 SHA。 # 还可以引用标记或分支,但该操作可能会更改而不发出警告。 name: Publish package to the Maven Central Repository on: release: types: [created] jobs: publish: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Set up Java uses: actions/setup-java@v4 with: java-version: '11' distribution: 'temurin' - name: Setup Gradle uses: gradle/actions/setup-gradle@af1da67850ed9a4cedd57bfd976089dd991e2582 # v4.0.0 - name: Publish package run: ./gradlew publish env: MAVEN_USERNAME: ${{ secrets.OSSRH_USERNAME }} MAVEN_PASSWORD: ${{ secrets.OSSRH_TOKEN }}
# 此工作流使用未经 GitHub 认证的操作。
# 它们由第三方提供,并受
# 单独的服务条款、隐私政策和支持
# 文档。
# GitHub 建议将操作固定到提交 SHA。
# 若要获取较新版本,需要更新 SHA。
# 还可以引用标记或分支,但该操作可能会更改而不发出警告。
name: Publish package to the Maven Central Repository
on:
release:
types: [created]
jobs:
publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Java
uses: actions/setup-java@v4
with:
java-version: '11'
distribution: 'temurin'
- name: Setup Gradle
uses: gradle/actions/setup-gradle@af1da67850ed9a4cedd57bfd976089dd991e2582 # v4.0.0
- name: Publish package
run: ./gradlew publish
env:
MAVEN_USERNAME: ${{ secrets.OSSRH_USERNAME }}
MAVEN_PASSWORD: ${{ secrets.OSSRH_TOKEN }}
此工作流程执行以下步骤:
-
检出项目仓库的副本。
-
设置 Java JDK。
-
设置 Gradle 环境。
gradle/actions/setup-gradle
操作负责工作流运行之间的缓存状态,并提供所有 Gradle 执行的详细摘要。 -
Executes the Gradle
publish
task to publish to theOSSRH
Maven repository. TheMAVEN_USERNAME
environment variable will be set with the contents of yourOSSRH_USERNAME
secret, and theMAVEN_PASSWORD
environment variable will be set with the contents of yourOSSRH_TOKEN
secret.For more information about using secrets in your workflow, see Using secrets in GitHub Actions.
Publishing packages to GitHub Packages
Each time you create a new release, you can trigger a workflow to publish your package. The workflow in the example below runs when the release
event triggers with type created
. The workflow publishes the package to GitHub Packages if CI tests pass. For more information on the release
event, see 触发工作流的事件.
You can define a new Maven repository in the publishing block of your build.gradle that points to GitHub Packages. In that repository configuration, you can also take advantage of environment variables set in your CI workflow run. You can use the GITHUB_ACTOR
environment variable as a username, and you can set the GITHUB_TOKEN
environment variable with your GITHUB_TOKEN
secret.
每当工作流中的作业开始时,GITHUB_TOKEN
机密都会设置为存储库的访问令牌。 应在工作流文件中设置此访问令牌的权限,以授予 contents
权限的读取访问权限,并授予 packages
权限的写入访问权限。 有关详细信息,请参阅“Automatic token authentication”。
For example, if your organization is named "octocat" and your repository is named "hello-world", then the GitHub Packages configuration in build.gradle would look similar to the below example.
plugins { ... id 'maven-publish' } publishing { ... repositories { maven { name = "GitHubPackages" url = "https://maven.pkg.github.com/octocat/hello-world" credentials { username = System.getenv("GITHUB_ACTOR") password = System.getenv("GITHUB_TOKEN") } } } }
plugins {
...
id 'maven-publish'
}
publishing {
...
repositories {
maven {
name = "GitHubPackages"
url = "https://maven.pkg.github.com/octocat/hello-world"
credentials {
username = System.getenv("GITHUB_ACTOR")
password = System.getenv("GITHUB_TOKEN")
}
}
}
}
With this configuration, you can create a workflow that publishes your package to GitHub Packages by running the gradle publish
command.
# 此工作流使用未经 GitHub 认证的操作。 # 它们由第三方提供,并受 # 单独的服务条款、隐私政策和支持 # 文档。 # GitHub 建议将操作固定到提交 SHA。 # 若要获取较新版本,需要更新 SHA。 # 还可以引用标记或分支,但该操作可能会更改而不发出警告。 name: Publish package to GitHub Packages on: release: types: [created] jobs: publish: runs-on: ubuntu-latest permissions: contents: read packages: write steps: - uses: actions/checkout@v4 - uses: actions/setup-java@v4 with: java-version: '11' distribution: 'temurin' - name: Setup Gradle uses: gradle/actions/setup-gradle@af1da67850ed9a4cedd57bfd976089dd991e2582 # v4.0.0 - name: Publish package run: ./gradlew publish env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# 此工作流使用未经 GitHub 认证的操作。
# 它们由第三方提供,并受
# 单独的服务条款、隐私政策和支持
# 文档。
# GitHub 建议将操作固定到提交 SHA。
# 若要获取较新版本,需要更新 SHA。
# 还可以引用标记或分支,但该操作可能会更改而不发出警告。
name: Publish package to GitHub Packages
on:
release:
types: [created]
jobs:
publish:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
with:
java-version: '11'
distribution: 'temurin'
- name: Setup Gradle
uses: gradle/actions/setup-gradle@af1da67850ed9a4cedd57bfd976089dd991e2582 # v4.0.0
- name: Publish package
run: ./gradlew publish
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
此工作流程执行以下步骤:
-
检出项目仓库的副本。
-
设置 Java JDK。
-
设置 Gradle 环境。
gradle/actions/setup-gradle
操作负责工作流运行之间的缓存状态,并提供所有 Gradle 执行的详细摘要。 -
Executes the Gradle
publish
task to publish to GitHub Packages. TheGITHUB_TOKEN
environment variable will be set with the content of theGITHUB_TOKEN
secret. Thepermissions
key specifies the access that theGITHUB_TOKEN
secret will allow.For more information about using secrets in your workflow, see Using secrets in GitHub Actions.
Publishing packages to the Maven Central Repository and GitHub Packages
You can publish your packages to both the Maven Central Repository and GitHub Packages by configuring each in your build.gradle file.
Ensure your build.gradle file includes a repository for both your GitHub repository and your Maven Central Repository provider.
For example, if you deploy to the Central Repository through the OSSRH hosting project, you might want to specify it in a distribution management repository with the name
set to OSSRH
. If you deploy to GitHub Packages, you might want to specify it in a distribution management repository with the name
set to GitHubPackages
.
If your organization is named "octocat" and your repository is named "hello-world", then the configuration in build.gradle would look similar to the below example.
plugins { ... id 'maven-publish' } publishing { ... repositories { maven { name = "OSSRH" url = "https://oss.sonatype.org/service/local/staging/deploy/maven2/" credentials { username = System.getenv("MAVEN_USERNAME") password = System.getenv("MAVEN_PASSWORD") } } maven { name = "GitHubPackages" url = "https://maven.pkg.github.com/octocat/hello-world" credentials { username = System.getenv("GITHUB_ACTOR") password = System.getenv("GITHUB_TOKEN") } } } }
plugins {
...
id 'maven-publish'
}
publishing {
...
repositories {
maven {
name = "OSSRH"
url = "https://oss.sonatype.org/service/local/staging/deploy/maven2/"
credentials {
username = System.getenv("MAVEN_USERNAME")
password = System.getenv("MAVEN_PASSWORD")
}
}
maven {
name = "GitHubPackages"
url = "https://maven.pkg.github.com/octocat/hello-world"
credentials {
username = System.getenv("GITHUB_ACTOR")
password = System.getenv("GITHUB_TOKEN")
}
}
}
}
With this configuration, you can create a workflow that publishes your package to both the Maven Central Repository and GitHub Packages by running the gradle publish
command.
# 此工作流使用未经 GitHub 认证的操作。 # 它们由第三方提供,并受 # 单独的服务条款、隐私政策和支持 # 文档。 # GitHub 建议将操作固定到提交 SHA。 # 若要获取较新版本,需要更新 SHA。 # 还可以引用标记或分支,但该操作可能会更改而不发出警告。 name: Publish package to the Maven Central Repository and GitHub Packages on: release: types: [created] jobs: publish: runs-on: ubuntu-latest permissions: contents: read packages: write steps: - uses: actions/checkout@v4 - name: Set up Java uses: actions/setup-java@v4 with: java-version: '11' distribution: 'temurin' - name: Setup Gradle uses: gradle/actions/setup-gradle@af1da67850ed9a4cedd57bfd976089dd991e2582 # v4.0.0 - name: Publish package run: ./gradlew publish env: MAVEN_USERNAME: ${{ secrets.OSSRH_USERNAME }} MAVEN_PASSWORD: ${{ secrets.OSSRH_TOKEN }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# 此工作流使用未经 GitHub 认证的操作。
# 它们由第三方提供,并受
# 单独的服务条款、隐私政策和支持
# 文档。
# GitHub 建议将操作固定到提交 SHA。
# 若要获取较新版本,需要更新 SHA。
# 还可以引用标记或分支,但该操作可能会更改而不发出警告。
name: Publish package to the Maven Central Repository and GitHub Packages
on:
release:
types: [created]
jobs:
publish:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@v4
- name: Set up Java
uses: actions/setup-java@v4
with:
java-version: '11'
distribution: 'temurin'
- name: Setup Gradle
uses: gradle/actions/setup-gradle@af1da67850ed9a4cedd57bfd976089dd991e2582 # v4.0.0
- name: Publish package
run: ./gradlew publish
env:
MAVEN_USERNAME: ${{ secrets.OSSRH_USERNAME }}
MAVEN_PASSWORD: ${{ secrets.OSSRH_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
此工作流程执行以下步骤:
-
检出项目仓库的副本。
-
设置 Java JDK。
-
设置 Gradle 环境。
gradle/actions/setup-gradle
操作负责工作流运行之间的缓存状态,并提供所有 Gradle 执行的详细摘要。 -
Executes the Gradle
publish
task to publish to theOSSRH
Maven repository and GitHub Packages. TheMAVEN_USERNAME
environment variable will be set with the contents of yourOSSRH_USERNAME
secret, and theMAVEN_PASSWORD
environment variable will be set with the contents of yourOSSRH_TOKEN
secret. TheGITHUB_TOKEN
environment variable will be set with the content of theGITHUB_TOKEN
secret. Thepermissions
key specifies the access that theGITHUB_TOKEN
secret will allow.For more information about using secrets in your workflow, see Using secrets in GitHub Actions.