Home > Tutorial > maven > Maven Add Repository

Maven Add Repository

This tutorial will go through the steps to add a new or additional Maven remote repository in your pom.xml file using Eclipse IDE. Maven can have both remote and local repositories. We will build on the previous tutorial that went step by step on how to Add Maven Dependency.


What is a Maven Repository?

A repository in terms of Maven is used to hold artifacts and dependencies of varying types. My main task is a Java developer so our focus is on java related dependency jars. A local Maven repository is a copy of all downloaded dependencies from the remote repository. On the other hand, a remote repository refers to remote repository accessed by file:// or http:// protocols.


Show Maven Repositories

Eclipse has a nice and easy way to view all of the repositories configured in your pom.xml file. In Eclipse, go to Window » Show View » Other » Maven » Maven Repositories which will show a new view. You can see the local repository, which corresponds to your local Maven installation path. The Global Repositories are the remote repositories. By default, all new Maven projects are configured to the Maven Central Repository.


Add Maven Repository

Most Java dependency packages you would want to add to your Maven project can be found in the default Maven Central Repository. However, if a needed dependency is not there or you need an older version that is not in Maven Central, you will need to add a new remote repository. For our purposes here, our Java project needs an older version of snmp4j, which can only be downloaded from the SNMP4j Repository. We will first add the SNMP4J repository URL and then add the dependency to the pom.xml file.

Step 1 – Add Repository Info to Pom.xml

 <repositories>
    <repository>
      <id>snmp4j.repo</id>
      <url>https://oosnmp.net/dist/release</url>
    </repository>
  </repositories>


After adding the repository to pom.xml file, save it and you will now see the new remote repository in the Maven Repositories view in Eclipse under Project Repositories.

Step 2 – Add Dependency Info to Pom.xml

<dependency>
      <groupId>org.snmp4j</groupId>
      <artifactId>snmp4j</artifactId>
      <version>2.0.0</version>
    </dependency>


After adding the dependency to the pom.xml file, save it and you will now see the new snmp4j package in your local Maven Repository. As a note if you include a version number that does not exist in a remote repository, Maven will sow you an error.


Import Package Into Java Project

At this point, you can now import and start using the new dependency package that we just downloaded from the remote Maven repository to your local Maven repository. The sample code snippet below show the successful import of snmp4j package.

package com.jcd.java.tutorials.MavenProject;

import org.snmp4j.PDU;

public class App 
{
    public static void main( String[] args )
    {
        
        //Create new PDU instance for our snmp activities
        PDU pdu = new PDU();
        pdu.setType(PDU.GET);
        
    }
}


Let me also share my pom.xml file after adding the new remote repository and the package dependency so you can see what it looks like.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.jcd.java.tutorials</groupId>
  <artifactId>MavenProject</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>MavenProject</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  
  <repositories>
    <repository>
      <id>snmp4j.repo</id>
      <url>https://oosnmp.net/dist/release</url>
    </repository>
  </repositories>
  
 
  <dependencies>
  
  <dependency>
      <groupId>org.snmp4j</groupId>
      <artifactId>snmp4j</artifactId>
      <version>2.0.0</version>
    </dependency>
  
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
    </dependency> 
    
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-lang3</artifactId>
      <version>3.7</version>
    </dependency> 
    
  </dependencies>
</project>
Smedley
Posted by

Smedley

Smedley has been programming in Java for about twenty years. He enjoys sharing code samples to other fellow Java coders.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

Copyright © 2018 JavaCodeDepot