Skip to content

Instantly share code, notes, and snippets.

@Timongcraft
Last active November 16, 2023 20:01
Show Gist options
  • Save Timongcraft/69ddbe39b894b819e93296339141b844 to your computer and use it in GitHub Desktop.
Save Timongcraft/69ddbe39b894b819e93296339141b844 to your computer and use it in GitHub Desktop.
Accessing Maven GitHub Packages [Guide]

This guide will walk you through the process of getting and accessing Maven GitHub Packages using a personal access token. You can either integrate the token directly into your pom.xml (potentially unsafe) or set it up in your ~/.m2/settings.xml (safe)

Generating a Personal Access Token

Adding the Dependency:

Configure Maven to Access the GitHub Package

You have two options:

⚠️ The second way is recommended because you are required to set your access token in the projects pom.xml and when uploading your project somewhere your access token is exposed to anyone who has access to that page.

Directly in your pom.xml

Import the Repository:

<repositories>
        <repository>
            <id>github-REPONAME</id>
            <name>GitHub PACKAGENAME Apache Maven Package</name>
            <url>https://YOUR_GITHUB_NAME:[email protected]/REPOOWNER/REPONAME</url>
        </repository>
    </repositories>

Note: Replace placeholders (in uppercase) with actual values.

In ~/-m2/settings.xml (recommended)

  • Locate the ~.m2 folder.

    • Windows: %USERNAME%\.m2 (paste this into the explorer's address bar)
  • Create the settings.xml.

  • Paste the following:

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
                      http://maven.apache.org/xsd/settings-1.0.0.xsd">

  <activeProfiles>
    <activeProfile>github</activeProfile>
  </activeProfiles>

  <profiles>
    <profile>
      <id>github</id>
      <repositories>
        <repository>
            <id>github-REPONAME</id>
            <name>GitHub PACKAGENAME Apache Maven Package</name>
            <url>https://YOUR_GITHUB_NAME:[email protected]/REPOOWNER/REPONAME</url>
          <!-- You may want to turn this on -->
          <snapshots>
            <enabled>false</enabled>
          </snapshots>
        </repository>
      </repositories>
    </profile>
  </profiles>

  <servers>
    <server>
      <id>github</id>
      <username>YOUR_GITHUB_NAME</username>
      <password>ACCESS_TOKEN</password>
    </server>
  </servers>
</settings>

Note: Replace placeholders (in uppercase) with actual values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment