Maven dependency mechanism, how it works

Maven’s dependency mechanism help to download all the necessary dependency libraries automatically, and maintain the version upgrade as well.

Case study

Let see a case study to understand how it works. Assume you want to use Log4J as your project logging mechanism. Here is what you do…

1. In traditional way

  1. Visit http://logging.apache.org/log4j/
  2. Download the Log4j jar library
  3. Copy jar to project classpath
  4. Include it into your project dependency manually
  5. All manages by yourself, you need to do everything

If there is a Log4j version upgrade, you need to repeat above steps again.

2. In Maven way

  1. You need to know the log4j Maven coordinates, for example
    	<groupId>log4j</groupId>
    	<artifactId>log4j</artifactId>
    	<version>1.2.14</version>

    It will download the log4j version 1.2.14 library automatically. If the “version” tag is ignored, it will upgrade the library automatically when there is a newer version.

  2. Declares Maven coordinates into pom.xml file.
    <dependencies>
        <dependency>
    	<groupId>log4j</groupId>
    	<artifactId>log4j</artifactId>
    	<version>1.2.14</version>
        </dependency>
    </dependencies>
  3. When Maven is compiling or building, the log4j jar will be downloaded automatically and put it into your Maven local repository.
  4. All manages by Maven.

Explanation

See the different? So what just happened in Maven? When you build a Maven’s project, the pom.xml file will be parsed, if it see the log4j Maven coordinate, then Maven search the log4j library in this order :

  1. Search log4j in Maven local repository.
  2. Search log4j in Maven central repository.
  3. Search log4j in Maven remote repository (if defined in pom.xml).

This Maven dependency library management is a very nice tool, and save you a lot of work.

How to find the Maven coordinates?
Visit this Maven center repository, search the jar you want to download.

Reference

  1. Introduction to the Dependency Mechanism
Tags :

About the Author

mkyong
Founder of Mkyong.com and HostingCompass.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

  • Pingback: over here()

  • Pingback: click this()

  • Pingback: here()

  • Pingback: alkaline water()

  • Pingback: water ionizer()

  • Pingback: electricity()

  • Pingback: loan payment plan()

  • Pingback: payment plan()

  • Pingback: eprofiling electrician()

  • Pingback: house blue()

  • Pingback: w cope plumber nairn()

  • Pingback: plumber greenville sc()

  • Pingback: locksmiths kaiserslautern mwr()

  • Pingback: emergency locksmith services()

  • Pingback: electrician license ma()

  • Pingback: electrician yelp()

  • Pingback: alkaline water brands()

  • Pingback: water ionizer loan()

  • Pingback: laan penge nu 18 aar()

  • Pingback: water ionizers()

  • Pingback: car parking()

  • Pingback: alkaline water machine()

  • Pingback: laan penge online()

  • Pingback: stop parking()

  • Pingback: mobile porn()

  • Pingback: DirectTV()

  • Pingback: tvpackages.net()

  • Pingback: online casinos()

  • Pingback: gloving()

  • Pingback: petunia seeds()

  • Pingback: kangen water machine()

  • Pingback: alkaline water()

  • Pingback: water purification systems()

  • Pingback: water ionizer()

  • Pingback: watch tv show episodes()

  • Pingback: stream movies()

  • Pingback: stream movies()

  • Pingback: Blue Coaster33()

  • Pingback: Google()

  • http://www.steveblack.co SteveBlack.Co

    Does Maven work for PHP projects?

  • omkar

    Dear sir,
    I am currently a learning Java Developer.
    I had been working in a workless small company from 2 years 6 months.
    for better opportunity i have decided to learn complete j2ee and using 3 years of experience i need to look for other company.
    I know only j2SE, servlets, jsp, “Struts & Hibernate(currently learning)”.
    I heard and realized that not only these there are so many development tools for development. Recently i heared about ant, “maven & log4j, SLF4j from you.”
    could you please give me the guidense to learn these…
    if there any other development tools which makes the developer as a complete J2EE developer?
    can you please tell the resources.
    For Maven you have provided the tutorial.

  • Luis

    Hi Mykong,

    Nice post again, thank you very much!

    One question, imaging this classic scenario, application that uses hibernate. Would it be possible that once you declared the hibernate dependency Maven download for you the rest of the “dependency tree” (i.e the commons.logging).

    Thanks in advance,

    Luis

    • http://www.mkyong.com mkyong

      Yes, Maven will grab all dependencies automatically.

    • Luis

      Hi Mykong,

      Thanks for you reply! Just one comment. Imaging that we continue in the same scenario, webapp with hibernate 3.6.10. After adding commons-logging and the JDBC driver (Oracle), at run time (tomcat:run), I get an hibernate exception:

      Initial SessionFactory creation failed.org.hibernate.HibernateException: Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]
      .../...
      Caused by: java.lang.NoClassDefFoundError: javassist/util/proxy/MethodFilter
      

      Once I added the javassist artifact everything goes ok… Running mvn dependency:analyze I see that javassist is not in the hibernate-core dependency hierarchy, so I guess that this kind of issues can not be automatically solved by maven.

      Thanks and best regards,

      Luis

  • Guru

    Thanks a million. Saved me lot of time.

  • Pingback: Why need to use Maven in Java project?()

  • Siddhanta Kumar Pattnaik

    Hi,and value are log4j it is any logical name or its name must same with jar file name…….

    • Gagandeep Singh

      No. log4j is not a logical name. instead the physical folders in the repositories do exists with this name. that’s why the folder path is

               repo path =  &quot;{groupId}/{artifactId}/{version}/{jar file name}&quot;
      • Siddhanta

        Thanks alot……

  • http://www.marzapower.com marzapower

    Hi, I recently released a log4j meta-library, called “Loggable”.
    It will ease development time and reduce the common log4j overhead while defining your application architecture.

    Please, check it out and let me know what you think about it:
    http://www.marzapower.com/loggable/

    Thank you!

  • Pingback: Maven Tutorials | Tutorials()