Skip to main content

Publishing Java packages with Gradle

You can use Gradle to publish Java packages to a registry as part of your continuous integration (CI) workflow.

Introduction

이 가이드에서는 GitHub Packages 및 Maven 중앙 리포지토리에 Java 패키지를 게시하는 워크플로를 만드는 방법을 보여 줍니다. 단일 워크플로를 사용하면 패키지를 단일 리포지토리 또는 여러 리포지토리에 게시할 수 있습니다.

경고

이 가이드에 사용된 예제는 레거시 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:

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".

Groovy
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.

YAML

# 이 워크플로는 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 }}

이 워크플로는 다음 단계를 수행합니다.

  1. 프로젝트 리포지토리의 복사본을 체크 아웃합니다.

  2. Java JDK를 설정합니다.

  3. Gradle 환경을 설정합니다. 이 gradle/actions/setup-gradle 작업은 워크플로 실행 간의 캐싱 상태를 처리하고 모든 Gradle 실행에 대한 자세한 요약을 제공합니다.

  4. Executes the Gradle publish task to publish to the OSSRH Maven repository. The MAVEN_USERNAME environment variable will be set with the contents of your OSSRH_USERNAME secret, and the MAVEN_PASSWORD environment variable will be set with the contents of your OSSRH_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.

Groovy
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.

YAML

# 이 워크플로는 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 }}

이 워크플로는 다음 단계를 수행합니다.

  1. 프로젝트 리포지토리의 복사본을 체크 아웃합니다.

  2. Java JDK를 설정합니다.

  3. Gradle 환경을 설정합니다. 이 gradle/actions/setup-gradle 작업은 워크플로 실행 간의 캐싱 상태를 처리하고 모든 Gradle 실행에 대한 자세한 요약을 제공합니다.

  4. Executes the Gradle publish task to publish to GitHub Packages. The GITHUB_TOKEN environment variable will be set with the content of the GITHUB_TOKEN secret. The permissions key specifies the access that the GITHUB_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.

Groovy
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.

YAML

# 이 워크플로는 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 }}

이 워크플로는 다음 단계를 수행합니다.

  1. 프로젝트 리포지토리의 복사본을 체크 아웃합니다.

  2. Java JDK를 설정합니다.

  3. Gradle 환경을 설정합니다. 이 gradle/actions/setup-gradle 작업은 워크플로 실행 간의 캐싱 상태를 처리하고 모든 Gradle 실행에 대한 자세한 요약을 제공합니다.

  4. Executes the Gradle publish task to publish to the OSSRH Maven repository and GitHub Packages. The MAVEN_USERNAME environment variable will be set with the contents of your OSSRH_USERNAME secret, and the MAVEN_PASSWORD environment variable will be set with the contents of your OSSRH_TOKEN secret. The GITHUB_TOKEN environment variable will be set with the content of the GITHUB_TOKEN secret. The permissions key specifies the access that the GITHUB_TOKEN secret will allow.

    For more information about using secrets in your workflow, see Using secrets in GitHub Actions.