🌎Publishing a Java library / package to Maven

Tutorial / guide to publishing a java package to Maven Central

OVERVIEW

Publishing a Java library to Maven Central involves several steps. Here is a brief overview of the process:

  1. Create a Maven project: First, you need to create a Maven project with your Java library code. You can use a tool like Apache Maven or Gradle to manage your project dependencies and build your library.

  2. Configure your project for Maven Central: Before you can publish your library to Maven Central, you need to configure your project to meet the repository's requirements. This involves adding some metadata to your project's POM (Project Object Model) file, including a unique group ID, artifact ID, and version number.

  3. Create a GPG key: You need to create a GPG key to sign your library. Maven Central requires that all artifacts are signed with a GPG key to ensure their authenticity and integrity.

  4. Deploy your library: Once your project is properly configured and signed, you can deploy it to Maven Central using the Maven Deploy Plugin. This will upload your library to the repository and make it available for others to use.

Here are the detailed steps you can follow to publish a Java library to Maven Central:

  1. Create a Maven project

    • Create a new Maven project in your preferred IDE or from the command line using the mvn archetype:generate command.

    • Add your Java library code to the project's src/main/java directory.

    • Define your library's dependencies in the project's POM file.

  2. Configure your project for Maven Central

    • Choose a unique group ID for your library. This should be a reverse domain name, such as com.example.

    • Choose a unique artifact ID for your library.

    • Set the version of your library.

    • Add a description of your library.

    • Add a URL for your library's homepage.

    • Add the license under which your library is distributed.

    • Add a developer's contact information.

  3. Create a GPG key

    • Install GPG on your system.

    • Generate a new GPG key using the gpg --gen-key command.

    • Export your public key using the gpg --export -a "Your Name" > public.key command.

    • Upload your public key to a key server, such as https://keys.openpgp.org.

  4. Deploy your library

    • Create a user account on https://oss.sonatype.org/.

    • Create a new JIRA issue in the "Central (REPO)" project to request access to the repository.

    • Wait for your request to be processed and approved.

    • Configure the Maven Deploy Plugin in your project's POM file to use your Sonatype account credentials.

    • Run the mvn clean deploy command to deploy your library to the staging repository.

    • Log in to https://oss.sonatype.org/ and close and release the staging repository.

    • Wait for your library to be synced to Maven Central, which can take several hours.

Congratulations! Your Java library is now published to Maven Central and available for others to use.

PREPARING THE POM FILE

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.5</version>
        <relativePath/>
    </parent>

    <groupId>com.bloomscorp</groupId>
    <artifactId>bmx-behemoth</artifactId>
    <version>0.0.1</version>
    <packaging>jar</packaging>
    <inceptionYear>2023</inceptionYear>
    <url>https://github.com/bloomscorp/bmx-behemoth</url>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <description>

    </description>

    <issueManagement>
        <url>https://github.com/bloomscorp/bmx-behemoth/issues</url>
        <system>GitHub Issues</system>
    </issueManagement>

    <distributionManagement>
        <snapshotRepository>
            <id>ossrh</id>
            <url>https://s01.oss.sonatype.org/content/repositories/snapshots</url>
        </snapshotRepository>
        <repository>
            <id>ossrh</id>
            <url>https://s01.oss.sonatype.org/service/local/staging/deploy/maven2</url>
        </repository>
    </distributionManagement>

    <scm>
        <connection>scm:git:git://github.com/bloomscorp/bmx-behemoth.git</connection>
        <url>https://github.com/bloomscorp/bmx-behemoth</url>
    </scm>

    <developers>
        <developer>
            <name>Debabrata Acharya</name>
            <email>debabrata@bloomscorp.com</email>
        </developer>
    </developers>

    <licenses>
        <license>
            <name>MIT License</name>
            <url>https://www.mit.edu/~amini/LICENSE.md</url>
        </license>
    </licenses>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hibernate.validator</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>8.0.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.vladmihalcea</groupId>
            <artifactId>hibernate-types-52</artifactId>
            <version>2.21.1</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.26</version>
            <optional>true</optional>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>3.2.1</version>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <goals>
                            <goal>jar-no-fork</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>3.5.0</version>
                <executions>
                    <execution>
                        <id>attach-javadocs</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-gpg-plugin</artifactId>
                <version>3.0.1</version>
                <executions>
                    <execution>
                        <id>sign-artifacts</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>sign</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.sonatype.plugins</groupId>
                <artifactId>nexus-staging-maven-plugin</artifactId>
                <version>1.6.13</version>
                <extensions>true</extensions>
                <configuration>
                    <serverId>ossrh</serverId>
                    <nexusUrl>https://s01.oss.sonatype.org/</nexusUrl>
                    <autoReleaseAfterClose>true</autoReleaseAfterClose>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.owasp</groupId>
                <artifactId>dependency-check-maven</artifactId>
                <version>8.2.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>check</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

Last updated