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 188.8.131.52: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>
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.
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.
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.