The main idea of this blog is share solutions or walk arounds of problems I faced.

Friday, January 3, 2014

Host Maven repos on Github

Sometimes, we want to run a project as Maven repo to support other projects OR make it be available to other people to use as Maven dependencies. And I think hosting Maven artifacts on Github is more approachable than running a proper Maven Repository. In this post, I am going to show you how to do it step by step with a example.1. Set up your Maven repo project on Github. You need to figure out where you are going to deploy and then host your project's artifacts. Here I have created a new repo on Github, and checked it out at the root of my dev directory.dhcp123:server htxiong$ git clone git@github.com:htxiong/htxiong-mvn-repo.gitInitialized empty Git repository in ~/dev/htxiong-mvn-repo/.git/warning: You appear to have cloned an empty repository.2. Set up separate snapshots and releases directories.This is not a technical necessity, but its good to maintain a ongoing project by separating snapshots and releases directories. dhcp123:server htxiong$ cd htxiong-mvn-repodhcp123:server htxiong$ mkdir snapshotsdhcp123:server htxiong$ mkdir releases

3. Deploy your project's artifacts to Maven repo.In your project's pom.xml file, there is a configuration element <distributionManagement> that specifies the repositories to which one's project artifacts should be deployed. If you can not find it in your project's pom.xml, please add the following content into it and just before tag </project>.

</distributionManagement>Now let's build and deploy the project's artifacts by using altDeploymentRepository system property.For snapshots:dhcp123:server htxiong$ mvn -DaltDeploymentRepository=snapshot-repo::default::file:snapshots clean deployFor releases:dhcp123:server htxiong$ mvn -DaltDeploymentRepository=repo::default::file:releases clean deployAnd more information about DaltDeploymentRepository can be found at http://maven.apache.org/plugins/maven-deploy-plugin/deploy-mojo.html#altDeploymentRepository.
4. Push to Github.

In the first 3 steps, we have built and deployed your project's artifacts in our local maven repo which is just like any other git repo, so changes need to be committed and pushed up in order to be useful.

dhcp123:server htxiong$ git add snapshots/*dhcp123:server htxiong$ git add releases/*dhcp123:server htxiong$ git commit -m ''add snapshots and releases"dhcp123:server htxiong$ git push5. Use your new Maven repo in other projects.We have already deployed our project on Github as maven repo and the root of this repo will be at https://github.com/htxiong/htxiong-mvn-repo/raw/master/, then it is time now to use it in other projects by putting the following content into pom.xml.For snapshots: