SAGARFIVE

Tutorials

3.11 Maven Repository & Types

  • Maven repository is a directory where all the project jars, library jar, plugins or any other project specific artifacts are stored and can be used by Maven easily. or
  • A maven repository is a directory of packaged JAR files with a pom.xml file. Maven searches for dependencies in the repositories.
  • Maven repositories are of three types
  • (i) Local Repository
  • (ii) Central Repository
  • (iii) Remote Repository

(i) Local Repository :

  • Maven local repository is located in your local system.
  • It is created by the maven when you run any maven command.
  • It downloads the dependencies mentioned in pom.xml to local repository
  • By default, the maven local repository is %USER_HOME%/.m2 directory.
  • We can change the local repository location by editing maven settings.xml, by changing   <localRepository>

<settings xmlns = “http://maven.apache.org/SETTINGS/1.0.0”   xmlns:xsi = “http://www.w3.org/2001/XMLSchema-instance”   xsi:schemaLocation = “http://maven.apache.org/SETTINGS/1.0.0   http://maven.apache.org/xsd/settings-1.0.0.xsd”>   <localRepository>C:/MyLocalRepository</localRepository></settings>

(ii)  Central Repository :

  • Maven central repository is located on the web. It was created by the apache maven community itself.
  • The URL of the central repository is http://repo1.maven.org/maven2.
  • You can search the dependencies or API at webpage : https://central.sonatype.com/
  • Maintained by the Maven community.

(iii) Remote Repository :

  • Maven Remote Repository, which is developer’s own custom repository containing required libraries or other project jars.
  •  if we haven’t found any API/Dependencies. Maven remote repository is located on the web. Most of the libraries can be missing from the central repository such as JBoss library etc
  • so we need to define a remote repository in the pom.xml file. In the <dependencies> fields as <dependency>
  • Example :

<repositories>     
<repository>         
<id>companyname.lib1</id>         
<url>http://download.companyname.org/maven2/lib1</url>     
</repository> 
</repositories>
<!– https://mvnrepository.com/artifact/org.jboss.spec.javax.servlet/jboss-servlet-api_3.0_spec –><dependency>   
<groupId>org.jboss.spec.javax.servlet</groupId>   
<artifactId>jboss-servlet-api_3.0_spec</artifactId>   
<version>1.0.2.Final</version>   
<scope>provided</scope>
</dependency>

Maven Dependency Search Sequence :

When we execute Maven build commands, Maven starts looking for dependency libraries in the following sequence

1Local RepositorySearch dependency in local repository, if not found, move to step 2 else perform the further processing.
2Central repositorySearch dependency in central repository, if not found and remote repository/repositories is/are mentioned then move to step 4. Else it is downloaded to the local repository for future reference.
3Remote repositoryIf a remote repository has not been mentioned, Maven simply stops the processing and throws an error (Unable to find dependency).
Search dependency in remote repository or repositories, if found then it is downloaded to local repository for future reference.Otherwise, Maven stops processing and throws error (Unable to find dependency).