Tuesday, September 3, 2013

How Maven find dependency JARs while building Java Project

One of the most attractive feature of Apache Maven framework it to manage dependency JARs, but do you know, How Maven finds dependency JAR while building Java project? From where does it download all JAR files? How does Maven search for a particular dependency? In fact, these are some of the frequently asked questions by Maven beginners. Many Java programmer started using Maven as build tool, because of it's dependency management advantages. Since almost all Java project uses lot's of open source libraries, which means adding multiple JAR into classpath. In fact, this dependency on JAR file is one of the most time consuming part of setting up a Java project in Eclipse. Maven comes in picture, and allows you to just declare dependency in pom.xml file, rest will be taken care by Maven. In this article, we will take a closer look on How Maven manages dependencies and How does it finds respective JAR files during build  process.

How Maven searches for dependency JAR.

How Maven search dependency JAR working
So we know that a project's dependencies are declared in pom.xml. Apache Maven uses concept of repository to hold JAR files. There are two kinds of repository, local repository and remote repository. You can define this in settings.xml on maven/conf directory or pom.xml itself. When we kick off Maven build process by executing mvn install command, it first search for dependency JAR in local repository, if it found there it uses, otherwise it goes to remote repository to download corresponding version of JAR file and stores into local repo as well. There is one more thing, which is worth remember is type of dependency. If you have included a SNAPSHOT JAR e.g. <version>1.2-SNAPSOT</version> as part of your dependency, opposite to regular stable version <version>1.2</version, than Maven will hit remote repository, even if a relevant JAR file is available in local repository. Main reason of doing this is that, SNAPSHOT builds are unstable and Maven, downloads SNAPSHOT dependency daily. It might not hit remote repository, if it has just downloaded the JAR, but once again this is configurable. You can change update policy for SNAPSHOT builds from default daily to always, in a interval or none. In your repository definition


where POLICY can be always, daily, interval:200 or never. Where always means Maven will check for new version every time you will build the project. daily means once per day, interval:200 means in a interval of 200 minutes and never means, Maven will not attempt to check for newer version, until that JAR is available in local repository. One more thing, which is worth knowing is organization of dependency. In a maven repository, dependency JAR is arranged as per there groupId, artifactId and version, and name of JAR file is combination or artifactId and version mentioned in dependency tag of pom.xml. For example, following dependency on SLF4J logging library


will be found in local repository location C:\MAVEN_LOCAL_REPOSITORY\org\slf4j\slf4j-log4j12\1.6.4\slf4j-log4j12-1.6.4.jar , you can see name of the JAR file is combination of artifactId and version number.

That's all on How Maven finds dependency JAR while building Java project. This is the minimum knowledge, every Java programmer as an Apache Maven user, should have. Some time maven issues are really tricky to fix, when you see a correct pom.xml but still maven not able to find dependencies, good place to check is your settings.xml and network settings. We will learn about Maven build issues in some other article, but basic knowledge of maven repository and when maven downloads dependency is worth remembering.

Other Programming and Software development articles from Javarevisited Blog

1 comment :

Anonymous said...

you can also use this tool to search and download jars with dependencies: http://jar-download.com/index.php

Post a Comment