Friday, March 16, 2012

Maven - repository

Maven has a central repository where people publish their jars using a set of 3 coordinates:

orgId: This is usually (but not always) the reverse internet domain name, like org.apache.ant, org.jboss, org.hibernate, com.oracle, etc

artifactId: This is the name of the jar, like ant, hibernate-core, lucene, etc

version: This is the version number of the jar, usually of the form 1.2.3, but can be something like 1.0.SNAPSHOT or 2011.03.12, etc

When we specify a dependency in our pom.xml, we specify the co-ordinates - and Maven looks for the jar in the repository using these coordinates.

Here is an example of a apache camel dependency in pom.xml:
      <dependencies>   
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
<version>2.9.0</version>
</dependency>
</dependencies>


Maven also maintains a local repository in your <home_dir>/.m2 folder.

Whenever it downloads a jar from the central repository, it makes a copy of it in the local repository. This makes subsequent builds go faster, because it can just pull the jar from the local repository.

Jars are stored in the repository in the following directory pattern:
<home_dir>/.m2/repository/<orgId>/<artifactId>/<version>/artifactId-version.jar



While Maven Central Repository is the biggest repository, others can maintain their own repositories as well. For example, jboss maintains its own repository. Many companies maintain an internal repository for their own projects.

By default, Maven will always look for jars in the central repository.

To tell Maven to look in another repository, you can add the repository to your pom.xml.

For example, here is a pom.xml that points to the JBoss repository in addition to the Maven Central Repository, so it can find the richfaces jar:
  <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>org.confucius</groupId>
<artifactId>HelloWorldJARArch</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>HelloWorldJARArch</name>
<url>http://maven.apache.org</url>

<repositories>
<repository>
<id>jboss</id>
<name>JBoss Repository</name>
<url>http://repository.jboss.org/nexus</url>
</repository>
<repository>
<id>central</id>
<name>Central Repository</name>
<url>http://repo.maven.apache.org/maven2</url>
</repository>
</repositories>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.richfaces.core</groupId>
<artifactId>richfaces-core-impl</artifactId>
<version>4.3.0-SNAPSHOT</version>
</dependency>
</dependencies>

</project>

No comments: