Whether for private tinkering projects or public libraries: it often makes sense to store the packages in a Maven repository. For those who already use GitHub, the GitHub Package Repository comes in very handy. However, there are a few things to bear in mind here.
Prepare pom.xml
Before we take care of the GitHub workflow, we first need to customise our pom.xml
and configure the distributionManagement
options there, which is just inside the project
tag.
1
2
3
4
5
6
7
<distributionManagement>
<repository>
<id>github</id>
<name>Projekt Name</name>
<url>https://maven.pkg.github.com/tinyoverflow/project-name</url>
</repository>
</distributionManagement>
Create GitHub Workflow
Now the project is prepared and we can turn our attention to the GitHub workflow file. The following workflow works as follows:
- The workflow is triggered by pushing tags in the format
*.*
. - On an Ubuntu machine, it is executed:
- Retrieve the source code from the repository.
- Install and configure Java, including cache. The
server-id
is identical to the ID from thepom.xml
- The project is compiled using Maven.
- The project is built and uploaded to the package repository using the configuration in the
pom.xml
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
name: Build and Publish
on:
push:
tags: [ "*.*" ]
jobs:
build:
name: Build and Publish
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: "17"
distribution: 'temurin'
cache: maven
server-id: github
settings-path: ${{ github.workspace }}
- name: Build with Maven
run: mvn -B package --file pom.xml
- name: Publish to GitHub Packages Apache Maven
run: mvn --batch-mode deploy -s $GITHUB_WORKSPACE/settings.xml
if: startsWith(github.ref, 'refs/tags/')
env:
GITHUB_TOKEN: ${{ github.token }}
More detailed information about the workflow file can be found in the GitHub Actions documentation.
Troubleshooting
Sometimes problems occur that are not self-explanatory at first glance. In the following I will go into some of these possible errors.
Workflow fails with 403 Unauthorised
This error can have the following causes, among others:
- The workflow token does not have the required permissions
- Go to the settings of your repository to
Code and automation -> Actions -> General
. - At the bottom of the page you will find the option
Workflow permissions
. - Make sure that the option
Read and write permissions
is selected.
- Go to the settings of your repository to
- The repository URL in the
pom.xml
is wrong
Make sure that the repository URL in yourpom.yml
is correct. It starts withhttps://maven.pkg.github.com/
instead ofhttps://github.com/
.
Example:https://maven.pkg.github.com/tinyoverflow/project-name
Workflow fails with 422 Unprocessable Entity
If this error occurs, the artifactId
in your pom.xml
is probably invalid. In order for you to upload an artefact to the GitHub package repository, it must only consist of lowercase letters, numbers and hyphens.
1
2
3
4
5
6
7
8
9
<!-- Correct -->
<groupId>me.tinyoverflow</groupId>
<artifactId>project-name</artifactId>
<version>1.0.0-SNAPSHOT</version>
<!-- Incorrect -->
<groupId>me.tinyoverflow</groupId>
<artifactId>ProjectName</artifactId>
<version>1.0.0-SNAPSHOT</version>
Please do not forget to adjust any scripts when changing your
artifactId
as this may affect the final filename.