Deploying Open Source artifact to Central Maven
We all have build open source artifacts, and always grumbled because it was difficult to deploy them to Central Maven. Hence, we declare repositories in our poms, to explain how to access to our own artifacts !
I’ve search how to do it, I’ve failed to manage it, and then, with a lot perseverance, finally find the way to deploy an artifact to Central Maven ! So, I give you my recipe.
If you want to deploy to Central Maven, it’s not allowed ! You have to deploy to another repository, who will be automatically synchronized with Central. I’ve choosen http://oss.sonatype.org. You have to create an account, you get a login / password.
First, declare this authentication to your ~/.m2/settings.xml :
<settings> <servers> <server> <id>ossrh</id> <username>cmarchand</username> <password>xxxxxx</password> </server> </servers> </settings>
Then, you’ll need to sign artifacts to make them deployable. Maven has a plugin to sign artifacts, but it requires use – and installation – of GPG. Dependending on your OS, choose the right documentation, the right distribution, create your GPG key and deploy it to a server ; it is explained here. I’ve created a no expire key, to make life simpler.
Once your key created, declare it in your ~/.m2/settings.xml :
<settings> <servers ... /> <profiles> <profile> <id>ossrh</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <gpg.executable>gpg2</gpg.executable> <gpg.passphrase>xxxx</gpg.passphrase> </properties> </profile> </profiles> </settings>
This will allow you to use your key to sign artifacts from Maven. Your keys are simple files, that can be moved from a computer to another.
Then, you must register your groupId. You should own a domain name, and declare a groupId based on this domain name. Create a JIRA issue on Jira. In Issue type field, choose New Project. Be carefull, it’s not a new project, it’s your groupId root, so a new groupId root. I own marchand.top, I’ve created an issue for top.marchand/tt>. Once the issue will be processed (48h max), you'll get URI of repositories where you can deploy.
An open source artifact must have various informations, else it’ll be refused. :
Add these informations to you pom.xml.
Then, you have to declare what you deploy. I deploy only releases, so I’ve decalred this in pom :
<distributionManagement> <repository> <id>ossrh</id> <url>URL sent in JIRA issue</url> </repository> </distributionManagement>
If you want to deploy snapshots, add a snapshotRepository>, with URL sent back in JIRA issue.
Then, I declare a profile to generate sources (it’s an open-source project), javadoc, and to sign artifacts. All of this is required to deploy :
<profiles> <profile> <id>release</id> <build> <plugins> <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.7</version> <extensions>true</extensions> <configuration> <serverId>ossrh</serverId> <nexusUrl>https://oss.sonatype.org/</nexusUrl> <autoReleaseAfterClose>true</autoReleaseAfterClose> </configuration> </plugin> </plugins> </build> </profile> </profiles>
Then, when you want to deploy, just type :
mvn -Prelease deploy
That’s it !
When you deploy, your artifact goes to a staging repository, where it is check. If it does not succeed checks, you’ll be only able to remove it. If you’ve added true in nexus-staging-maven-plugin, it will be automatically moved to a release repository on sonatype, repository synchronized with Central Maven. Else, you have to type
mvn -Prelease org.sonatype.plugins:nexus-staging-maven-plugin:staging:release
or use Nexus web application to convert you artifact to a release.