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 dependency management. Most of maven builds produces artifacts, also projects, of course, depend on another artifact (e.g java jar files) in different versions and configurations. It’s all about managing the artifacts.

Every maven installation already brings a local repository that may be sufficient for a 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 Bandwidth
  • 3dt party artifact predictability
  • Intra/inter organizational collaboration and distribution of work

Installation & Configuration

The Following describes the installation of nexus on Debian Linux (Wheezy release). So let’s start…

One pre requisite is an installed Java 5 (JRE) or higher.


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

#change to work dir
cd /tmp
#Then download a fresh version of the 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 only directory from archive. No need of extracting working dir.
tar xzvf /tmp/nexus-2.1.2-bundle.tar.gz nexus-2.1.2/

# Creating a new symlink to avoid the version in the path.
ln -s nexus-2.1.2/ nexus

Now basics are done, but not start Nexus now please. I don’t like the idea 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 and start it automatically. Last but not least I have to provide some configuration for example a “run as” Linux user for Nexus.

Let’s agree the repository should be in a directory: /srv/nexus/main-repo then to do 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

Congratulation! Now your Nexus installation is available on http://localhost:8081/nexus/. The default user and password are admin and admin123 :)

Basic configuration of the maven clients

Log in 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 flexible 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 this 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.