Maven
Maven is a project management tool that includes aProject Object Model (POM: Project Object Model), which is expressed in an XML file (
), which contains the project's basic learning , dependencies , plug-in configuration , build paths , etc.
Why Use Maven
-
Importing third-party jar packages is easier.Previously, when we used third-party frameworks, we would copy and paste the jar package into the WEB-INF/lib package. We had to repeat the usual tedious steps every time we created a new project, and when we used maven, we were able to do that.It will be each jar package in the maven local repository to save a copy of the need for jar package project as long as the coordinates of the way simply refer to it can be. Save space and make your project lighter!
-
With dependencies on jar packages.Often jar packages are not self-contained, and many of them depend on other packages in order to work. Whenever we introduce a new jar package, we don't know if it depends on other packages.If you use maven, you can automatically inject the current jar package depends on all the other jar package , without manual participation , saving a lot of human energy .
-
Get the canonical jar package.In the development process we will use a lot of jar packages, each jar package to get a different method, in the early online jar packages, perhaps not standardized, can not be used.And maven can provide us with a completely unified specification of the jar package management system
-
Modularization of projects.With the expansion of the project can not be simply through the package structure to divide the module , because it will cause multiple import jar package , very inconvenient , this time it must be split into multiple projects for collaborative development of the project , so that each jar package only to the maven repository to import it
Principles of Maven
Maven repository
maven repository is an important part of the principle of Maven, Maven in any one of the dependencies, plug-ins, or the output of the project build can be called building blocks
-
Two types of warehouses
maven repository is divided into a local repository in the remote repository, looking for components, the first will be in the local repository to look for, if you do not find it then jump to the remote repository to look for; in the original repository to find the local repository to download it for the next time you use the
-
Local Warehouse.After downloading maven, if you do not configure the address of the local repository, maven will be configured to the C disk by default. So we need to manually configure: open the maven file → find the conf folder → open the setting, xml file
Tag one of the 55 lines in the file
<localRepository> path address </localRepository>
This way the local repository is configured
-
Remote Warehouse.
- Remote repositories are divided into: centralized repositories, private repositories, and mirror repositories.
- Central Warehouse.is a public repository maintained by the Maven community , contains a large number of commonly used Java libraries and components . Maven will be downloaded from here by default dependencies , but after all, is an extranet , the domestic download slower , do not recommend , usually the central warehouse address to the note off
- Mirror repository.Mirror repository is to accelerate the reliance on the download and set up the warehouse, usually located in the agreement of the local area network, can be used as a central repository of the mirror, the download speed is faster, the recommended method.Usually use Alibaba for warehouse
- Find in the file
<mirrors>
The tags will be among the original<mirror>
Tags commented out will be replaced with Alibaba's mirror site to experience high-speed downloads
<!-- Alibaba MirrorMavenweb address --> <mirror> <id>nexus-aliyun</id> <mirrorOf>central</mirrorOf> <name>Nexus aliyun</name> <url>/nexus/content/groups/public</url> </mirror>
- Remote repositories are divided into: centralized repositories, private repositories, and mirror repositories.
Configuring Maven in IDEA
-
In IDEA Work go to the top menu and find File, scroll down and select Setting.
-
In the pop-up dialog box, expand "Build,Execution,Deployment", and select Maven under Build Tools, as shown below.
-
In the Maven home path, specify the local Maven location
-
Check Override after User Settings file and specify the local repository's file
-
Check Override after Local repository and specify the address of the local repository
- Open a new project configuration and repeat the above
Creating a Maven Project
Maven's Multi-Level Module Project
In the actual development process, we are in contact with the project is relatively large , so through a reasonable module split , you can realize the reuse of the code to facilitate the maintenance of the use of
Creating a maven_parent parent module
The role of the parent module is to coordinate the development of each sub-module
- Creating a Maven project directly in IDEA
- Then you don't have to choose anything.
- Remove the src directory from it
Create maven_common
common module is used to encapsulate the common code and resources that need to be used by multiple modules throughout the project. common module can contain a variety of tools, constants, configuration files, entity classes and so on.
- Right-click on the created parent maven and select New → select module
- Select maven when creating a module → select quickstart
- The rest of the project files are similar except we need to choose the webapp template
Dependency management
maven passes the
Dependencies that will be used in the file management project
- When using maven's multilevel modules each module has its own
file, called a submodule
- Files in a submodule usually inherit the configuration of the parent module, but you can also add module-specific dependencies.
Dependency Injection
- Basic structure of dependencies
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
</dependency>
- coordinate (geometry)
-
groupld, artifactld, version
: the basic coordinates of the dependency, for any dependency, the basic coordinates are the most important, Maven is based on the coordinates in order to find the required dependencies - Using these three coordinates a dependency can be identified
-
- For the coordinates of the maven dependencies you can go to the AliMirror site and look up /mvn/search