• 周四. 12月 1st, 2022

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

[Maven Series 1] very simple Maven warehouse

[db:作者]

1月 6, 2022

Introduce Maven, Then explain in detail Maven Warehouse related knowledge .

Preface

I started to think Maven There is very little to master , It turns out that there’s something .Maven There are many practical exercises , I’ll go through this article first , Yes Maven Give a brief introduction , Then focus on the explanation Maven Warehouse related knowledge ..

Feeling Maven Relatively simple , There are also a lot of related information on the Internet , Why do I need to write this series ? In fact, the main thing is to record your own learning track , Easy to follow-up query learning content , That’s it !

Maven Introduce

What is? Maven?

Maven It is a project management tool , It contains a project object model (Project Object Model), Reflected in the configuration , It’s just one. pom.xml file . It’s a set of standards , The life cycle of a project 、 A dependency management system , It also includes plug-ins that are defined in the project lifecycle phase (plugin) And the goal (goal).

When we use Maven Use , Through a custom project object model ,pom.xml To describe our own project in detail .

Maven There are two cores in it :

  • Dependency management : Yes jar Unified management of (Maven Provides a Maven The central warehouse ,mvnrepository.com/, When we finish adding in the project … Will automatically go to the central warehouse to download the relevant dependencies , And solve the problem of dependency )
  • The project build : Compile the project 、 test 、 pack 、 Deploy 、 Upload to private server, etc

Why use Maven?

because Java The ecology is very rich , No matter what function you want to achieve , Can find the corresponding tool class , These tool classes are all based on jar In the form of a package , for example Spring,SpringMVC、MyBatis、 Database driven , wait , It’s all about jar In the form of a package ,jar There will be a connection between the packages , Before using a dependency , You also need to identify other dependencies on which this dependency depends , therefore , When the project is large , Dependency management can become very cumbersome , This is a Maven The first problem solved .

Maven It can also handle multi module projects . Simple projects , Single module subcontract processing is enough , If the project is complex , To make a multi module project , For example, an e-commerce project has an order module 、 Membership module 、 Commodity module 、 Payment module …, Generally speaking , Multi module project , Each module cannot run independently , To have multiple modules together , The project can run , This is the time , With the help of Maven Tools , One click packaging of the project can be realized .

Maven Can help developers complete the following work :

  • structure
  • Document generation
  • The report
  • rely on
  • Release

Maven Warehouse

The origin of the warehouse

stay Maven In the world , Any one depends on 、 The output of a plug-in or project build , Can be called a component . Thanks to the coordinate mechanism , whatever Maven The way a project uses any component is exactly the same . On this basis ,Maven You can store everything in one place Maven Project shared components , This unified location is the warehouse .

Actually Maven Projects will no longer store their dependent files , They just need to declare the coordinates of these dependencies , When needed ( for example , When compiling a project, you need to add dependencies to classpath in ),Maven It will automatically find the components in the warehouse according to the coordinates , And use them .

To achieve reuse , After the project is built, the components that can be generated can also be installed or deployed to the warehouse , For other projects .

The layout of the warehouse

Every component has its own unique coordinates , According to this coordinate, you can define its unique storage path in the warehouse , This is Maven The layout of warehouse based on GIS . The corresponding relationship between the path and coordinates is groupId/artifactId/version/artifactId-version.packaging. for instance , For example, the following paging plug-in relies on the following :

<dependency>
      <groupId>com.github.pagehelper</groupId>
      <artifactId>pagehelper-spring-boot-starter</artifactId>
      <version>1.1.0</version>
</dependency>

That’s the path to the warehouse :


Maven The warehouse is based on a simple file system , We also understand how it’s stored 、 therefore , When you encounter some warehouse related problems , Can be very convenient to find related documents , Easy to locate problems .

Warehouse classification


Warehouse call order

Warehouse call order :

  • First look for dependencies in the local repository , If it does not exist , Then go to the next step , otherwise , sign out ;
  • And then in Remote warehouse ( Private clothes ) Find dependencies in , If it does not exist , Then go to the next step , otherwise , sign out ;
  • Last in Find dependencies in the central repository , If it does not exist , It will prompt the error message , sign out .

Local repository

Generally speaking , stay Maven Under project directory , There is no such thing as lib/ This is used to store the directory of dependent files . When Maven During compilation or testing , If you need to use dependency files , It always uses the dependency files of the local repository based on coordinates .

By default , No matter in Window still Linux Next , Each user has a path named .m2/repository/ The warehouse directory of . If you want to customize the local warehouse directory address . You can edit the file ~/.m2/settings.xml, Set up localRepository The value of the element is the desired warehouse address , for example :

<settings>
<localRepository>D:\java\repository\</localRepository>
</settings>

It is not recommended to modify the local warehouse path , By default .

The central warehouse ( long-range )

Because the original local warehouse is empty ,Maven You must know at least one available remote warehouse , Can be executed Maven Download to the required component at command time . The central warehouse is such a default remote warehouse ,Maven The installation file of comes with the configuration of the central warehouse .

