Maven best practices: Managing Dependencies

Source: Internet
Author: User

 

[It168 technical documentation]

Some people think that Maven is a dependency management tool. Of course, this idea is wrong (specifically, Maven is a project management tool that runs through the entire project lifecycle, compilation, testing, packaging, publish ...), however, there is also a reason why Maven seems to have caused such errors, because Maven's dependency management is very powerful and Maven is ready for use, so you no longer need to face a lot of jar files and have dependency conflicts, useless dependencies and other problems can also be effectively prevented and solved. This section describes how to use Maven dependency management.

  Simplest dependency

Dependencies are located using Maven coordinates, while Maven coordinates are mainly determined by Gav (groupid, artifactid,
Version. Therefore, you need to know the maven coordinates of any dependency. The article "Search Maven repository" can help you find Maven coordinates.

The simplest dependency is as follows:

XML Code

<Dependency>
<Groupid> JUnit </groupid>
<Artifactid> JUnit </artifactid>
<Version> 4.4 </version>
</Dependency>

In the above example, we declare a dependency on JUnit. Its groupid is JUnit, and its artifactid is JUnit,
The version is 4.4. This group of Gav forms a Maven coordinate, based on which Maven can find the corresponding junit-4.4.jar file locally or in a remote repository.

  Dependency Classification

As the project grows, your dependencies become more and more. For example, you are dependent on a bunch of spring jar files, including org. Spring. Framework: Spring-core,
Org. Spring. Framework: Beans, org. Spring. Framework: Spring-web,
Org. Spring. Framework: Spring-mock. Their groupid is the same and their artifactid is different. To manage their versions, you have upgraded them to the latest version one by one. However, when the Pom is very large, you may make mistakes. When the versions are inconsistent, some strange compatibility problems may occur.

Maven has its solution:

XML Code

<Dependencies>
<Dependency>
<Groupid> org. Spring. Framework </groupid>
<Artifactid> spring-core </artifactid>
<Version >$ {spring. Version} </version>
</Dependency>
<Dependency>
<Groupid> org. Spring. Framework </groupid>
<Artifactid> spring-beans </artifactid>
<Version >$ {spring. Version} </version>
</Dependency>
<Dependency>
<Groupid> org. Spring. Framework </groupid>
<Artifactid> spring-Web </artifactid>
<Version >$ {spring. Version} </version>
</Dependency>
<Dependency>
<Groupid> org. Spring. Framework </groupid>
<Artifactid> spring-Mock </artifactid>
<Version >$ {spring. Version} </version>
</Dependency>
</Dependencies>

<Properties>
<Spring. version> 2.5 </spring. version>
</Properties>

Here we define a Maven attribute named spring. version with a value of 2.5. In this pom, we can use $ {spring. Version} to reference this attribute. We can see that all spring-dependent version elements are now $ {spring. Version}. When Maven runs, it will automatically replace this reference with a value of 2.5.

When we need to upgrade spring, we only need to change it easily, and now you can ensure that all spring dependency packages are of the same version.

 

This article from: http://tech.it168.com/a2009/0224/266/000000266570.shtml

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.