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
distributionManagementrepository pointing at your GitHub Packages URL. - Use
setup-javawithserver-id,server-username, andserver-passwordenv references. - Run
mvn deploywithpackages: writepermission.
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>indistributionManagementmust matchserver-idor auth is not applied. - GitHub Packages Maven does not accept re-deploying a released (non-SNAPSHOT) version.
Related guides
How to Publish an npm Package to GitHub PackagesPublish a scoped npm package to GitHub Packages from CI by pointing the registry at npm.pkg.github.com and au…
How to Publish a Maven Artifact to Maven CentralPublish a Maven artifact to Maven Central from CI by GPG-signing the artifacts and deploying through the Sona…
How to Publish a NuGet Package to GitHub PackagesPublish a NuGet package to GitHub Packages from CI by adding a source pointing at nuget.pkg.github.com and pu…