Central repository contains most of the most popular open source resources in the world Java artifacts , And source code 、 The author information 、SCM, Information 、 License information, etc , Every month, the world is accepted here Java The programmer probably 1 Billion visits , It’s good for the whole world Java The contribution of developers can be seen from this .

Private service warehouse ( long-range )

Private server is a special remote warehouse , It is a warehouse service built in LAN , Remote warehouse of private agent Wan , For Maven The user to use . When Maven When you need to download components , It requests from private service , If the component does not exist on the private server , Download from an external remote warehouse , After caching on private server , Again Maven Download request service for . therefore , Some components that can’t be downloaded from the external warehouse can also be uploaded from the local to the private server for everyone to use .

The benefits of private service :

  • Save your own internet speed
  • Speed up Maven structure
  • Deploy third party builds
  • Improve stability , Enhanced control
  • Reduce the load of central warehouse

Remote warehouse configuration related content

Remote warehouse configuration

In normal development , We often don’t use the default central warehouse , The access speed of the default central warehouse is relatively slow , There may be a lot of people visiting , Sometimes it can’t meet the needs of our project , Maybe some components needed by the project are not in the central warehouse , And in other remote repositories there are , Such as JBoss Maven Warehouse . At this time , Can be in pom.xml Configure the warehouse in , The code is as follows :

<repositories>
  <repository>
    <snapshots>
      <enabled>false</enabled>
    </snapshots>
    <id>central</id>
    <name>maven-release-virtual</name>
    <url>https://xiaomi_xxx/artifactory/maven-release-virtual</url>
  </repository>
  <repository>
    <snapshots>
      <enabled>false</enabled>
    </snapshots>
    <id>miremote</id>
    <name>maven-remote-virtual</name>
    <url>https://xiaomi_xxx/artifactory/maven-remote-virtual</url>
  </repository>
  <repository>
    <snapshots />
    <id>snapshots</id>
    <name>maven-snapshot-virtual</name>
    <url>https://xiaomi_xxx/artifactory/maven-snapshot-virtual</url>
  </repository>
</repositories>
  • **repository:** stay repositories Under the element , have access to repository The child element declares one or more remote repositories .
  • **id:** The only thing the warehouse claims is id, In particular, we need to pay attention to ,Maven It’s used in its own central warehouse id by central, If other warehouse claims also use this id, It will override the configuration of the central warehouse .
  • **snapshots: Used to control Maven For download permission of snapshot component .

Remote warehouse Authentication

Most remote warehouses don’t require authentication , But if it’s for internal use , For safety’s sake , Still need to configure authentication information .

Configuring authentication information is different from configuring a remote warehouse , The remote warehouse can be directly in pom.xml Middle configuration , But the authentication information must be configured in settings.xml In file . This is because pom It is often submitted to the code repository for all members to access , and settings.xml Generally, it only exists in this machine . therefore , stay settings.xml It is more secure to configure authentication information in .

<servers>
  <server>
    <username>louzai</username>
    <password>xxx</password>
    <id>central</id>
  </server>
  <server>
    <username>louzai</username>
    <password>xxx</password>
    <id>snapshots</id>
  </server>
</servers>

In addition to configuring the account and password , The key is id 了 , This id To be with you pom.xml The remote warehouse configured inside repository Of id Agreement , It’s this id Connect authentication information with warehouse configuration .

Mirror image

If the warehouse X Can provide warehouse Y Everything stored , Then we can think of X yes Y A mirror image of . Used to Maven Of all know , Foreign central warehouses are too slow to use , So you can choose your own image .

<mirrors>
  <mirror>
      <id>others</id>
      <mirrorOf>*,!central,!miremote,!snapshots</mirrorOf>
      <url>https://pkgs.d.xiaomi.net/artifactory/maven-remote-virtual</url>
  </mirror>
</mirrors>

The meaning here is , If it wasn’t for this 3 A warehouse central、miremote and snapshots, Will go to this image to download and build , introduce mirrorOf Configuration options :

<mirrorOf>*<mirrorOf>: Match all remote warehouses .
<mirrorOf>external:*<mirrorOf>: Match all remote warehouses , Use localhost Except for , Use file:// Except for the agreement . in other words , Match all remote warehouses that are not on this machine .
<mirrorOf>repo1,repo2<mirrorOf>: Match warehouse repo1h and repo2, Use commas to separate multiple remote warehouses .
<mirrorOf>*,!repo1<mirrorOf>: Match all remote warehouses ,repo1 With the exception of , Use exclamation marks to exclude the warehouse from matching .

It should be noted that , Because the mirror warehouse completely blocks the mirror warehouse , When the image warehouse is unstable or the service is stopped ,Maven The mirrored warehouse will still not be accessible , So you won’t be able to download the component .

“ Remote warehouse configuration related content ”, It’s divided into two parts 3 Block content , In fact, I will be Xiaomi’s Maven Default profile content Copy come out , And then I’ll explain it to you separately , Before writing this article , In fact, I don’t understand , Now I understand .

Welcome to like it more , More articles , Please follow the WeChat public account “ Louzi’s way to advancement ”, Focus , Neverlost ~~


发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注