Hello, today we share Maven with you. Take out your little notebook and write it down!

The concept of Maven

Why Maven?

1. A project is a project

If a project is very large, it is not suitable to use package to divide modules. It is better that one module corresponds to one project, which is conducive to division of labor and cooperation.

Maven allows you to split a project into multiple projects.

2. The REQUIRED JAR package must be manually assigned and pasted to the WEB-INF/lib directory

The problem is that the same JAR packages repeatedly appear in different projects, wasting storage space and making projects bloated.

With Maven, jar packages can only be stored in the “repository”, and projects that need to use the “reference” file interface, without actually copying the jar packages over.

3. The JAR package needs to be prepared for us or downloaded from the official website

Different technology websites offer JAR packages for download in a variety of forms.

Some technology websites are available for download through specialized efforts like Maven or SVN.

If the JAR package is downloaded in a noncanonical manner, the contents are probably noncanonical as well.

Jar packages can be downloaded in a canonical manner with Maven because all well-known framework or third-party jar packages are stored in Maven’s central repository according to uniform specifications, and the content is reliable.

4. Other JARS that a JAR package depends on need to be manually added to the project

For example, Spring’s core relies on commons-logging-1.1.3.jar.

If the dependencies between all the JAR packages need to be clearly understood by the programmer himself, this can greatly increase the cost of learning.

Maven automatically imports dependent JAR packages.

What is Maven?

Maven is an automated build tool for the Java platform.

Make — >Ant — >Maven — >Gradle

Build: The process of “producing” a working project using “raw materials” such as “Java source files”, “framework configuration files”, “JSP”, “HTML/CSS”, “images” and other resources.

  • Compile: Java source files (user.java) — > compile — >Class bytecode files (user.class) — > hand over to the JVM for execution
  • Build: The process of “producing” a running project using written Java code, framework configuration files, resource files such as internationalization, JSPS, and static resources as “raw materials.”
  • Deployment: A BS project ultimately runs not the dynamic Web project itself, but the “compilation result” of the dynamic Web project. Deployment is the process of taking the project to a specified directory on the server.

The JRE System Library runtime environment shown below is actually a reference to a set of JAR packages. The JAR packages themselves are not assigned to the project, so it is not a directory.

[img- a6b5jrGd-1622857103106] [img- a6b5jrgd-1622857103106] (E:\ development note \maven\image\ runtime environment.jpg)

The following is the result of compiling the Dynamic Web Project:

[img- w3RDKKFL-1622857103109] [img- w3RDKKFL-1622857103109] [img- w3rdKKFL-1622857103109]

During development, all paths or class paths configured in configuration files are based on the resulting directory structure as standard.

Each part of the construction process

  1. Cleanup: Deletes old class files from previous compilations in preparation for the next compilation
  2. Compile: Compile Java source files into class files
  3. Test: Automatic test, automatic call junit program
  4. Report: Results of test program execution
  5. Packaging: Dynamic Web projects are war packages, Java projects are JAR packages
  6. Installation: Maven-specific concepts ———— copies the packaged files to the specified location in the repository
  7. Deployment: Copy the war package generated by the dynamic Web project to the specified directory of the Servlet container and make it ready to run

Install the Maven core program

  1. Configure the JAVA_HOME environment variable
  2. Extract the Maven core zip

3. Configure the MAVEN_HOME and PAHT environment variables

The core concepts of MAVEN

  1. The convention directory structure
  2. POM
  3. coordinates
  4. Rely on
  5. warehouse
  6. Life cycle/plug-in/target
  7. inheritance
  8. The aggregation

MAVEN Project Directory

Create a convention directory structure

Root directory: project name

SRC directory: source code

Pom. XML file: the core configuration file of the Maven project

Main directory: stores the main program

Test directory: stores test programs

Java directory: Stores Java source files

Resource directory: Stores configuration files for frameworks and other tools

Why follow the agreed directory structure? Maven is responsible for automating the build for our project. For Maven to automate the build process, for example, it must know where the Java source files are stored. If you want your own custom things to be known to the framework or tools, there are two ways to explicitly tell the framework, in the form of configuration, to adhere to conventions that exist within the framework

Common Maven commands

Note: To execute Maven commands related to the build process, you must go to the directory where pom.xml resides

Common Commands

  1. MVN clean: indicates cleaning
  2. MVN compile: Compiles the main program
  3. MVN test-compile: compiles test programs
  4. MVN test: Executes a test
  5. MVN package: package

