Publishing to Maven Central Repo in 5 steps

Maven Central Repo is the official Maven repository where all “official” jars are published. It is also known as the “central” repo and is part of the super pom.xml which is implicitly included by every Maven project. The URL of the Maven Central Repo is http://repo1.maven.org/maven2/.

If you need to share your Maven artifacts with the world, you too can publish it on the official Maven repo! Let others discover your artifacts without any repository configuration. This article is intended to be a practical, step by step, easy to follow tutorial to publish your artifacts to the central repo.

Step 1: pom.xml configuration

  • Required elements: These elements must be present in your pom.xml: modelVersion, groupId, artifactId, packaging, name, version, description, url, licenses, scm url, dependencies
  • No <repositories> or <pluginRepositories> tag allowed. All you dependencies must be present in the central repo.
  • Choose a groupId that you own. Most common way to do this is to have control over the domain name corresponding to your groupId. For example, if your groupId is com.vineetmanohar then your must own or have control over the domain name vineetmanohar.com. This is required as maven will only download artifacts for a groupId from the corresponding domain.
  • You can only publish releases, not snapshots.

Step 2: Create your domain repo

This assumes that you have a hosted server somewhere which runs apache (or similar web server to host static files), and describes how to create a repo using by defining an path in a apache virtual server. If you have root access to the machine, Login as root and create a new user called ‘repo’

 # adduser repo
 # su - repo

As the repo user and create the repo directories.

# su - repo
$ cd /home/repo
$ chmod 755 .
$ mkdir -p html/releases html/snapshots

Now map this directory to apache

Map /home/repo/html/ to http://repo.<your domain>.com

Edit the /etc/httpd/conf/httpd.conf file and add the following mapping. Change IP address and server name as applicable. Remember that server name must match or should be a sub-domain of your groupId.

 <VirtualHost 215.12.34.111:80>
  <Directory /home/repo/html>
   Options +Indexes
   AllowOverride Limit
  </Directory>

  ServerName repo.vineetmanohar.com
  DocumentRoot /home/repo/html
  CustomLog "/home/repo/access_log" "combined"
  ErrorLog "/home/repo/error_log"
  SuexecUserGroup repo repo
 </VirtualHost>

Restart apache

/etc/init.d/httpd restart

Step 3: Publish artifacts to your repo

Add a <distributionManagement> section to your pom.xml. Here’s an example, change it to reflect your server names and paths.

<distributionManagement>
 <repository>
   <id>vineetmanohar-release-repo</id>
   <name>vineetmanohar.com Release Repo</name>
   <url>scp://repo.vineetmanohar.com/home/repo/html/releases/</url>
 </repository>

 <snapshotRepository>
   <id>vineetmanohar-snapshot-repo</id>
   <name>vineetmanohar.com Snapshot Repo</name>
   <url>scp://repo.vineetmanohar.com/home/repo/html/snapshots/</url>
 </snapshotRepository>
</distributionManagement>

Once you are ready to release, change your pom.xml version to a non-SNAPSHOT version, e.g. 0.0.1. Remember that if your version is a non-SNAPSHOT version, you cannot have any SNAPSHOT dependencies either, that’s just a universal maven rule.

Now deploy your version to your release repo.

mvn deploy

If you use scp to deploy (as in the example above) you will be asked to enter your password. Alternatively you can configure to SSH without password.

PS: Alternatively you can automatically increment version and deploy automatically by using the Maven Release plugin.

Step 4: Give permission to Maven to access your repo

The simplest way is to authorize Maven by adding its private key to your repo’s authorized ssh keys. Make sure that the permissions are set as described below.

# su - repo
$ mkdir -p .ssh
$ chmod 700 .ssh
$ cd .ssh
$ vi authorized_keys
<paste maven's public key in file authorized keys>
$ chmod 400 authorized_keys

Copy the contents of Maven’s public key and paste it in /home/repo/.ssh/authorized_keys

Step 5: Submit a request to the Maven folks

You have your artifacts published in a public repo and Maven has the permission to rsync your repo over SSH. All you need to do now is to submit a request to Maven to add your server to the list of servers they sync with.

Open an account at http://jira.codehaus.org, if you don’t have one already. Once you have created an account, submit your request here.

Here is an example request that I submitted. It took them 7 days to close the ticket, so be patient. After the ticket has been closed, it might take half a day for the servers to gets sync’d, after which I could see my artifacts appear here.

Reference

Related posts:

  1. Getting started with Nexus Maven Repo Manager
  2. How to automate project versioning and release with Maven
  3. Tweet your builds with Maven Twitter Plugin
  4. Maven Docbook for beginners
  5. 3 ways to run Java main from Maven

3 comments to Publishing to Maven Central Repo in 5 steps

  • The rsync process over the years has grown to become unmaintainable. It’s not efficient to reach out twice a day to thousands of servers to pick up artifacts that on average change only a few times a year. Keeping all the syncs updated when people change servers etc has grown to an enormous effort.

    Additionally, one of the major rants against Maven is the quality of the metadata in Central. Unfortunately the trash that gets in there is almost always coming from these rsynced in projects because there’s no good way to validate it.

    Because of this, the rsyncs are no longer being added for individual projects, only for large forges that host many projects, or very large and active projects by exception. These new forges and syncs must meet much more stringent criteria and this goes to help all Maven users by stopping the garbage at the front door.

    The Guide has been updated to reflect this policy change that took effect this year.

    –Brian Fox
    Apache Maven PMC Chair

  • vineet

    Hi Brian,

    Will the old rsyncs for individual projects be maintained?

    Thanks,
    Vineet

  • Hi Vineet,
    For the short term the rsyncs will be continued but we will encourage people to switch and gain the benefits of automatic verification in the forges. Over the long term, we hope to reduce the rsyncs to just the forges.

    The other benefit I forgot to mention about this strategy is it becomes more of a push model for releases via the forges and thus things can get into Central much faster. For example, the 3 forges listed on my link above all sync hourly instead of daily like everything else since there’s so much less overhead.

    –Brian

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Get Adobe Flash playerPlugin by wpburn.com wordpress themes