DEV Community

Ed Legaspi
Ed Legaspi

Posted on • Edited on

How to Upload Maven Artifacts to GitHub Packages

Learn how to deploy your Java Maven artifacts into GitHub packages using GitHub actions.

1. Introduction

GitHub is continuously upgrading its service offerings, and one of the latest that came out is the GitHub Packages. GitHub Packages is a platform for hosting and managing packages or artifacts. In a way, it's similar to Sonatype Nexus and can be used as the "maven central" for a private organization. In addition, it can also store containers like Docker images.

GitHub Actions is a CI/CD platform that will help us deploy and publish our Java project in GitHub Packages.

2. How do we use GitHub Packages?

Let's start with our use case, where we have a utility project logging referenced by all other services.

3. Utility Project - Logging

A Java maven project that contains all logging-related classes. This project is added as a dependency on all the organization's microservices.

Let's modify this project to:

3.1 Add distribution management in pom.xml.

<distributionManagement>
    <repository>
      <id>github</id>
      <name>GitHub Packages</name>
      <url>https://maven.pkg.github.com/your_org/common-logging</url>
    </repository>
  </distributionManagement>
Enter fullscreen mode Exit fullscreen mode

3.2 Create a GitHub action that publishes our logging artifact into packages.

You can create an action by clicking the "Action" tab / New workflow. https://docs.github.com/en/actions/quickstart

name: Publish the package to GitHub Packages
on:
  release:
    types: [created]
jobs:
  publish:
    runs-on: ubuntu-latest 
    permissions: 
      contents: read
      packages: write 
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-java@v3
        with:
          java-version: '11'
          distribution: 'adopt'
      - name: Publish package
        run: mvn --batch-mode deploy
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Enter fullscreen mode Exit fullscreen mode

Make sure to define a GITHUB_TOKEN under the project's or your organization's Secrets -> Actions.

4 Service Project

A random Java Maven project that needs a logging mechanism. Therefore, it is dependent on our logging project.

4.1 Add a repositories section pointing to your packages in pom.xml.

<repositories>
    <repository>
        <id>github</id>
        <url>https://maven.pkg.github.com/your_org/packages/</url>
    /repository>
</repositories>
Enter fullscreen mode Exit fullscreen mode

4.2 Configure your maven's local settings.xml. Please make sure to add GitHub in the server section with your username and access token. This file is normally saved in %USER%/.m2/settings.xml.

<settings>
  <servers>
    <server>
      <id>github</id>
      <username>czetsuya</username>
      <password>xxx</password>
    </server>
  </servers>
</settings>
Enter fullscreen mode Exit fullscreen mode

Create a personal access token https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token.

Top comments (0)