Making Private Maven Packages Work on GitHub
I’ve been messing around with GitHub Actions lately, trying to streamline how to publish and share Java libraries across projects. The tricky part? All repos are private, which makes things way more complicated than they need to be. If you’re in the same boat, here’s how I got everything working.
GitHub Personal Access Token (PAT)
First thing you’ll need is a GitHub Personal Access Token. I spent way too long trying to make this work with just the default repo tokens before realizing you really do need a PAT with specific permissions.
Head over to your GitHub settings and create a new token with these permissions:
- repo (needed for private repositories)
- write:packages
- read:packages
Make sure you copy that token somewhere safe right after creating it! GitHub only shows it once, and I’ve had to regenerate these things more times than I care to admit.
Changes to Project POM file
Next up, you need to tell your project where to publish the jar file. Add this to your pom.xml file:
|
|
Obviously replace USER/REPO with your actual GitHub username and repository. I always forget to do this and spend 20 minutes wondering why things aren’t working.
Maven Configuration
Now we need to configure Maven itself. Run mvn -X
to find where your settings.xml file lives (you’ll probably need sudo rights to edit it).
Add this server definition:
|
|
The ID must match what you used in the distributionManagement section (I just use “github” everywhere to keep things simple).
Next, set up a profile:
|
|
See that wildcard asterisk in the URL? That’s the secret sauce! It lets Maven search through all your organization’s repositories for packages instead of just one specific repo. I wasted hours trying to figure this out.
Finally, make this profile active by default (because who wants to type -P github
every time?):
|
|
Maven Deploy CLI
If you want to test things out manually, just run:
|
|
Just remember to bump your version number each time, or you’ll get really cryptic version conflict errors. Ask me how I know…
GitHub Action
Let’s be honest though - nobody wants to manually deploy packages. Here’s the workflow I set up to do it automatically whenever code hits the main branch:
|
|
The cool thing here is that you don’t need your PAT for this part - the default GITHUB_TOKEN
has enough permissions to publish packages to the current repo.
Use JAR in Another Project
Now for the fun part - actually using your library in another project. Add this to your consumer project’s pom.xml:
|
|
Again, the wildcard is your friend. Then just add your dependency like any other jar:
|
|
Run mvn clean install
and if everything’s set up correctly, Maven should pull your private package without complaining.
GitHub Action for Depending Projects
Here’s the tricky part - if you want a GitHub Action to build a project that depends on your private package, the default token won’t work anymore. You need to use a PAT again.
Here’s what worked for me:
|
|
This time you do need to use your own secrets (I recommend creating organization-level secrets rather than adding them to each repo).
I’m sure there’s a cleaner way to do all of this, but hey - it works! And after the hours I spent figuring it out, that’s good enough for me.