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