Software DevelopmentLearn About How to Use Maven Repositories

Learn About How to Use Maven Repositories

In this article, we are going to discuss the use of different types of Maven repositories which are used to download library JARs, etc. during building a Maven project. A Maven repository can be defined as a directory where all the projects APIs such as JARs, library JARs, plugins, etc. are placed that can be readily used by Maven when required. There are three types of Maven repositories as specified below.

• Local Maven Repository
• Central Maven Repository
• Remote Maven Repository

Local Maven Repository
A local Maven repository is nothing but a folder which is located on your local machine. Maven plugin creates a folder when you execute any maven command for the very first time. The purpose of local repository is to keep all your project related dependencies such as library JARs, plugin JARs, etc. in one place. These dependencies are automatically downloaded into your local repository when you kick in a Maven build. Such a practice is very beneficial as it helps to mitigate the references to dependencies again and again which are available on remote machine during building a Maven project.

By default, a local Maven repository is created at %USER_HOME% directory by Maven plugin. If you want to override this default location, then you need to provide a different path in Maven’s setting.xml file which is present at %M2_HOME%\conf directory. The following will be the content of setting.xml file which has the customized path defined for local Maven repository.

<settings 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/settings-1.0.0.xsd">
   <mirrors>
      <mirror>
         <id>maven.dev.snaponglobal.com</id>
         <name>Internal artifact Maven repository</name>
         <url>http://repo1.maven.org/maven2/</url>
         <mirrorOf>*</mirrorOf>
      </mirror>
   </mirrors>
   <localRepository>C:/CustomizedLocalRepository</localRepository>
</settings>

Upon execution of the Maven command, the required project dependencies will be downloaded at your given customized path on your local machine.

Central Maven Repository
Central Maven repository is referred to the repository which is provided by Maven community. It is a giant repository that contains a large number of library JARs which are used very commonly. The following are the key concepts of central Maven repository.

• It is a repository which is wholly managed by the Maven community.
• No configuration is required for this repository.
• Central repository can be searched through internet access. Its content can be browsed through a URL − https://search.maven.org/#browse provided by the Maven community. All the library JARs which are available at central repository can be searched and used by a developer through this URL.

Remote Maven Repository
Remote Maven repository is very useful and comes into the picture when the required library JARs are not even spotted in the central Maven repository that may result into the failure of build process after throwing the error message on the console. In order to mitigate such situations, the concept of remote Maven repository comes into the picture, which is nothing but a customized repository that contains the required project’s library JARs. In the following example, the POM.xml has a number of remote repositories which will download the required dependencies (i.e. library JARs) when they are not present on the local repository as well as central repository.

<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.eduonix</groupId>
	<artifactId>springframework.myfirstapp</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>springframework.myfirstapp</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>
	<build>
		<resources>
			<resource>
				<directory>src/main/resources</directory>
				<filtering>true</filtering>
				<targetPath>${project.build.directory}</targetPath>
			</resource>
		</resources>
	</build>
	<repositories>
		<repository>
			<id>org.springframework.spring-core</id>
			<url>https://mvnrepository.com/artifact/org.springframework/spring-core</url>
		</repository>
		<repository>
			<id>org.springframework.spring-webmvc</id>
			<url>https://mvnrepository.com/artifact/org.springframework/spring-webmvc</url>
		</repository>
		<repository>
			<id>org.springframework.spring-test</id>
			<url>https://mvnrepository.com/artifact/org.springframework/spring-test</url>
		</repository>
		<repository>
			<id>org.springframework.spring-aspects</id>
			<url>https://mvnrepository.com/artifact/org.springframework/spring-aspects</url>
		</repository>
		<repository>
			<id>org.springframework.spring-jdbc</id>
			<url>https://mvnrepository.com/artifact/org.springframework/spring-jdbc</url>
		</repository>
		<repository>
			<id>org.springframework.spring-jms</id>
			<url>https://mvnrepository.com/artifact/org.springframework/spring-jms</url>
		</repository>
		<repository>
			<id>org.springframework.spring-messaging</id>
			<url>https://mvnrepository.com/artifact/org.springframework/spring-messaging</url>
		</repository>
		<repository>
			<id>org.springframework.spring-orm</id>
			<url>https://mvnrepository.com/artifact/org.springframework/spring-orm</url>
		</repository>
		<repository>
			<id>org.springframework.spring-orm</id>
			<url>https://mvnrepository.com/artifact/org.springframework/spring-oxm</url>
		</repository>
		<repository>
			<id>org.springframework.spring-tx</id>
			<url>https://mvnrepository.com/artifact/org.springframework/spring-tx</url>
		</repository>
		<repository>
			<id>org.springframework.spring-webmvc-portlet</id>
			<url>https://mvnrepository.com/artifact/org.springframework/spring-webmvc-portlet</url>
		</repository>
		<repository>
			<id>org.springframework.spring-websocket</id>
			<url>https://mvnrepository.com/artifact/org.springframework/spring-websocket</url>
		</repository>
		<repository>
			<id>org.springframework.spring-instrument</id>
			<url>https://mvnrepository.com/artifact/org.springframework/spring-instrument</url>
		</repository>
		<repository>
			<id>org.springframework.spring-instrument-tomcat</id>
			<url>https://mvnrepository.com/artifact/org.springframework/spring-instrument-tomcat</url>
		</repository>
		<repository>
			<id>org.springframework.spring-instrument-tomcat</id>
			<url>https://mvnrepository.com/artifact/org.springframework/spring-context-support</url>
		</repository>
	</repositories>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>4.3.9.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>4.3.9.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>4.3.9.RELEASE</version>
			<scope>test</scope>
		</dependency>
<!-- 		 <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> 
			<version>4.3.9.RELEASE</version> </dependency>  -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>4.3.9.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jms</artifactId>
			<version>4.3.9.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-messaging</artifactId>
			<version>4.3.9.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
			<version>4.3.9.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-oxm</artifactId>
			<version>4.3.9.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>4.3.9.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc-portlet</artifactId>
			<version>4.3.9.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-websocket</artifactId>
			<version>4.3.9.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-instrument</artifactId>
			<version>4.3.9.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-instrument-tomcat</artifactId>
			<version>4.3.9.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>4.3.9.RELEASE</version>
		</dependency>

	</dependencies>
</project>

The above POM.xml will download all the dependencies specified in it through the repository URLs present under repositories/repository elements.

Source code for the different maven repositories

Conclusion: –
In this article, we discussed about the different Maven repositories along with a suitable example for each of the type.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -