Skip to main content

Publishing Java packages with Maven

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

Introduction

This guide shows you how to create a workflow that publishes Java packages to GitHub Packages and the Maven Central Repository. With a single workflow, you can publish packages to a single repository or to multiple repositories.

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 Maven, see "Building and testing Java with Maven."

You may also find it helpful to have a basic understanding of the following:

About package configuration

ThegroupIdandartifactIdfields in thepom.xmlfile create a unique identifier for your package that registries use to link your package to a registry. For more information seeGuide to uploading artifacts to the Central Repositoryin the Apache Maven documentation.

Thepom.xmlfile also contains configuration for the distribution management repositories that Maven will deploy packages to. Each repository must have a name and a deployment URL. Authentication for these repositories can be configured in the.m2/settings.xmlfile in the home directory of the user running Maven.

You can use thesetup-javaaction to configure the deployment repository as well as authentication for that repository. For more information, seesetup-java.

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 thereleaseevent triggers with typecreated.The workflow publishes the package to the Maven Central Repository if CI tests pass. For more information on thereleaseevent, see "Events that trigger workflows."

In this workflow, you can use thesetup-javaaction. This action installs the given version of the JDK into thePATH,but it also configures a Mavensettings.xmlfor publishing packages. By default, the settings file will be configured for GitHub Packages, but it can be configured to deploy to another package registry, such as the Maven Central Repository. If you already have a distribution management repository configured inpom.xml,then you can specify thatidduring thesetup-javaaction invocation.

For example, if you were deploying to the Maven Central Repository through the OSSRH hosting project, yourpom.xmlcould specify a distribution management repository with theidofossrh.

XML
<project...>
...
<distributionManagement>
<repository>
<id>ossrh</id>
<name>Central Repository OSSRH</name>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>
</project>

With this configuration, you can create a workflow that publishes your package to the Maven Central Repository by specifying the repository managementidto thesetup-javaaction. You’ll also need to provide environment variables that contain the username and password to authenticate to the repository.

In the deploy step, you’ll need to set the environment variables to the username that you authenticate with to the repository, and to a secret that you’ve configured with the password or token to authenticate with. For more information, see "Using secrets in GitHub Actions."

YAML
name:PublishpackagetotheMavenCentralRepository
on:
release:
types:[created]
jobs:
publish:
runs-on:ubuntu-latest
steps:
-uses:actions/checkout@v4
-name:SetupMavenCentralRepository
uses:actions/setup-java@v4
with:
java-version:'11'
distribution:'temurin'
server-id:ossrh
server-username:MAVEN_USERNAME
server-password:MAVEN_PASSWORD
-name:Publishpackage
run:mvn--batch-modedeploy
env:
MAVEN_USERNAME:${{secrets.OSSRH_USERNAME}}
MAVEN_PASSWORD:${{secrets.OSSRH_TOKEN}}

This workflow performs the following steps:

  1. Checks out a copy of project's repository.

  2. Sets up the Java JDK, and also configures the Mavensettings.xmlfile to add authentication for theossrhrepository using theMAVEN_USERNAMEandMAVEN_PASSWORDenvironment variables.

  3. Runs themvn --batch-mode deploycommand to publish to theossrhrepository. TheMAVEN_USERNAMEenvironment variable will be set with the contents of yourOSSRH_USERNAMEsecret, and theMAVEN_PASSWORDenvironment variable will be set with the contents of yourOSSRH_TOKENsecret.

    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 thereleaseevent triggers with typecreated.The workflow publishes the package to GitHub Packages if CI tests pass. For more information on thereleaseevent, see "Events that trigger workflows."

In this workflow, you can use thesetup-javaaction. This action installs the given version of the JDK into thePATH,and also sets up a Mavensettings.xmlfor publishing the package to GitHub Packages. The generatedsettings.xmldefines authentication for a server with anidofgithub,using theGITHUB_ACTORenvironment variable as the username and theGITHUB_TOKENenvironment variable as the password. TheGITHUB_TOKENenvironment variable is assigned the value of the specialGITHUB_TOKENsecret.

