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

Case study

Assume you want to use Log4J as your project logging mechanism (actually i more prefer SLF4J). Here is what you do…

1. In traditional way

  1. Visit http://logging.apache.org/log4j/
  2. Download the Log4j jar library
  3. Manually include it into your project dependency
  4. All manage by yourself, you need to do everything

If there is 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 ignore, it will upgrade the library automatically when there is a newer version.

  2. Include “Maven coordinates” into “pom.xml” file, under “<dependencies>” tag
    <dependencies>
        <dependency>
    	<groupId>log4j</groupId>
    	<artifactId>log4j</artifactId>
    	<version>1.2.14</version>
        </dependency>
    </dependencies>
  3. While Maven is compiling or building, the log4j will download automatically and put it into your Maven local repository.
  4. All manage by Maven… automatically.

See the different? So what just happened in Maven?

When you use Maven to build your project, “pom.xml” will be parsed, and the 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 define in pom.xml).

The Maven dependency library management is quite impressive and handy ~ nice tool.

How to find the Maven coordinates?

The only problem is how do you know what Maven coordinates you want to put? To get it, you always can refer to the Maven Central Repository for detail, or using this Google workaround – Use Google site search function.

Any Java questions or problems? please post at this JavaNullPointer.com forum, see you there ~
[ Read More ] You can find more similar articles at Maven Tutorials