Here we describe all the ceremonial stuff necessary to publish a Java library to Maven Central.
Publishing to Maven Central requires authentication in the form of username and password or user token (as username and password). We use user tokens for authentication, which are stored in our password manager.
The MAVEN_USERNAME
and MAVEN_PASSWORD
environment variable needs to be set on the device.
In the GitHub Secrets, we need to add two secrets called MAVEN_USERNAME
and MAVEN_PASSWORD
. They can be accessed in a yaml file with ${{ secrets.MAVEN_USERNAME }}
and ${{ secrets.MAVEN_PASSWORD }}
. We pass both these secrets in the env
block.
For example:
- name: Publish to Maven Central
run: ./gradlew publish
env:
MAVEN_USERNAME: ${{ secrets.MAVEN_USERNAME }}
MAVEN_PASSWORD: ${{ secrets.MAVEN_PASSWORD }}
This guide is based on Working with PGP Signatures and OpenPGP Best Practices.
Real name: the secureCodeBox authors
Email: securecodebox@iteratec.com
Comment: Maven Release Signing Key
Fingerprint: 40AA7D29EB6DE0667D7E723ADE4725604A739BAF
Password: [see our password manager]
We create a new GPG key with:
gpg --full-generate-key
Download private key from password manager and import it locally:
gpg --import private.key
gpg --armor --export-secret-keys $KEYID
In the GitHub Secrets, add the output of this command to the SIGNING_KEY
secret.
Additionally, you must add the corresponding password as SIGNING_PASSWORD
. Both can be accessed in a YAML file with ${{ secrets.SIGNING_KEY }}
and ${{ secrets.SIGNING_PASSWORD }}
. We pass both these secrets in the env
block.
For example:
- name: Publish to Maven Central
run: ./gradlew publish
env:
SIGNING_KEY: ${{ secrets.SIGNING_KEY }}
SIGNING_PASSWORD: ${{ secrets.SIGNING_PASSWORD }}
gpg -o private.key --export-secret-key $KEYID
It is recommended to use an expiration date less than two years. We use an interval of two years. This means that we need to extend the expiration date every two years! We use an appointment of the secureCodeBox team calendar to remind us.
- Download the private key file
private.key
from password manager - Import it locally:
gpg --import private.key
- Select the key :
gpg --edit-key $KEYID
- Now select the subkey and set the expiry date (use
2y
for two years):
gpg> key 1
gpg> expire
- Save it:
gpg> save
- Update the private key in out password manager and GitHub Secrets