Jenkins and Nexus
DevOps & Cloud Solutions, Managed Services

Integrating Nexus Repository OSS 3.x with Jenkins to store build artifacts.

In the time of Continuous Integration and Continuous Delivery, projects are getting built on weekly, daily or even hourly basis. With the advent of Continuous Integration (CI) servers such as Jenkins, software development has become more about building projects on a continuous basis rather than deploying them periodically.
By developing a mechanism to store, organize and keep track of the artifacts generated by these builds, we can pinpoint our deployments to choose to-be-deployed artifacts on the basis of timestamps of the particular builds, build number etc.
One way to implement this mechanism is by leveraging Jenkins and Nexus Repository OSS. Jenkins is arguably the most popular automation server which can be used to automate tasks related to building, testing and deploying software and Nexus Repository OSS is a widely used free artifact repository which can be used to store binaries and build artifacts.
So, here is a step-by-step guide which can help in creating the environment discussed above by integrating these two tools.

Configuring Nexus Repository Manager (CentOS 7)

  1. Navigate to /opt directory
    $ cd /opt
  2. Download UNIX archive for latest version with direct download link
    $ sudo wget (Check for the latest link here
  3.  Extract the downloaded file
    $ sudo  tar xvzf  latest-unix.tar.gz
  4. Rename the resulting directory(nexus-3.10.0-04) to nexus
    $ sudo mv nexus-3.10.0-04 nexus
  5. Add a user for nexus
    $ sudo adduser nexus
  6. Change ownership of nexus and sonatype-work directories
    $ sudo chown -R nexus:nexus /opt/nexus
    $ sudo chown -R nexus:nexus /opt/sonatype-work
  7. Edit nexus.rc file to make Nexus run as nexus user
    $ sudo vi /opt/nexus/bin/nexus.rc
    Add this line to it:
  8. Configure Nexus as a service
    $ sudo ln -s /opt/nexus/bin/nexus /etc/init.d/nexus
  9. Enable nexus service to start at boot
    $ sudo chkconfig –add nexus

    $ sudo chkconfig –levels 345 nexus on

  10. Start Nexus
    $ sudo service nexus start


Create New Repository in Nexus

  1. Log in to your Nexus server at http://yourserverurl:8081 (Default Username: admin, Default Password: admin123)
  2. Click on Settings/Gear icon. Go to Repositories
  3. Click on Create Repository
  4. Choose “maven2 (hosted)” from the list
  5. Enter the desired name (e.g. test-repo)
  6. Choose Version Policy as “Mixed”, Layout policy as “Permissive” and Deployment policy as “Allow Redeploy”. These properties allow us to create a custom hierarchical directory structure to store artifacts in our repository.
    Repository properties
  7. Click on Create repository.
  8. The repository URL will be used while storing artifacts. It can be found here
    Copy Repository URL


Jenkins Job Configuration

  1. Create a new job. Choose “Maven Project” and give the SCM link and build steps (e.g “clean package”) or configure Step 2 in your existing job.
  2. In post-build steps choose “Execute Shell” and execute the script that contains URL to the repository.
    TIME=`date +%d%m%Y_%H%M`
    cd /var/lib/jenkins/workspace/projectname
    mkdir zipfiles
    find /var/lib/jenkins/workspace/projectname -name "*.zip" -not -path "./zipfiles/*" -exec cp -rf {} zipfiles \;
    cd zipfiles
    find /var/lib/jenkins/workspace/projectname/zipfiles -name "*.zip" -exec basename {} .zip \; >> filenames
    #Nexus Details
    #Repository URL
    echo " Uploading Artifacts to Nexus"
    while IFS= read -r LINE; do
    curl -v -u $User:$Passwd --upload-file $ http://$Nexus_Host:$Nexus_Port$URL/$timestamp/$groupid/$artifactid/$LINE-$
    if [ $? -eq 0 ] ; then
    echo "Installing Artifacts to Nexus Succeeded" >> /tmp/jenkinslog
    echo "Installing Artifacts to Nexus Failed" 1>&2 >> /tmp/jenkinslog
    done < "$file"

    This stores the artifacts generated by the build in the “sample-repo” of Nexus Repository Manager 3.10.0 categorized by the timestamps of the builds. The build number is also appended to the artifacts which can be done by using the environment variables provided in the build by Jenkins.

    Please Note that this job builds a project which gives .zip artifacts. The script can be modified to make it work with any format of artifacts.


We have successfully stored the build artifacts in our repository. We can fetch these from the repository and deploy to the servers as and when required. This whole environment is highly customizable. With minimal changes in the script we can customize the hierarchical directory structure as per our requirement, for example instead of timestamp we can create a root directory on the basis of the date of build and all the builds on that day will have separate directories of their own inside that root directory.

We can also create a Jenkins job with parameters where the user can choose the deployment environment and particular date and/or timestamp and/or build number etc whose artifacts they need to deploy and can do that with just one click. Seems like a good “nexus”. Doesn’t it?

About The Author

Leave a Reply