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.

      <name>GitHub Packages</name>

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.

name: Publish the package to GitHub Packages
    types: [created]
    runs-on: ubuntu-latest 
      contents: read
      packages: write 
      - uses: actions/checkout@v3
      - uses: actions/setup-java@v3
          java-version: '11'
          distribution: 'adopt'
      - name: Publish package
        run: mvn --batch-mode deploy
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
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.

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.

Create a personal access token


maven 7243151179308482813

Post a Comment Default Comments