Maven (iii) coordinates and dependencies

Source: Internet
Author: User

        Maven A major feature of Manage project Dependencies , in order to be able automatically resolves any one java Span lang= "ZH-CN" style= "Font-family:simsun; Font-size:18px "> widget, maven They must be uniquely identified, which is the underlying foundation of dependency management -- coordinates . Let's describe in detail the maven

The coordinates hereare not the same as the coordinates in the familiar plane geometry (x,y), but the meaning is consistent, which also shows that there is no new knowledge, only the mutual transformation between different knowledge. It can be said that any coordinate, whether in plane geometry or an address, is a point that uniquely identifies the plane.

relative to the point in the plane and the address in the city, Maventhe world has a very large number of constructs, that is, the usual use ofJar,Warand other documents. Now, without the introduction of the concept of coordinates, we need to download differentJarpackage. For example, when you need to introducespringframeworkdepends on the time we need to goSpring Frameworkwebsite search and download, when required to uselog4jwhen we are dependent, we go back.Apachesite Search, so you can imagine, we need to spend a lot of time in search, browse the Web and other work above.

without a unified standard, the principle of unification, the download relies on the function cannot realize the automation. To solve this problem,Mavendefines such a set of specifications: coordinates. The main elements of the coordinates are: groupId,artifactid,version,packaging, classifier . A complete coordinate can uniquely determine a dependent package, as long as we provide the correct coordinate element,Mavenwe can find the corresponding component from the warehouse for our use.


Let's look at a set of coordinate definitions:

     <groupId>com.google.code.gson</groupId>     <artifactId>gson</artifactId>     < Version>2.3.1</version>     <packaging>jar</packaging>

Gson is a Java class library provided by Google for mapping between Java objects and JSON data. You can turn a JSON string into a Java object, or vice versa.

with the above coordinate definition, I would like to introduce the various elements about coordinates: groupId: Defines the currentMaventhe actual project to which the project belongs. andArtifactidrefers to one of the actual projectsMavenProject (module). versionas the name implies, it is definedMaventhe version in which the project is currently located. Packagingis the definition ofMavenhow the project is packaged.

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; understand the definition of coordinates, and then take a look at dependencies. In pom The dependency configuration in the file is by the root element project dependencies configured.

       <dependencies><dependency><groupId>com.google.code.gson</groupId><artifactId> gson</artifactid><version>2.3.1</version><packaging>jar</packaging></ Dependency><dependency><groupid>org.codehaus.jackson</groupid><artifactid> Jackson-mapper-asl</artifactid><version>1.9.13</version></dependency><dependency ><groupid>com.tgb</groupid><artifactid>itoo-base</artifactid><version>${ Itoo-base.version}</version></dependency></dependencies>


&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; speaking of dependence, I have to say transitive dependency . What is transitive dependency? For example: If you do not use the maven We manually download the required spring Framework dependent, due to spring Framework Also rely on other open source class libraries, because in practice we often need to download these packages, so often introduce a lot of unnecessary dependencies.

andMaventhe transitive dependency mechanism is a good solution to this problem. It's coordinate elements<scope>defines the scope of the dependency (Compile,Test,provided,Runtime,system, etc.). The dependency range is not declared in the above dependency example, so its scope is the defaultCompile. With a transitive dependency mechanism, the use of theSpring Framework, you don't have to think about what it depends on, and don't worry about introducing unnecessary dependencies. Maven parses each of the directly dependent Pom, introducing those necessary indirect dependencies into the current project in the form of transitive dependencies.

This article focuses on the two core concepts of Maven: coordinates and dependencies. I don't know, do you understand?

Maven (iii) coordinates and dependencies

Related Article

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.