Skip to content

Instantly share code, notes, and snippets.

@jeszy75
Last active August 16, 2024 14:21
Show Gist options
  • Save jeszy75/2ed0cb37406c8c49c7019ae1e3d4b953 to your computer and use it in GitHub Desktop.
Save jeszy75/2ed0cb37406c8c49c7019ae1e3d4b953 to your computer and use it in GitHub Desktop.
Deploying Maven Artifacts to GitHub

Deploying Maven Artifacts to GitHub

This document explains how to deploy Apache Maven artifacts created during the package lifecycle phase to GitHub using GitHub Packages. You can read about working with Maven and GitHub Packages here.

Setting Up Authentication

Create a "classic" personal access token (PAT) here by clicking on the Generate new token button and choosing Generate new token (classic). When a new token is created you must check at least the write:packages scope. You should copy the PAT to a safe location, since you won't be able to see it again.

Use your ~/.m2/settings.xml file to store your PAT adding the following element to the settings/servers element:

<server>
  <id>github</id>
  <username>your_github_username</username>
  <password>your_PAT</password>
</server>

Alternatively, you can encrypt the PAT with your master password, as explained here.

pom.xml

Add the repository element to the project/distributionManagement element as follows:

<distributionManagement>
  <repository>
    <id>github</id>
    <url>https://maven.pkg.github.com/name_of_owner/name_of_your_repository</url>
  </repository>
</distributionManagement>

Important: If you have created the repository for yourself on GitHub, provide your username for name_of_owner. However, if the repository is created automatically by accepting an assignment in GitHub Classroom, name_of_owner must be the name of the GitHub organization behind the classroom, e.g., INBPA0420L.

Note that artifacts created in different projects can be deployed to the same repository.

Deploying the Artifacts

The artifacts of the project can be deployed by executing

mvn deploy

Important: if you are using Apache Maven 3.9.0, executing the command will result in a BUILD FAILURE. This is caused by a bug. Currently, the issue can be fixed by executing the

mvn deploy -Dmaven.resolver.transport=wagon

command instead. However, this issue does not occur in 3.8.7 or earlier versions.

Browsing Packages

You can browse the packages deployed to your repository by clicking on Packages at the right side of the landing page your repository.

If you need to redeploy the artifacts of your project, first you must remove them from the GitHub Packages registry. This can be done by clicking on Package settings at the bottom right of the page of the package, then clicking on Delete this package.

Downloading Artifacts Deployed to GitHub

To enable access to artifacts deployed to GitHub Packages, add the following repositories element to the project element of the pom.xml file (not to be confused with the project/distributionManagement element):

<repositories>
  <repository>
    <id>github</id>
    <url>https://maven.pkg.github.com/name_of_owner/name_of_your_repository</url>
  </repository>
</repositories>

Then simply add dependency elements to the project/dependencies element providing the Maven coordinates of the artifacts to be used as dependencies.

Repository access may require authentication, in this case, credentials will be taken from the corresponding server element of the settings.xml file.

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<servers>
<server>
<id>github</id>
<username></username>
<password></password>
</server>
</servers>
</settings>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment