javamaven

How to manually publish JAR to maven central?


I have created an open source project that I'd like to publish to maven central so that the users can use the library by simply referencing it in their pom. Like so:

<dependency>
    <groupId>in.ksharma</groupId>
    <artifactId>log4j-weblayout</artifactId>
    <version>0.0.1-BETA</version>
</dependency>

I've found several online tutorials, but some of them are out of date, some recommend automating the entire process and thereby overtly complicate it.

For example one tutorial recommended creating SSH keys for your github account and having maven automatically create a git tag whenever pushing to maven central. Though this is useful it is not necessary to get started.

Another example, trying to release it directly through maven also gives some kind of error:

mvn release:clean release:prepare release:perform -B -e | tee maven-central-deploy.log

Gives:

svn: E155007: '/home/kshitiz/Documents/workspaces/ggts/log4j-weblayout/pom.xml' is not a working copy

When you're doing something for the first time it often helps to do it manually first and then automate it.

What is the most basic, bare-bones way to put a JAR in maven central?


Solution

  • This answer assumes that you have a maven based project and that it is in a package-able state. mvn package should run without any errors.

    When publishing to maven central you'll need to use a group id that would identify all artifacts uploaded by you. Something like in.ksharma. You'll also need to sign your artifacts so that the users are able to verify that they're actually coming from you.

    So first go to sonatype jira and create an account, and then create a jira issue to have your group id approved. Something like this.

    Now generate a gpg keypair for signing your artifacts:

    $ gpg --gen-key
    

    Define this key in ~/.m2/settings.xml:

    <profiles>
      <profile>
        <id>sonatype-oss-release</id>
        <properties>
          <gpg.keyname>B63EFB4D</gpg.keyname>
          <gpg.passphrase>****</gpg.passphrase>
          <gpg.defaultKeyring>true</gpg.defaultKeyring>
          <gpg.useagent>true</gpg.useagent>
          <gpg.lockMode>never</gpg.lockMode>
          <gpg.homedir>/home/kshitiz/.gnupg</gpg.homedir>
        </properties>
      </profile>
    </profiles>
    

    Modify your project's pom file and append -SNAPSHOT to your version. So 0.0.1-BETA becomes 0.0.1-BETA-SNAPSHOT. Otherwise maven would complain:

    [ERROR] Failed to execute goal org.apache.maven.plugins:maven-release-plugin:2.4.2:prepare (default-cli) on project log4j-weblayout: You don't have a SNAPSHOT project in the reactor projects list. -> [Help 1]

    Also add:

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

    This parent pom provides you with some ready made functionality like configuring the maven-gpg-plugin to sign your JAR.

    Now run mvn release:clean release:prepare to generate your artifacts and gpg signature. It will give you something like:

    log4j-weblayout-0.0.1-BETA-javadoc.jar.asc
    log4j-weblayout-0.0.1-BETA-sources.jar.asc
    log4j-weblayout-0.0.1-BETA.pom.asc
    log4j-weblayout-0.0.1-BETA.pom
    log4j-weblayout-0.0.1-BETA.jar.asc
    log4j-weblayout-0.0.1-BETA-javadoc.jar
    log4j-weblayout-0.0.1-BETA-sources.jar
    log4j-weblayout-0.0.1-BETA.jar
    

    Now package these into a single JAR:

    jar -cvf bundle.jar log4j-weblayout-0.0.1-BETA*
    

    Go to Sonatype Nexus and login with your credentials. Go to staging upload and upload your bundle.

    enter image description here

    Then go to staging repositories section, select your repository and click release (More help here). Comment on the jira issue that you have released the artifact and wait some time.

    Now your users can search and use the artifact from the central repository: enter image description here