If you have missed my previous post on maven basic terminologies then you can read it here . In this post we will roam around Maven Central Repository, how they work , what are their significance in Maven, How you can customize Maven Repositories and lot of other things about maven repositories.
What is a Maven Repository:
When you invoke maven reactor for the first time by calling any maven build life cycle , goal , plugin etc , you might have noticed that maven started downloading a lot of files from a url , these files are nothing but the artifacts and plugins that are required to complete the build, and here the url refers to a location where all these files (artifacts and plugins) are present.
By default maven shipped with a default remote repository location i.e http://central.maven.org/maven2/ and from here it tries to download all the files and then it will store it into your local maven repository. The advantage of storing it in a local repository is , when the next time you build your maven project it don’t have to go to remote repository for downloading the files as it will be available in your local repo , hence it will use it from local system.
Standard location of this maven local repository varies with the underlying operating system , e.g : On Windows XP, your local repository is likely in C:Documents and SettingsUSERNAME.m2repository, and on Windows Vista, your local repository is in C:UsersUSERNAME.m2repository. On Unix systems, your local Maven repository is available in ~/.m2/repository.
How Maven Repository Works :
Everything stored in Maven Repository follows standard directory structure format that is used by Maven when it looks for any artifact in the maven central repository. Path for any of the artifact is always determined by the by its maven coordinates , maven reactor scans the path provided in coordinates and after reaching the target location it downloads artifacts from there and install it in your local maven repository.
For example : Maven coordinates for activemq-all jar file will be like this.
<groupId>org.apache.activemq</groupId> <artifactId>activemq-all</artifactId> <version>5.0.0</version>
So, location in maven central repository will be something like this.
As a general rule location will be look like this.
In our example packaging was jar , it can also be a pom.
Configure External Remote Repositories :
Now it might be possible that their exists some artifacts that are not available in Maven Central Repository in that case your build might break as it is not able to resolve all the artifacts successfully. Don’t worry maven also provides solution to these types of problems, only difference is that you have to take some action and have to add some other remote repositories so that if any artifact is not available in maven central repository then it can look into other repositories that you have configured.This can be achieved in 2 ways , you can pick any of those solution as per your requirements.
Solution 1: Suppose there are some artifacts that are required only for a particular maven project not by all other remaining other modules , in this case you can configure your other remote repositories in that project specific pom itself ,so that it will look into these other remote repositories only for this maven project not for all others. For doing this you have to add the repositories in your pom.xml file under project like this.
<repositories> <repository> <id>repository1</id> <url>http://repository.example.com/maven</url> </repository> <repository> <id>repository2</id> <url>http://repository.xyz.org/maven2</url> </repository> </repositories>
In the above example we have configured two remote repositories , so that whenever any artifact is missing in Maven Central Repository , maven reactor will look into these two additional repositories for that artifact.
Solution 2: Suppose you want to generalize this rule across all of your modules such that while building any of the maven project if any of the artifact is not available in Maven Central Repo then it should check other remote repositories also, for imposing this rule across all of your projects you can configure these remote repositories in settings.xml that comes with apache maven bundle under profile tag.
<repositories> <repository> <id>repsoitory1</id> <name>Repository 1</name> <releases> <enabled>false</enabled> <updatePolicy>always</updatePolicy> <checksumPolicy>warn</checksumPolicy> </releases> <snapshots> <enabled>true</enabled> <updatePolicy>never</updatePolicy> <checksumPolicy>fail</checksumPolicy> </snapshots> <url>http://repository.example.com/maven</url> <layout>default</layout> </repository> </repositories>
This way you can configure as many repositories you want for all of your maven projects.
For detail of each and every attribute of repository you can visit here.
So, till now we have understand that what is a maven repository , how it works and how to configure other remote repositories.
If you have any question or suggestion , please drop a comment and we will discuss on that.
Thanks for dropping by.
Latest posts by Saurabh Jain (see all)
- java.lang.IncompatibleClassChangeError: Found interface org.apache.hadoop.mapreduce.TaskInputOutputContext, but class was expected - August 8, 2014
- org.datanucleus.store.rdbms.exceptions.MappedDatastoreException: INSERT INTO “TABLE_PARAMS” – Hive with Kite Morphlines - July 17, 2014
- java.io.IOException: can not read class parquet.format.PageHeader: null – Hive - July 12, 2014