About Sonatype

Articles

Nexus Repository Manager in the Cloud for Free with OpenShift

Published: September 27, 2012 11:15


Sonatype Nexus is a repository manager software which manages your software artifacts for development, deployment, and provisioning. It acts like your own central maven repository. All the teams with in an organization can download jars from Nexus and upload their team artifacts to Nexus. It makes it very easy to share binaries within teams in an organization and outside world. In this blog, I will first talk about how you will need Nexus and then we will setup a nexus instance running on OpenShift.

Why you would need Nexus?

There are various reasons you would like to host your own nexus instance. Some of the following are listing below.

  1. You want to improve the build time : Downloading artifacts from busy online maven repository takes time and leads to increase in overall build time. Having your own nexus instance, handling only your enterprise can speed things up.
  2. Better Security and Audits : Nexus can help an organization control over which dependencies should be used by development. Its health check features help you find out any license violation issues, etc.
  3. Share your artifacts with world : If you want to share the binaries of your project within your organization or with the whole world you can use nexus for it.
  4. Uploading artifacts which does not exist in maven central : Couple of days back I was working with an open source library called fongo which is an in-memory Java implementation for unit-testing MongoDB applications. I wanted to use the library but it does not existed in maven central. I upload the dependency to my nexus instance running on OpenShift and I was able to use the library.

I believe these are good reasons to have your own nexus instance. The best part is we can get our nexus instance running on OpenShift for free in under two minutes.

GitHub repository of blog

You can get the code from github https://github.com/shekhargulati/nexus

Nexus on OpenShift in two minutes

Follow the following steps to get Nexus running on OpenShift. In case you have not signed for OpenShift and not installed client tools, please refer to getting started tutorial.

  1. The first step is to create diy application on OpenShift. The reason I am not using JBoss is because you get exceptions when you try to deploy nexus on any application server. It works fine on web server but gives exception on application server like JBoss. You can refer to this   stackoverflow question for more information.

    rhc app create -a nexus -t diy-0.1
  2. Once you have created the application you have to pull the code from my github repository. To do that type the following commands on the command line.

    git remote add nexus git@github.com:shekhargulati/nexus.git
    git pull -s recursive -X theirs nexus master
    git push
  3. Next view your nexus instance running at  http://nexus-.rhcloud.com/nexus/. You can login to nexus with credentials(username and password) admin/admin123.

Under the Hood

Now that you have got your nexus instance running in cloud lets talk in detail about what have we have done in three steps mentioned above. The first and last step didn't do anything special. The second step is where all the magic happens. So, lets take a look at that.

  1. In the second command we pulled the code from github. In the diy directory you will see that we have tomcat directory which contains the tomcat installation. As I told you above nexus war does not work on JBoss so we are installing tomcat on the OpenShift. You can refer to Grant's blog in case you want to know in detail about how to setup tomcat on OpenShift.
  2. In the webapps directory under tomcat you can see that we have nexus.war file. This is the war that you can download from Sonatype website http://www.sonatype.org/nexus/go . We are deploying latest version i.e. 2.1.2 of nexus on OpenShift.
  3. Nexus requires a directory to contains all of the repository and configuration data. By default, this directory is always a sibling to the Nexus installation directory but you can customize it by altering the nexus-work property in plexus.properties file which exist in war file or by creating an environment variable with name PLEXUS_NEXUS_WORK. So, if you look at start script which exist under .openshift / action_hooks / directory you will find a declaration as shown below. The $OPENSHIFT_DATA_DIR is an environment variable exposed by OpenShift for the persistent data directory location. OpenShift data directory will persist data between pushes of your repo.
export PLEXUS_NEXUS_WORK=$OPENSHIFT_DATA_DIR

Lets use it -- Creating Repository Group

Now that we have nexus running it make sense to use it in a real scenario. You can use it for uploading artifacts or releasing artifacts to nexus or any other use case. The use case that I am going to show is of grouping repositories together so that you can use a single repository in your pom.xml. Lets take a look at the pom.xml snippet shown below.

  <repositories>       
    <repository>         
      <id>springsource-repo</id>         
      <name>SpringSource Repository</name>           
      <url>http://repo.springsource.org/release</url>        
    </repository>        
    <repository>         
      <id>springsource-milestone</id>            
      <name>SpringSource Milestone Repository</name>         
      <url>http://repo.springsource.org/milestone/</url>     
    </repository>        
    <repository>         
      <id>spring-release</id>            
      <name>Spring Maven Release Repository</name>           
      <url>http://repo.springsource.org/libs-release</url>       
    </repository>
  </repositories>

The snippet above shows a project using three different repositories for Spring related dependencies. Wouldn't it be great if we just have to specify single repository as shown below?

  <repositories>   
    <repository>         
      <id>shekhar-nexus</id>  
      <url>http://nexus-ideas.rhcloud.com/nexus/content/groups/com.shekhar/</url>  
    </repository>
  </repositories>

To achieve this you have to first create three proxy repositories -- one each for springsource-repo, springsource-milestone, and spring-release. To create a proxy repository, login to nexus using username/password as admin/admin123 and click on Repositories. Configuration for one of the proxy repository is shown below.

Once you have created all the proxy repositories and click on add > Repository Group button. Enter values for  Group Id, Group Name, and select all the repositries you want to group together. Make sure Publish URL is true so that you get a valid url for the group repository. The snapshot is shown below.

Finally specify the single repository in url as shown below and everything would work fine.

  <repositories> 
    <repository>         
      <id>shekhar-nexus</id>  
      <url>http://nexus-ideas.rhcloud.com/nexus/content/groups/com.shekhar/</url>  
    </repository>
  </repositories>

That's it for this blog.

Note: You can get the latest nexus code from github at https://github.com/sonatype/nexus

OpenShift

News Source Openshift