Skip to content
Latchkey

How to Publish a Maven Artifact to GitHub Packages

Point distributionManagement at maven.pkg.github.com and inject settings.xml credentials from GITHUB_TOKEN so mvn deploy pushes the artifact.

A GitHub Packages Maven repository lives at https://maven.pkg.github.com/OWNER/REPO. mvn deploy reads credentials from settings.xml, which actions/setup-java can generate from environment variables backed by GITHUB_TOKEN.

Steps

  • Add a distributionManagement repository pointing at your GitHub Packages URL.
  • Use setup-java with server-id, server-username, and server-password env references.
  • Run mvn deploy with packages: write permission.

Workflow

.github/workflows/ci.yml
permissions:
  contents: read
  packages: write
jobs:
  publish:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-java@v4
        with:
          distribution: temurin
          java-version: 17
          server-id: github
          server-username: GITHUB_ACTOR
          server-password: GITHUB_TOKEN
      - run: mvn --batch-mode deploy
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

pom.xml

pom.xml
<distributionManagement>
  <repository>
    <id>github</id>
    <url>https://maven.pkg.github.com/my-org/my-repo</url>
  </repository>
</distributionManagement>

Gotchas

  • The <id> in distributionManagement must match server-id or auth is not applied.
  • GitHub Packages Maven does not accept re-deploying a released (non-SNAPSHOT) version.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →