Maven Dependency Mechanism, How It Works

转自:http://www.mkyong.com/maven/how-to-use-maven-dependency-to-download-library-automatically/


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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值