Wednesday 10 July 2019

Maven Repositories

 

A repository is a storage location, where maven stores all the build artifacts and dependencies.

 

There are three types of repositories.

a.    Local repository

b.    Remote repository

c.    Maven central repositories

 

Whenever maven wants a dependency, it searches in the local repository first, if the dependency is not present in the local repository, it downloads the dependency from maven central or configured remote repositories and place it in local repository.




Local Repository

While building the artifact, maven scans all the project dependencies, if a dependency artifact is not presented in the local repository (.m2), maven download the artifacts from the maven central repository and from other configured remote repositories, copy the artifact to the local repository.


Location of Local Repository

maven local repository is presented at %USER_HOME%/.m2. For example, in my case, it is located at ‘C:\Users\krishna\.m2’


Can I change the location of local repository?
Yes, you can change the location of local repository by updating the element <localRepository> in settings.xml file.

settings.xml file is located at %MAVEN_INSTALLATION_DIR%\CONF\settings.xml

In my case, the settings.xml file is located at C:\Users\krishna\Documents\Maven\softwares\apache-maven-3.5.3\conf.

<!-- localRepository
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ${user.home}/.m2/repository
  <localRepository>/path/to/local/repo</localRepository>
  -->

Remote Repository
Remote repositories are in the web, they can be accessed by variety of protocols like file:// and http://.

Example of remote repository

"remote" repositories may be internal repositories set up on a file or HTTP server within your company, used to share private artifacts between development teams and for releases.
 

What is the maven public or central repository?

https://repo1.maven.org/maven2/ : This repository is hosted by the maven community and available for public usage.



How can I use internal repositories?
Use the <repositories> element in pom.xml file to specify the internal repository details.

<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">
 
 <repositories>
  <repository>
   <id>my-repo1</id>
   <name>your custom repo</name>
   <url>http://jarsm2.dyndns.dk</url>
  </repository>
  <repository>
   <id>my-repo2</id>
   <name>your custom repo</name>
   <url>http://jarsm2.dyndns.dk</url>
  </repository>
 </repositories>

</project>


If your internal repository requires authentication, the id element can be used in your settings file to specify login information.

Where is my copied dependency location in local repository?

 

Let me explain this with an example.

<dependency>
	<groupId>com.google.code.gson</groupId>
	<artifactId>gson</artifactId>
	<version>2.9.0</version>
</dependency>

 

‘groupId’, ‘artifactId’ and ‘version’ together used to uniquely identify the artifact. By default, all the artefacts are downloaded to {USER_HOME}/.m2/repository folder. groupId, artifacteId and version are transformed to directories in the local file system.

 

For example, the groupId ‘com.google.code.gson’ is changed to ‘com/google/code/gson’.

$ls ~/.m2/repository/com/google/code/gson/gson/2.9.0
_remote.repositories		gson-2.9.0.jar.sha1
gson-2.9.0-sources.jar		gson-2.9.0.pom
gson-2.9.0-sources.jar.sha1	gson-2.9.0.pom.sha1
gson-2.9.0.jar			m2e-lastUpdated.properties

 

 

 



Previous                                                    Next                                                    Home

No comments:

Post a Comment