About networking

  1. Maven’s core program only defines the abstract lifecycle, but the specific work must be done by specific plug-ins, which themselves are not included in Mavaen’s core program.
  2. When a plugin is needed for a Maven command, the Maven core program first looks for it in the local repository
  3. Location of local repository: [system user directory.m2\repository]
  4. If the Maven core program does not find the required plug-in in the local repository, it will automatically connect to the Internet and download it from the central repository.
  5. If the extranet cannot be connected, the build fails.
  6. Changing the location of the default local repository allows the Maven core program to look for plug-ins in our pre-prepared directory

Go to the Maven decompression directory \conf\settings.xml

Add the following tags to the settings.xml file

POM

Meaning: Project Object Model

Pom.xml is the core configuration file for the Maven project, where everything related to the build process is configured.

coordinates

2. Coordinates in mathematics:

The X and Y vectors on the plane can be unique to any point on the plane

Using X, Y, and Z vectors in space can uniquely locate any point in space

Maven: Uniquely locate a Maven project in the repository using the following three vectors

Groupid: company or organization domain name in reverse order + project name

com.atguigu.maven

Artifactid: indicates the module name

Hello

Version: version

1.1.0

Corresponding relations between

io.projectreactor

reactor-core

3.1.8, RELEASE

IO/projectreactor/reactor core / 3.1.8, RELEASE/reactor – core – 3.1.8. The jar

warehouse

Classification of warehouse:

  1. Local repository: The deployed repository directory on the current computer serves all Maven projects on the current computer
  2. Remote warehouse

Local area network (private server) : Built in the local area network environment, for all the LOCAL area network Maven project services

Central repository: Located on the Internet, it serves all Maven projects worldwide

Central warehouse mirroring: To share warehouse traffic and improve user access speed

What’s saved in the repository: Maven project

Plugins required by Maven itself

Jar packages for third-party frameworks or tools

Maven project developed by myself

Rely on

Maven parses dependency information by looking for dependent JARS in the local repository

For our own Maven project, use the Install command to Install and enter the repository.

Scope of dependencies:

Compile scope dependency

  1. Valid for main program: Yes
  2. Valid for the test program: Yes
  3. Package or not: Package or not

Test range dependencies are valid for the main program: Invalid Are valid for the test program: Valid participate in packaging: Do not participate

Provided scope dependency

  1. Valid for main program: Yes
  2. Valid for the test program: Yes
  3. Package or not: No

The life cycle

Order of execution of each build: Do not mess with the order, it must be executed in the correct order established.

Maven’s core program defines an abstract life cycle in which specific tasks at each stage are performed by plug-ins.

The Maven core program, for better automated builds, executes each phase of the declaration cycle from where the declaration cycle started, regardless of which phase of the life cycle is now being executed.

Package or not: No

The life cycle

Order of execution of each build: Do not mess with the order, it must be executed in the correct order established.

Maven’s core program defines an abstract life cycle in which specific tasks at each stage are performed by plug-ins.

The Maven core program, for better automated builds, executes each phase of the declaration cycle from where the declaration cycle started, regardless of which phase of the life cycle is now being executed.

Rely on

Transitivity of dependencies:

  • Benefits: Transitable dependencies do not have to be declared repeatedly in every module project; the “bottom” project depends once.

  • Note: Dependencies that are not in the compile scope cannot be passed, so this dependency must be declared repeatedly in each module if necessary

Dependency exclusion

  • You need to set exclusions
  • How dependency exclusion is set

Principle of dependence

Purpose: To solve the jar package conflict between module projects

Scenario 1: Shortest path first

Scenario 2: Same path: Declared first (dependency tag declaration order)

Centrally manage dependent versions

The dependency on each Spring JAR package is 4.0, how to upgrade to 4.1.1?

Use the properties TAB to automatically specify the version number with the label

Where a unified version is required, use ${custom tag name} to reference the version number

inheritance

The status quo

  1. Hello relies on junit: 4.0
  2. HelloFirend relies on junit: 4.0
  3. MakerFriends relies on junit: 4.9

Since the dependencies of the test scope cannot be passed, they will inevitably be scattered in various module projects, easily resulting in version inconsistency

demand

The version of the unified management module project that depends on junit

solution

Extract the junit dependency version to the parent project. When declaring the junit dependency in the child project, you do not specify the junit dependency version.

steps

Create a Maven project as a parent project, note that the package is poM

Declare a reference to a parent project in a child project

Delete duplicates in the coordinates of the child project and the parent project (yellow line)

Unified management of junit dependencies in the parent project

Remove the version number section that junit depends on in the child project

The aggregation

role

Build and install each module project

Configuration mode

Configure each participating aggregation module in a “total aggregation project.

use

Right click on the aggregator project pom.xml — > Run as — > Maven install. That’s it