2. Maven-related concepts – Maven repositories and coordinates

1. The target

  • Understand the role of Maven repositories

2. The path

  1. Maven’s warehouse
  2. The coordinates of the Maven

3. The interpretation of the

3.1 Maven’s warehouse

The name of the warehouse role
Local repository Equivalent to caching, the project first downloads the JAR package from a remote repository (Internet) and stores the JAR package in a local repository (on the programmer’s computer). The second download does not need to be from a remote repository. Look at the local warehouse first. If you can’t find it, look at the remote warehouse.
The central warehouse The JARS in the repository are maintained by a professional team (Maven team). Address of central warehouse:repo1.maven.org/maven2/
Remote warehouse Set up a private service in the company, other companies set up a warehouse, public.
3.1.1. There is a slow downloading of JAR packages in common development

Generally speaking, whether we manually download jar packages before or later download JAR packages in Maven’s central repository, some download addresses may be in foreign countries, resulting in slow download, which affects the efficiency of our project construction.

So what’s the solution?

Answer: You can use caching.

3.1.2 Using a local repository as a cache for local JAR package downloads

The process of downloading the JAR package for the first time is still slow, because after the first download, the JAR package is stored in the local repository. Then start the second time, download and use the JAR again, and efficiency will increase.

However, the local repository does not solve the problem of slow downloading jar packages from Maven’s central repository for the first time. How to solve it?

3.1.3 Assume remote warehouse services in China (for example, Aliyun Warehouse), providing domestic developers to download JAR packages

In order to solve the problem of slow downloading jar packages from abroad for the first time, Ali Cloud will copy and download jar packages from the central warehouse. And provide remote warehouse service in China, provide domestic developers download, so as to solve the problem of low efficiency of the first download.

Of course, we can also build this remote warehouse in our own company.

3.2 Coordinates of Maven

One of Maven’s core roles is to manage project dependencies, importing the various JARS we need, and so on. In order to automate the parsing of any Java artifact, Maven must uniquely identify these JARS or other resources, which are the basis for managing project dependencies, known as coordinates. Projects, including our own, are also uniquely identified by coordinates so that they can be referenced by dependencies in other projects. The defining elements of coordinates are as follows:

  • GroupId: unique identifier of the project organization that actually corresponds to the structure of the JAVA package (usually written as the organization name of the company eg: com.Alibaba)
  • ArtifactId: Name of the project
  • Version: Defines the current version of the project
3.2.1 Access to view Maven’s central repository as follows:

Visit repo1.maven.org/maven2/

You can see that there are quite a few JAR packages included.

And you can find a lot of Alibaba projects from the path of com.alibaba. And this com.alibaba is the name of the company.

3.2.2 Druid library files can be found in Alibaba library

Under the com.alibaba path, search druid and we will find it.

3.2.3 You can locate the required JAR package by organization name, project name, and version number

3.2.4 Configuring the JAR package in the project Introduction, just need to configure the organization name + project name + version number of the JAR package in pom.xml.

For example, to import druid, simply configure the coordinates to import Druid in the pom.xml configuration file:

<dependecies>
	<! --druid connection pool -->
	<dependency>
  		<groupId>com.alibaba</groupId>
  		<artifactId>druid</artifactId>    
  		<version>1.0.9</version>  
	</dependency>
    <dependency>
    	<groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.6</version>
    </dependency>
</dependecies>
Copy the code

There is a big problem, however, that we generally do not remember the organization name + project name + version number of these JAR packages. Where can we get it?

3.2.5 Coordinate information of JAR packages can be searched on Baidu or maven coordinate search websites abroad
  1. Visit the Maven coordinate search site at mvnrepository.com/

  1. Click to enter the project name as follows:

  1. Select the version number you want

4. Copy available Maven coordinates

4. Summary

  1. Warehouse (local, central, remote (private))
    • Look at the local warehouse first
      • If there is, it is directly available for use
      • If not, find it from the central repository and download it automatically to the local repository
  2. Use the coordinates to find the corresponding JAR from the warehouse
<dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>druid</artifactId>    
  <version>1.0.9</version>  
</dependency>
Copy the code
  1. Maven coordinates search site address: mvnrepository.com/
  2. Maven coordinates will be copied directly in future work