TheGITHUB_TOKENsecret is set to an access token for the repository each time a job in a workflow begins. You should set the permissions for this access token in the workflow file to grant read access for thecontentspermission and write access for thepackagespermission. For more information, see "Automatic token authentication."

For a Maven-based project, you can make use of these settings by creating a distribution repository in yourpom.xmlfile with anidofgithubthat points to your GitHub Packages endpoint.

For example, if your organization is named "octocat" and your repository is named "hello-world", then the GitHub Packages configuration inpom.xmlwould look similar to the below example.

XML
<project...>
...
<distributionManagement>
<repository>
<id>github</id>
<name>GitHub Packages</name>
<url>https://maven.pkg.github /octocat/hello-world</url>
</repository>
</distributionManagement>
</project>

With this configuration, you can create a workflow that publishes your package to GitHub Packages by making use of the automatically generatedsettings.xml.

YAML
name:PublishpackagetoGitHubPackages
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:Publishpackage
run:mvn--batch-modedeploy
env:
GITHUB_TOKEN:${{secrets.GITHUB_TOKEN}}

This workflow performs the following steps:

  1. Checks out a copy of project's repository.

  2. Sets up the Java JDK, and also automatically configures the Mavensettings.xmlfile to add authentication for thegithubMaven repository to use theGITHUB_TOKENenvironment variable.

  3. Runs themvn --batch-mode deploycommand to publish to GitHub Packages. TheGITHUB_TOKENenvironment variable will be set with the contents of theGITHUB_TOKENsecret. Thepermissionskey specifies the access granted to theGITHUB_TOKEN.

    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 using thesetup-javaaction for each registry.

Ensure yourpom.xmlfile includes a distribution management 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 theidset toossrh,and you might want to specify GitHub Packages in a distribution management repository with theidset togithub.

YAML
name:PublishpackagetotheMavenCentralRepositoryandGitHubPackages
on:
release:
types:[created]
jobs:
publish:
runs-on:ubuntu-latest
permissions:
contents:read
packages:write
steps:
-uses:actions/checkout@v4
-name:SetupJavaforpublishingtoMavenCentralRepository
uses:actions/setup-java@v4
with:
java-version:'11'
distribution:'temurin'
server-id:ossrh
server-username:MAVEN_USERNAME
server-password:MAVEN_PASSWORD
-name:PublishtotheMavenCentralRepository
run:mvn--batch-modedeploy
env:
MAVEN_USERNAME:${{secrets.OSSRH_USERNAME}}
MAVEN_PASSWORD:${{secrets.OSSRH_TOKEN}}
-name:SetupJavaforpublishingtoGitHubPackages
uses:actions/setup-java@v4
with:
java-version:'11'
distribution:'temurin'
-name:PublishtoGitHubPackages
run:mvn--batch-modedeploy
env:
GITHUB_TOKEN:${{secrets.GITHUB_TOKEN}}

This workflow calls thesetup-javaaction twice. Each time thesetup-javaaction runs, it overwrites the Mavensettings.xmlfile for publishing packages. For authentication to the repository, thesettings.xmlfile references the distribution management repositoryid,and the username and password.

This workflow performs the following steps:

  1. Checks out a copy of project's repository.

  2. Callssetup-javathe first time. This configures the Mavensettings.xmlfile for theossrhrepository, and sets the authentication options to environment variables that are defined in the next step.

  3. Runs themvn --batch-mode deploycommand to publish to theossrhrepository. TheMAVEN_USERNAMEenvironment variable will be set with the contents of yourOSSRH_USERNAMEsecret, and theMAVEN_PASSWORDenvironment variable will be set with the contents of yourOSSRH_TOKENsecret.

  4. Callssetup-javathe second time. This automatically configures the Mavensettings.xmlfile for GitHub Packages.

  5. Runs themvn --batch-mode deploycommand to publish to GitHub Packages. TheGITHUB_TOKENenvironment variable will be set with the contents of theGITHUB_TOKENsecret. Thepermissionskey specifies the access granted to theGITHUB_TOKEN.

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