Installation and Configuration of Sonatype Nexus

There are many people using Maven or Ant for years but do no use a repository manager like Nexus or Artifactory. This article describes how easy it is to setup and run your Nexus - an Artifact Repository Manager.

Motivation

One of the greatest features of maven is the dependency management. Most of maven builds produces artifacts, also projects of course depend on another artifacts (e.g java jar files) in different versions and configurations. It's all about managing the artifacts.

Every maven installation already brings local repository that may be sufficient for single developer. But the more professional your software development is, the more reason some professional repository becomes. Here are some of them...

  • More control (Releases, Dependencies, Audits)
  • Scalability
  • Speeding up of Builds
  • Saved Bandtwidth
  • 3dt party artifact predictability
  • Intra/inter organisational collaboration and distribution of work

and more....

Installation & Configuration

First of all it good practice to thing about low level things like, hardware, bandwidth, operating system and backup strategy... but i don't cover it here. Following describes the installation of nexus on Debian Linux (Wheezy release). So let's start...

One Pre Requirement is installed Java 5 (JRE) or higher.

# Start by creating new user and group, you will prompted do add additional info.
adduser nexus

#change to work dir
cd /tmp  
#Then download fresh version of nexus. In my case v2.1.2
wget www.sonatype.org/downloads/nexus-2.1.2-bundle.tar.gz

#Create nexis basedir and change to it
mkdir /usr/lib/nexus-oss  
cd /usr/lib/nexus-oss/

#Extract nexus-2.1.2 omly directory from archive. No need of extracting working dir.
tar xzvf /tmp/nexus-2.1.2-bundle.tar.gz nexus-2.1.2/

# Creating new symlink to avoit version in path.
ln -s nexus-2.1.2/ nexus  

Now basic's are done. But do not start Nexus now...
I don't want to create "big" artifact repository (or nexus's working directory) in the same default place...
Also i want to register the init.d script to be able to control the nexus server an start it automatically. Last but not least i have to provide some configuration for example a "run as" Linux user for Nexus.

Let's assume the repository should be in directory:
/srv/nexus/main-repo then to the following:

mkdir /srv/nexus/main-repo

#Set owner user and group
chown nexus:nexus /srv/nexus/main-repo  

Now open /usr/lib/nexus-oss/nexus-2.1.2/conf/nexus.properties file and change nexus-work property to:

nexus-work=/srv/nexus/main-repo  

Now we are ready to register the provided init.d script that will allow us to run nexus as a Linux daemon.

# copy init.d sctipt to proper place
cp /usr/lib/nexus-oss/nexus/bin/nexus /etc/init.d/nexus

#replace default location
sed -i "s/NEXUS_HOME=".."/NEXUS_HOME="/usr/lib/nexus-oss/nexus"/g"; /etc/init.d/nexus

#Set PID dir
sed -i -"s/#PIDDIR="."/PIDDIR="/var/run"/"; /etc/init.d/nexus  
#Set RUN_AS user to nexus
sed -i "s/#RUN_AS_USER=/RUN_AS_USER=nexus/" /etc/init.d/nexus  
#now register the new script
update-rc.d nexus defaults  

In case of troubles or for different Linux distributions look here

Congratulation! Now you'r Nexus installation is available on http://localhost:8081/nexus/. The default user and password are
"admin" and "admin123".

Basic configuration of the maven clients

Login as admin, locate predefined repositories. All repositories of type "proxy" need to change "Download Remote Indexes" property  to true in the configuration tab.

As you see there are several types of repositories.

  • proxy - acts as proxy for external repository.
  • hosted - repository that managed artifact produced by you
  • virtual - kind of adapter for e.g transforming maven1 to maven 2 format.
  • group - maybe not a repository in sonatyp's terminology but behaves like one. A group groups several repositories to one exposing result as single URI.
Per default there is  a group "public" present. This group includes all the needed stuff, we just need to tell our maven clients to use this group. Maybe the easiest and flexiblest way to do so, is to use a mirror in settings.xml of your local maven.
<mirrors>  
  <mirror> <id>nexus</id> 
   <mirrorOf>*</mirrorOf> 
   <url>http://YORNEXUSHOST:8081/nexus/content/groups/public</url> 
  </mirror> 
</mirrors>  

Then we use a power of Maven Profiles and define new repositories they are magically (consider "*" in the mirror declaration) maps to the mirror.

<profile>  
<id>nexus</id>  
<!--all requests to nexus via the mirror -->  
<repositories>  
  <repository>
   <id>central</id>
   <url>http://central</url>
   <releases>
     <enabled>true</enabled>
   </releases>
   <snapshots>
    <enabled>true</enabled>
   </snapshots> 
  </repository> 
</repositories>  
<pluginRepositories>  
  <pluginRepository>
    <id>central</id>
    <url>http://central</url>
    <releases>
     <enabled>true</enabled>
    </releases>
    <snapshots><enabled>true</enabled></snapshots> 
 </pluginRepository>
</pluginRepositories>  
</profile>  

Do not forget to activate thins new profile

<activeProfiles>  
   <activeProfile>nexus</activeProfile> 
</activeProfiles>  

Now your maven client knows only your nexus and everything it needs and how it gets it, should be controlled by nexus.

Further Reading & Comparison of Repositories