no

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

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.

&ltdistributionManagement>
    &ltrepository>
      &ltid>github&lt/id>
      &ltname>GitHub Packages&lt/name>
      &lturl>https://maven.pkg.github.com//common-logging&lt/url>
    &lt/repository>
  &lt/distributionManagement>

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 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 }}
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 repositories' section pointing to your packages in pom.xml.

&ltrepositories>
	&ltrepository>
		&ltid>github&lt/id>
		&lturl>https://maven.pkg.github.com//packages&lt/url>
	&lt/repository>
&lt/repositories>

4.2 Configure your maven's local settings.xml. 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.

&ltsettings>
  &ltservers>
	&ltserver>
      &ltid>github&lt/id>
      &ltusername>czetsuya&lt/username>
      &ltpassword>xxx&lt/password>
    &lt/server>
  &lt/servers>
&lt/settings>
Create a personal access token https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token.

Related

maven 7243151179308482813

Post a Comment Default Comments

item