DEV Community

Jakub Zalas
Jakub Zalas

Posted on • Updated on

How to publish Maven packages to a single GitHub repository

image

A working example of this setup can be found in vlingo XOOM repositories: https://github.com/vlingo/xoom-platform/packages

Publishing to the current repository

Before we look at publishing to an external repository, let's review how straight forward it is to publish to the current one:

name: Build

on:
  release:
    types: [created]

jobs:
  publish:
    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

      - name: Deploy to GitHub
        run: mvn --batch-mode -DuseGitHubPackages=true deploy
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Enter fullscreen mode Exit fullscreen mode

GitHub packages repository will be automatically configured for us in Maven by the actions/setup-java action. All's left to do is to pass the GITHUB_TOKEN secret as an environment variable to the mvn deploy command.

Note: GITHUB_TOKEN secret is available on all GitHub action builds out of the box. The token's scope is limited to the current repository.

In the above example we also enabled the useGitHubPackages flag in order to load the Maven profile that configures distribution mangement:

<!-- pom.xml -->
<project ...>
  <!-- ... -->

  <profiles>
    <profile>
      <id>github</id>
      <activation>
        <property>
          <name>useGitHubPackages</name>
          <value>true</value>
        </property>
      </activation>
      <distributionManagement>
        <repository>
          <id>github</id>
          <name>GitHub Packages</name>
          <url>https://maven.pkg.github.com/vlingo/xoom-common</url>
        </repository>
      </distributionManagement>
    </profile>
  </profiles>
</project>
Enter fullscreen mode Exit fullscreen mode

The above example works for publishing vlingo/xoom-common from within the same repository.

Publishing to a shared repository

The setup from previous section works great if we only have a single repository to publish from. Once we start publishing from multiple GitHub repositories, we'll end up with one Maven repository per GitHub repository.

This is not always desired, especially if we develop a set of libraries that are usually installed together. In such a scenario, our clients would be forced to include in their pom.xml one Maven repository per library we provide.

The solution is to choose a dedicated repository for publishing our Maven packages and use it in distribution management for all the other repositories.

Maven configuration

For example, having libraries like xoom-common, xoom-actors, xoom-http, we could choose to publish all of them to a single repository xoom-platform:

<!-- pom.xml -->
<project ...>
  <!-- ... -->

  <profiles>
    <profile>
      <id>github</id>
      <activation>
        <property>
          <name>useGitHubPackages</name>
          <value>true</value>
        </property>
      </activation>
      <distributionManagement>
        <repository>
          <id>github</id>
          <name>GitHub Packages</name>
          <url>https://maven.pkg.github.com/vlingo/xoom-platform</url>
        </repository>
      </distributionManagement>
    </profile>
  </profiles>
</project>
Enter fullscreen mode Exit fullscreen mode

GitHub action

The GitHub action will remain very similar to the one we looked at in the beggining with one notable difference - the GITHUB_TOKEN:

name: Build

on:
  release:
    types: [created]

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

      - name: Deploy to GitHub
        run: mvn --batch-mode -DuseGitHubPackages=true deploy
        env:
          GITHUB_TOKEN: ${{ secrets.DEPLOY_GITHUB_TOKEN }}
Enter fullscreen mode Exit fullscreen mode

We can no longer use the token provided by default as its scope is limited to the current repository.

Instead, we'll need to create a Personal Access Token and pass it to the action as a secret (DEPLOY_GITHUB_TOKEN in the example above).

Creating a GitHub Personal Access Token

To make it work across repositories we'll need to define a new Personal Access Token in:

Select the write:packages scope and all the repo scopes should be automatically selected for us.

image

Defining a secret

Next, the token needs to be defined as a secret in our organisation or each of the repositories we need to publish packages from.

Give it a name (i.e. DEPLOY_GITHUB_TOKEN) and set its value to the Personal Access Token created in the previous step.

image

Repository secrets are defined in repository Settings > Secrets. There's a similar section for the organisation secrets.

Top comments (5)

Collapse
 
cmani97 profile image
Mani • Edited

Hi, I followed the same but getting the following errors. What is the issue? Please help.
"Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.8.2:deploy (default-deploy) on project demolibrary: Failed to deploy artifacts: Could not transfer artifact com.example:demolibrary:jar:3.0.2 from/to github (maven.pkg.github.com/-----/test-pl... authentication failed for maven.pkg.github.com/-----/test-pl..., status: 401 Unauthorized -> [Help 1]"
and
"Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.8.2:deploy (default-deploy) on project demolibrary: Failed to deploy artifacts: Could not find artifact com.example:demolibrary:jar:3.0.1 in github (maven.pkg.github.com/-------/test-...) -> [Help 1]"

Collapse
 
jakub_zalas profile image
Jakub Zalas

Looks like your github access token isn't valid or wasn't given the right scope. Make sure you've assigned write:packages scope.

Collapse
 
cmani97 profile image
Mani

Yes, I added that also. But still same error "status: 422 Unprocessable Entity".

Thread Thread
 
jakub_zalas profile image
Jakub Zalas

Feel free to review the repositories where it all works fine.

For example: github.com/vlingo/xoom-actors/

All vlingo xoom packages are published to github.com/vlingo/xoom-platform/pa...

Thread Thread
 
jakub_zalas profile image
Jakub Zalas

One other thing to check is if you're creating the artifact properly. Perhaps it's missing or something like that. see stackoverflow.com/questions/643221...