Apisero is now part of NTT DATA - Learn more.

Search
Close this search box.

Publishing a Maven Artifact to GitHub Package Registry

Author: Sushma Kamuni

GitHub Packages is a platform for hosting and managing packages, including containers and other dependencies. GitHub Packages combines your source code and packages in one place to provide integrated permissions management and billing, so you can centralize your software development on GitHub.

Publishing a Maven artifact to GitHub Packages 

GitHub Package Repository provides an easy way to share dependencies like Maven artifacts between GitHub projects. Publishing of new artifacts into GitHub Package Repository can be automated with GitHub Actions.

Prerequisites
  • Anypoint Platform Account
  • GitHub Repository Account
  • Git bash Terminal
  1. Adding distribution management in pom.xml 

<distributionManagement>

    <repository>

      <id>github</id>

      <name>Owner</name>

      <url>https://maven.pkg.github.com/Owner/repo-name</url>

    </repository>

</distributionManagement>

  1. Commit the code to github

Steps to be followed

Prerequisite

Download git bash on local system

  • Steps to commit the application
  1. Copy the github repository URL
  1. Create an empty folder in local directory and right-click on empty directory to open gitbash terminal.
  2. Enter the commands
    1. $ git clone <Copied URL from Git repository>

Downloads the data from the repository to the local directory

  1. $ cd <github repository name>

3. Now, in Anypoint Studio, right-click on the project -> Show In -> System Explorer. It redirects to the project folder in the workspace.

  1. Copy and paste the project files into the git local directory
  1. Next, in the terminal, enter commands 
    1. $ git add * (adds all the project files)
    2. $ git commit -m “Initial Commit” (Commits the files to set into git repository)
    3. $ git push (Pushes the files from local system to git repository)
  2. In the below screenshot, you can see that all the files are pushed to the git repository.
1.3. Personal Access Token for your GitHub account

To upload artifacts into the GitHub Package Registry, your Personal Access Token must have the scope – write:packages as shown below

1.4. Adding Personal Access Token and Github repo name in settings.xml

<servers>

<server>

     <id>github</id>

     <username>Owner</username>

     <password>Your Personal Access Token</password>

     </server>

</servers>

<profiles>

 <profile>

      <id>github</id>

      <repositories>

        <repository>

          <id>central</id>

          <url>https://repo1.maven.org/maven2</url>

  <releases><enabled>true</enabled></releases>

          <snapshots><enabled>true</enabled></snapshots>

        </repository>

        <repository>

          <id>github</id>

          <url>https://maven.pkg.github.com/Sushma-GitRepo/<repo-name></url>

          <snapshots>

            <enabled>true</enabled>

          </snapshots>

        </repository>

      </repositories>

    </profile>

</profiles>

Note: Locally, you can now publish a new Maven artifact into the GitHub Package Registry with the below command. 

$ mvn -Pgithub deploy

1.5. Publishing through GitHub Actions workflow 

name: Maven Package

on:

  push:

    branches:

      – main

jobs:

  build:

    runs-on: ubuntu-latest

    steps:

      – uses: actions/checkout@v2

      – name: Set up JDK 1.8

        uses: actions/setup-java@v1

        with:

          java-version: 1.8

          server-id: github

          server-username: GITHUB_USER_REF  # env variable name for username

          server-password: GITHUB_TOKEN_REF

      – name: Build with Maven.

        run: mvn clean package -DattachMuleSources -Pgithub deploy

        env:

          GITHUB_USER_REF: ${{ secrets.GIT_USERNAME }}

          GITHUB_TOKEN_REF: ${{ secrets.GIT_TOKEN }}

1.6. Creating secrets for GitHub username and token

 For creating secrets, goto Settings -> Secrets -> New Repository Secret button

With the next GitHub Actions build, your artifacts will be published in the repository’s dashboard

We can see the dependency created in packages

1.6. Installing a package

To install an Apache Maven package from GitHub Packages, edit the pom.xml file to include the package as a dependency.

  1. Authenticate to GitHub Packages.
  2. Add the package dependencies to the dependencies element of your project pom.xml file, replacing com.mycompany:hello-world-3 with your package.

<dependency>

  <groupId>com.mycompany</groupId>

  <artifactId>hello-world-3</artifactId>

  <version>1.0.7-SNAPSHOT</version>

</dependency>

  1. Install the package.

$ mvn install

Note:  Maven dependency downloaded at .m2\repository\com\mycompany\hello-world-3

2. Deploying the jar file to Runtime Manager
  1. Download the jar file from Github packages
  1. Copy the jar file and paste to the local repository.
  2. Open the gitbash terminal, clone the repository and follow below commands

$ git clone <url>

$ git pull

$ add <.jar file>

$git commit -m “<add comments>”

$git push 

 Provide github token as username and password in the pop up window

We can see in the below screenshot, a jar file is added to the repository.

2.1. Deploying jar file to Runtime Manager through Github actions

name: Deploying on to Runtime Manager

on:

  push:

    branches: [ main ]

  pull_request:

    branches: [ main ]

jobs:

  deploy:

    # The type of runner that the job will run on

    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job

    steps:

      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it

      – uses: actions/checkout@v2

      – name: Cache

        uses: actions/cache@v2.1.5

        with:

    # A list of files, directories, and wildcard patterns to cache and restore

          path: /home/runner/.m2/repository

          key: ${{ runner.os }}-maven-${{ hashFiles(‘**/pom.xml’) }}

          restore-keys: |

              ${{ runner.os }}-maven-

      – name: Setup Java JDK

        uses: actions/setup-java@v1

        with:

    # The Java version to set up. Takes a whole or semver Java version. See examples of supported syntax in the README file

          java-version: 1.8   

      – name: Deploy to CloudHub

        env:

          USERNAME: ${{ secrets.ANYPOINT_USERNAME }}

          PASSWORD: ${{ secrets.ANYPOINT_PASSWORD }}

        run: |

          artifactName=$(ls *.jar | head -1)

          mvn mule:deploy -Dmule.artifact=$artifactName -Danypoint.userName=$USERNAME -Danypoint.password=$PASSWORD -Danypoint.env=”Sandbox” -Danypoint.appName=”DEMO-WORK-1″

2.3. Adding the Maven plugin and cloudhub configuration in pom.xml

<plugin>

<groupId>org.mule.tools.maven</groupId>

<artifactId>mule-maven-plugin</artifactId>

<version>${mule.maven.plugin.version}</version>

<extensions>true</extensions>

<configuration>

<cloudHubDeployment>

        <username>${anypoint.userName}</username>

         <password>${anypoint.password}</password>   

<environment>${anypoint.env}</environment>

<applicationName>${anypoint.appName}</applicationName>

         <workers>1</workers>

         <workerType>Micro</workerType>

         <muleVersion>4.3.0</muleVersion>

         <objectStoreV2>true</objectStoreV2>

      </cloudHubDeployment>

            </configuration>

</plugin>

Similarly we can download old version jar files and deploy to Runtime Manager.

The below screenshot shows the jar file is successfully deployed on Runtime Manager

References

https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-apache-maven-registry#authenticating-to-github-packages

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

We use cookies on this site to enhance your user experience. For a complete overview of how we use cookies, please see our privacy policy.