(9) detailed explanation of Maven coordinates and maven coordinates

Source: Internet
Author: User

(9) detailed explanation of Maven coordinates and maven coordinates

A core role of Maven is to manage project dependencies and introduce various jar packages that we need. In order to automatically parse any Java component, Maven must uniquely identify these Jar packages or other resources. This is the basis for managing project dependencies, that is, the coordinates to be discussed. Projects developed by ourselves must also be uniquely identified by coordinates so that dependent references can be carried out in other projects.

 

1. What is Maven coordinates?

 

The most familiar thing about Coordinate is the coordinates in mathematics. I'm not particularly impressed. The major of mathematics is plane ry. In a plane coordinate system, there are x and y axes. the x axis is a horizontal line, and the y axis is a vertical line. The coordinates (x, y) indicate that the distance from the point to the x axis is y, and the distance from the y axis is x, any coordinate can uniquely identify a point in the plane.

 

In actual life, we can regard the address as a coordinate. Different provinces, cities, districts, streets, and other information mark different addresses. People who often take out food in Shenzhen should have some experience. The food delivery staff will give you the food delivery according to the address you fill in, the address you entered indicates a unique address.

 

Coordinates are like the ID card of every Java component. Maven has many Java components in the world. It may be jar, war, or something else. If Maven does not have the concept of coordinates, we cannot distinguish these components, so we need to uniquely identify each component. Otherwise, just like the traditional manual method, you need spring to download the spring package, and mysql to download the mysql package, without uniform specifications, how can we automate the dependency on these components.

 

Maven gives us a set of rules, that is, using coordinates for unique identification. The coordinate elements of Maven include groupId, artifactId, version, packaging, and classfier. As long as we provide the correct coordinate element, Maven can find the corresponding component. First, go to your local repository to find it. If not, go to the remote repository to download it. If remote repository is not configured, components are downloaded from the central repository address (http://repo1.maven.org/maven2) by default, which contains most of the world's popular open source project components, but not all of them have, in my previous development, I encountered the problem that the oracle database jar package could not be found.

 

When we develop our own projects, we also need to define coordinates for our projects. This is mandatory. Only in this way can other projects reference the components of this project.

 

2. Coordinate details

 

Maven coordinates are defined by the groupId, artifactId, version, packaging, and classfier elements. Let's take a look at the example:

 

 

In normal development, we only need to use a few necessary elements. unnecessary elements are often not needed. Let's take a look at the meaning of each element, as mentioned above.

 

GroupId: Defines the actual project to which the current Maven project belongs. First, the Maven project and the actual project are not necessarily one-to-one. For example, SpringFrameWork has many Maven projects, such as spring-core and spring-context. This is because of the concept of modules in Maven. Therefore, a real project is often divided into many modules. Second, groupId should not correspond to the organization or company of the project. The reason is simple. There are many actual projects in an organization. If the groupId is defined only at the organization level, we will see that artifactId can only define Maven projects (modules ), it is difficult to define the level of the actual project. Finally, the expression of groupId is similar to that of Java package name, which is usually one-to-one correspondence with the domain name. In the above example, the groupId is junit. Does it feel special? It is also possible because there are no many branches around the world.

 

ArtifactId: This element defines a Maven Project (module) in the current project. We recommend that you use the actual project name as the prefix of artifactId. For example, in junit in the above example, junit is the actual project name, which is convenient and intuitive. By default, maven generates components that use artifactId as the file header, such as the junit-3.8.1.jar, with the actual project name as the prefix, you can easily find the component of a project from the local repository.

 

Version: This element defines the version of the component to be used. In the above example, the junit version is 3.8.1. You can also change it to 4.0, which indicates that junit of Version 4.0 is used.

 

Packaging: Defines how Maven projects are packaged and what packages of components are used. First of all, the packaging method usually corresponds to the file extension of the generated component, as in the above example, there is no packaging, the default is the jar package, the final file name is the junit-3.8.1.jar. It can also be packaged into war.

 

Classifier: This element is used to help define attachments for building output. The ancillary component corresponds to the main component, as in the above example the main component is a junit-3.8.1.jar, the project may also be generated through some plug-ins such as junit-3.8.1-javadoc.jar, junit-3.8.1-sources.jar, so that the ancillary component has its own unique coordinates.

 

Among the above five elements, groupId, artifactId, and version must be defined. packaging is optional (jar by default), while classfier cannot be directly defined. It must be used in combination with plug-ins.

 

We can find that the name of a component is composed of the values of these elements, the junit-3.8.1.jar, to compare themselves, not only that, the layout of the Maven repository is also based on the Maven coordinate, you can check whether the local repository layout is like this.

 

After understanding the Maven coordinates, you can start to learn about Maven dependency management.

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.