Showing posts with label repository. Show all posts
Showing posts with label repository. Show all posts

Sunday, 31 July 2022

Quick guide to maven local repository

 

Maven support three kinds of repositories.

a.   Local repository: It is a is a directory on the computer where Maven runs. It caches remote downloads and contains temporary build artifacts that you have not yet released.

b.   Remote repository: Custom repository provided by your organization. In general, every organization configures a remote repository, where maven download the dependencies that are not exist in the local repository.

c.    Central repository: Repository provided by Maven community (https://repo1.maven.org/maven2/).

 

 


Typical flow looks like below.

a.   When you build a project that require dependency ‘X’.

b.   Maven check for the dependency ‘X’ in the local repository. If it exist, it build the artefact with given dependency. If the dependency is not exist in the local repository, maven contact the remote repository, download the dependency to local repository, and build the artifact.

 

Location of maven local repository

Default local repository location varies based on the operating system, find the following details.

 

Windows: 

C:\Users\<user_name>\.m2


Linux: 

/home/<user_name>/.m2 or ~/.m2


Mac: 

/Users/<user_name>/.m2 or ~/.m2


Customize maven local repository

You can customize the local repository location by updating the ‘localRepository’ property in the file '{MAVEN_HOME}\conf\settings.xml'.

<settings xmlns="http://maven.apache.org/SETTINGS/1.2.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 https://maven.apache.org/xsd/settings-1.2.0.xsd">
  <!-- localRepository
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ${user.home}/.m2/repository
  <localRepository>/path/to/local/repo</localRepository>
  -->

  <localRepository>/Users/krishna/Documents/my_m2</localRepository>

</settings>

Can I pass my custom local repository location to the maven command?

Yes, you can specify the custom local repository location to the maven command using the argument -Dmaven.repo.local.

mvn -Dmaven.repo.local=/Users/krishna/Documents/my_m2 clean install

References

https://maven.apache.org/guides/introduction/introduction-to-repositories.html


Previous                                                 Next                                                 Home

Friday, 4 October 2019

Introduction to Nexus Repository Manager

Nexus is a repository manager. We can publish our artefacts to nexus, so they can be available for other developers.

What is Repository?
Repository is a warehouse where the artefacts are stored and retrieved. For example, if you are from java background, jar, war files are example of artefacts.

In a typical application, we use third party libraries which are hosted in a repository like ‘https://mvnrepository.com/’. Whenever your application needs these libraries application contact the repository and get them.



What is a Repository Manager?
Repository manager is a system that manages all the repositories that your development teams required. Nexus is an example of repository manager.


Why nexus?
a.   It hosts repositories for internal artefacts. We can’t host organisational specific artefacts in public repository, so nexus help here.
b.   It proxies remote repositories and cache the artefacts.
c.    We can group repositories and user can access the artefacts from single endpoint.
d.   You can audit the artefacts that are used in various applications across the organisations.
e.    You can host the artefacts, that are not available in remote/public repositories.
f.     You can control the access to resources by providing authentication and authorization.
g.   It allows you to control access to, and deployment of, every component in your organization from a single location.



Previous                                                    Next                                                    Home

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