Publishing a Java Maven project to Central Repository using a Github actions pipeline

From Maven project to the Central Repository

What is the Maven Central Repository?

Recently, I finished the first iteration of my new open source project Property Aggregator which simplifies Java property handling. It is a Java project that uses Maven for dependency management and building.

For Maven projects, it is good practice that released artifacts are published to the Maven Central Repository - a central storage for jars that Maven projects automatically downloads dependencies from when they do not exist on a local system.

The Central Repository is maintained by the software company Sonatype which is also the authority to provide access to it by new projects..

My own project can be found here as well: https://mvnrepository.com/artifact/blog.softwaretester/property-aggregator

Maven Central versions

Note that the different major and minor version of my library are all stored here. If you click on one of them, you will see the actual dependency information in case you want to use it in your own Maven or Gradle project:

Maven dependency info

Preparing a Maven project for Central Repository publishing

Required information

When publishing your project to Maven Central, certain standards must be met in your project POM file.

All sample information here is directly taken from my Property Aggregator open source project.

SCM information

This information is required by Sonatype. For a project on Github, this data looks like this:

<scm>
    <connection>scm:git:git@github.com:bischoffdev/property-aggregator.git</connection>
    <developerConnection>scm:git:git@github.com:bischoffdev/property-aggregator.git</developerConnection>
    <url>https://github.com/bischoffdev/property-aggregator</url>
</scm>

The connection and developerConnection elements are the same Git URLs with the format scm:git:git@github.com:user-name/project-name.git. The url is the main project site. For Github hosted projects, it can be just the URL to the Github project page or a completely separate website hosted elsewhere.

Note: More information can be found here: https://central.sonatype.org/publish/requirements/#scm-information

Developer information

The second piece of information required to push to Maven Central is the developers block:

<developers>
    <developer>
        <name>Benjamin Bischoff</name>
        <organizationUrl>/</organizationUrl>
        <roles>
            <role>Test Automation Engineer</role>
        </roles>
        <url>/</url>
    </developer>
</developers>

Note: Not all of these elements is required. More information can be found here: https://central.sonatype.org/publish/requirements/#developer-information

The license information

Your project needs to have a valid open source license attached to it. If your project is not licensed yet, you can check available types of license and their characteristics here: https://choosealicense.com/

<licenses>
    <license>
        <name>Apache License, Version 2.0</name>
        <url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
        <distribution>repo</distribution>
        <comments>A business-friendly OSS license</comments>
    </license>
</licenses>

More information can be found here: https://central.sonatype.org/publish/requirements/#license-information

Adding dependencies

The GPG key

Generating the key

Uploading the public key

Setting up the Github actions pipeline

Setting up the secrets

Setting the trigger

Setting up Java and Maven

Adding the publish workflow

Result