Enterprise Java

How to publish jar to Maven central

You created your brand new project and you want to release it to maven central so people can use in their maven based project. It is not a really straightforward thing to accomplish so I wanted to write a step by step guide on it (based on his stackoverflow post). 

Create your jira account on sonatype

-Login to your jira account

Create a ticket for your project: For this step you will need a group id, a project website and a link to your source control. 

I used “com.sezinkarli” for group id because I own this domain. If you don’t have a domain and you use github, you can easily use “io.github.yourusername”

Project website can be your github link for the project and you can link your github .git link as source control. So as you can see, github will be really useful here.

-Create a PGP key. You can download it here. Then open a command prompt and do

gpg –gen-key

It will generate our public and secret keys and sign them.

In the meantime it will print something like this:

gpg: key [Your key] marked as ultimately trusted

We will use this key for the following command:

gpg –keyserver hkp://pool.sks-keyservers.net –send-keys [Your key] 

-Now we will update our user’s maven settings. Go to your .m2 folder and edit/add settings.xml .

<settings>
  <servers>
    <server>
      <id>ossrh</id>
      <username>your jira username for sonatype</username>
      <password>your jira passwordfor sonatype</password>
    </server>
  </servers>
  <profiles>
    <profile>
      <id>ossrh</id>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <properties>
        <gpg.executable>gpg</gpg.executable>
        <gpg.passphrase>passphrase you used for gpg key</gpg.passphrase>
      </properties>
    </profile>
  </profiles>
</settings>

-Now we will update our project’s pom.xml .

Add parent:

<parent>
    <groupId>org.sonatype.oss</groupId>
    <artifactId>oss-parent</artifactId>
    <version>9</version>
</parent>

Add distribution management:

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

Add build plugins:

<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.2.0</version>
                <configuration>
                    <source>11</source>
                </configuration>
                <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>1.6</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.8</version>
                <extensions>true</extensions>
                <configuration>
                    <serverId>ossrh</serverId>
                    <nexusUrl>https://oss.sonatype.org/</nexusUrl>
                    <autoReleaseAfterClose>true</autoReleaseAfterClose>
                </configuration>
            </plugin>
        </plugins>
    </build>

-Now we have everything in place. After we made sure from sonatype jira ticket that we are good to go (they do a check for group id), we can deploy our project to maven central:

mvn clean deploy

And that’s it!

I had a problem while deploying because I did not have any javadoc. After I added it, everything worked like a charm.

Published on Java Code Geeks with permission by Sezin Karli, partner at our JCG program. See the original article here: How to publish jar to Maven central

Opinions expressed by Java Code Geeks contributors are their own.

Sezin Karli

Mathematics Engineer & Computer Scientist with a passion for software development. Avid learner for new technologies. Currently working as Senior Software Engineer at Sahibinden.com.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button