Big data learning summary (2021 version) ---Maven

1: Why use Maven? What problems can it help us solve?

  • ①Add a third-party jar package

In today's JavaEE development field, there are a large number of third-party frameworks and tools available for us to use. The easiest way to use these jar packages is to copy and paste them into the WEB-INF/lib directory. But this will cause the jar package to be repeatedly copied to the lib directory every time a new project is created, resulting in a large number of duplicate files in the workspace, making our project bloated.

After using Maven, each jar package itself is only saved in the local warehouse. Projects that require the jar package only need to be simply quoted in the form of coordinates. It not only greatly saves storage space, makes the project lighter, but also avoids the confusion caused by too many duplicate files

  • ② Dependency between jar packages

After introducing Maven, Maven can automatically import all other jar packages that the current jar package depends on for us, without manual involvement.

  • ③Obtain the third-party jar package

There are many types of jar packages that need to be used in JavaEE development, and almost every jar package can be obtained in different ways on its own official website. Searching the Internet for a jar package is exhausted physically and mentally. People who have never experienced it may not experience this torture. Not only that, sometimes the jar package that you painstakingly find does not have the class you need, or the class with the same name does not have the method you want-jar packages obtained in an irregular way are often irregular.

Using Maven, we can enjoy a fully unified and standardized jar package management system. You only need to rely on a jar package in a coordinated manner in your project, and Maven will automatically download it from the central warehouse and download other jar packages that this jar package depends on at the same time

——Standard, complete and accurate! Solve all problems at once!

  • ④ Split the project into multiple engineering modules

Many years have been normal for JavaEE projects. So let's imagine: the project developed by hundreds of people is the same Web project. So how do architects and project managers divide the modules of the project and how to divide the work? It is no longer possible for such a large project to divide the modules through the package structure, and the project must be split into multiple projects for collaborative development. Some of the multiple module projects are Java projects and some are Web projects. So how do you call and visit each other after the project is split? This requires the use of Maven's dependency management mechanism.

Everyone, please take a look at the split of our Survey project: the
Insert picture description here
upper-level module depends on the lower-level, all APIs defined in the lower-level modules can be called and accessed by the upper-level

2: How Maven builds projects on the Java platform

2.1: What is project construction

Construction is the process of using the Java code, framework configuration files, internationalization and other resource files, JSP pages and pictures and other static resources that we write as "raw materials" to "produce" a runnable project.

2.2: Several main links of the build process and Maven's automated build

①Clean up: delete previous compilation results and prepare for recompilation.
②Compile: Compile the Java source program into a bytecode file.
③Test: Test the key points in the project to ensure the correctness of the key points in the iterative development process of the project.
④Report: Record and display the test results in a standard format after each test.
⑤Package: Package a project containing many files into a compressed file for installation or deployment. The Java project corresponds to the jar package, and the Web project corresponds to the war package.
⑥ Installation: In the Maven environment, it specifically refers to the installation of the packaged result—jar package or war package into the local warehouse.
⑦Deployment: deploy the packaged result to a remote warehouse or deploy the war package to the server to run.

small issue?
Compile, package, deploy, test..." If programmers perform these programmatic tasks themselves, it will waste a lot of time. Can these programmatic tasks be done automatically by the machine?-Of course! It is automated construction.

At this point, the meaning of Maven is reflected. It can automatically execute from the beginning to the end of the build process:
Insert picture description here

3: Maven nine core concepts

Conventional directory structure

Find the Java source file, the next step can be compiled, and after compilation, there must be an accurate location to keep the compiled bytecode file.

If we need to let third-party tools or frameworks know where our own resources are in development, there are basically two ways:

  • ① Tell it clearly in the form of configuration
  • ②Agreement based on third-party tools or frameworks

Maven's requirements for the project directory structure belong to the latter one.
Insert picture description here
Maven can automate the construction of our Java project just because it specifies the directory where the specific file is saved.

POM

Project Object Model: Project Object Model. Encapsulate the relevant information of the Java project as an object as a model for easy operation and management. The core configuration of the Maven project. It can be said that learning Maven is learning the configuration in the pom.xml file.

coordinate

  • The coordinates in geometry
    [1] use the x and y vectors in a plane to uniquely determine a point in the plane.
    [2] Using x, y, z three vectors in space can uniquely determine a point in space.
  • The coordinates of Maven
    use the following three vectors to uniquely determine a Maven project in the Maven warehouse.
    [1]groupid: the reverse order of the domain name of the company or organization + the name of the current project
    [2]artifactId: the module name of the current project
    [3]version: the version of the current module

For example:

<groupId>com.atguigu.maven</groupId>
<artifactId>Hello</artifactId>
<version>0.0.1-SNAPSHOT</version>

How to find the jar package in the warehouse by coordinates?

  • Connect the three vectors of gav
com.atguigu.maven+Hello+0.0.1-SNAPSHOT
  • Use the concatenated string as the directory structure to search in the warehouse
com/atguigu/maven/Hello/0.0.1-SNAPSHOT/Hello-0.0.1-SNAPSHOT.jar

※Note: Our own Maven project must be installed before entering the warehouse. The command to install is: mvn install

rely

The most critical part of Maven, the main thing we use Maven is to use its dependency management function. To understand and master Maven
's dependency management, we only need to solve a few problems:

  • What is the purpose of dependence?

When the A jar package uses some classes in the B jar package, A has a dependency on B, which is a conceptual description. So how to introduce a jar package we need in the project in a dependent way?

The answer is very simple, just use ==dependency The label specifies the coordinates of the dependent jar package.

Insert picture description here

  • Scope of dependence: scope

Everyone noticed that in addition to the coordinates of the target jar package, there is a scope setting in the above dependency information, which is the scope of dependency. There are several optional values ​​for the dependent range,

What we get is: compile, test, and provided three.

Insert picture description here

  • Transitivity of dependency

A depends on B, and B depends on C. Can A use C? It depends on whether the scope of B depends on C is compile, if it is, it can be used, otherwise it is not.
Insert picture description here

  • Exclusion of dependence

If we introduce a dependency of A in the current project, and A depends on B, then Maven will automatically introduce A dependent B into the current project, but in some cases, B may be an unstable version, or the current project Have adverse effects. At this time, we can exclude B when introducing A.

[1] Scenario example
Insert picture description here
[2] Configuration method
Insert picture description here
[3] Effect after elimination
Insert picture description here

  • Dependency principle: resolve jar package conflicts
    [1] The shortest path takes precedence
    Insert picture description here
    [2] When the paths are the same, the declared first takes precedence
    Insert picture description here
    . The order of "declaration" here refers to the order of dependency label configuration

warehouse

classification

  • [1] Local warehouse: Serve all Maven projects on the current local computer.
  • [2] Remote warehouse
    Insert picture description here

(1) Private server: set up in the current local area network environment, serving all Maven projects within the current local area network
(2) Central warehouse: set up on the Internet, serving all Maven projects in the world.
(3) Mirroring of the central warehouse: set up on all continents to share traffic for the central warehouse. Reduce the pressure on the central warehouse, while responding to user requests faster.

Files in the warehouse

  • [1] Maven plug-in
  • [2] Module of the project developed by ourselves
  • [3] Jar packages of third-party frameworks or tools

※No matter what kind of jar package, the directory structure is generated according to the coordinates in the warehouse, so you can query or rely on it in a unified way

life cycle

What is the life cycle of Maven?

  • The Maven life cycle defines the execution order of each build link. With this list, Maven can automatically execute build commands.

  • Maven has three independent life cycles, namely:

    • ①Clean Lifecycle performs some cleaning work before proceeding to the actual construction.
    • ②The core part of Default Lifecycle construction, compiling, testing, packaging, installation, deployment, etc.
    • ③Site Lifecycle generates project reports, sites, and release sites.

They are independent of each other, you can just call clean to clean up the working directory, and only call site to generate the site. Of course, you can also run mvn clean install site directly to run all three life cycles .

Each life cycle consists of a set of phases, and the commands we usually enter on the command line will always correspond to a specific phase. For
example, run mvn clean, which is a stage of the Clean life cycle. There is a clean life cycle, and there is also a clean phase.

Clean life cycle The
Clean life cycle consists of three stages:

  • ①pre-clean performs some work that needs to be completed before clean
  • ②clean remove all files generated in the last build
  • ③post-clean performs some work that needs to be completed immediately after clean

Site life cycle

  • ①The pre-site performs some work that needs to be completed before the site documentation is generated
  • ②site site document of the project generated
  • ③post-site performs some work that needs to be completed after the site documentation is generated, and prepares for deployment
  • ④site-deploy deploys the generated site documents to a specific server

The site phase and site-deploy phase are often used here to generate and publish Maven sites. This is a very powerful feature of Maven. Manager (leader) prefers it. Documents and statistics are automatically generated, which is very beautiful.

Default life cycle The
Default life cycle is the most important one in the Maven life cycle, and most of the work occurs in this life cycle. Here,
only some of the more important and commonly used stages are explained:

validate
generate-sources
process-sources
generate-resources
process-resources :复制并处理资源文件,至目标目录,准备打包。
compile 编译项目的源代码。
process-classes
generate-test-sources
process-test-sources
generate-test-resources
process-test-resources :复制并处理资源文件,至目标测试目录。
test-compile :编译测试源代码。
process-test-classes
test :使用合适的单元测试框架运行测试。这些测试代码不会被打包或部署。
prepare-package
package :接受编译好的代码,打包成可发布的格式,如 JAR。
pre-integration-test
integration-test
post-integration-test
verify
install :将包安装至本地仓库,以让其它项目依赖。
deploy :将最终的包复制到远程的仓库,以让其它开发人员与项目共享或部署到服务器上运行。

Plugins and goals

  • The core of Maven only defines an abstract life cycle, and specific tasks are completed by plugins.
  • Each plug-in can implement multiple functions, and each function is a plug-in target .
  • Maven's life cycle and plug-in goals are bound to each other to complete a specific build task.

For example: compile is a goal of plug-in maven-compiler-plugin; pre-clean is a goal of plug-in maven-clean-plugin

inherit

Why do we need an inheritance mechanism?
Since non-compile dependency information cannot be passed in the "dependency chain", projects that need it can only be configured separately

polymerization

Why use aggregation?
After splitting multiple projects into modules, you need to manually install them to the warehouse one by one before the dependencies can take effect. After modifying the source code, you also need to manually
perform the clean operation one by one . After using aggregation, you can install and clean up Maven projects in batches.

How to configure aggregation?
Use the modules/module tag combination in the total aggregation project and specify the relative path of the module project.

4: Install Maven core program

  • Download link: http://maven.apache.org/

  • Check the JAVA_HOME environment variable. Maven is developed in Java, so you must know
    the installation directory of the JDK in the current system environment.

  • Unzip the core program of Maven.
    Unzip apache-maven-3.6.3-bin.zip to a non-Chinese directory with no spaces. For example, D:\ProgramFiles\maven\apache-maven-3.6.3
    Insert picture description here

  • Configure environment variables.
    MAVEN_HOME, D:\ProgramFiles\maven\apache-maven-3.6.3
    %MAVEN_HOME%\bin
    Insert picture description here
    Insert picture description here

  • View Maven version information to verify that the installation is correct mvn -v
    Insert picture description here

5: Configure local warehouse and Alibaba Cloud image

Configure local warehouse

  • Maven's core program does not contain specific functions, only responsible for macro scheduling. The specific function is
    completed by the plug -in. The Maven core program will look for plugins in the local repository. If it is
    not in the local warehouse, it will be downloaded from the remote central warehouse. At this time, if you cannot access the Internet, you cannot perform the
    specific functions of Maven . To solve this problem, we can point Maven's local warehouse to a
    directory that has been downloaded on the Internet.

  • Maven default local repository: ~.m2\repository directory.
    Tips: ~ indicates the home directory of the current user.

  • How to configure? Open Maven's core configuration file settings.xml file

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Configure Alibaba Cloud image
In order to download the jar package conveniently, configure the following tags in the tags of the settings.xml file of Maven's core configuration file:

<mirror>
 <id>nexus-aliyun</id>
 <mirrorOf>central</mirrorOf>
 <name>Nexus aliyun</name>
 <url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>

Insert picture description here

6: Configure Maven in Eclipse

Eclipse comes with a Maven plug-in by default, but the built-in Maven plug-in cannot modify the local warehouse,
so usually we don't use the built-in Maven, but use the one installed by ourselves. The
steps to configure Maven in Eclipse are as follows:

  • Click Window→Preferences in Eclipse
    Insert picture description here
  • Click the arrow in front of Maven, select Installations, and click Add...
    Insert picture description here
  • Click Directory...Select the root directory of the Maven core program we installed, and then click Finish
    Insert picture description here
  • Maven core program added on the hook
    Insert picture description here
  • Select User Settings under Maven, click Browse in the global settings...Select the configuration file settings.xml of the Maven core program, and the local warehouse will automatically change to the path we set in the settings.xml file
    Insert picture description here

7: Create a Maven project in Eclipse

Create a Java project
Click File→New→Maven Project, and the following window will pop up
Insert picture description here

Click Next, configure the coordinates (GAV) and packaging method, and then click Finish to
Insert picture description here
create the project. After creating the project, it is found that the default JDK compilation version is 1.5. Add the following configuration to the Maven core configuration file settings.xml file to change the compilation version to 1.8. Restart Eclipse to create the project and find that the default JDK compilation version is 1.5. Add the following configuration to the Maven core configuration file settings.xml file to change the compilation version to 11 (this is my version, please choose your own Version configuration), restart Eclipse

	<profile>
	<id>jdk-11</id>
	<activation>
		<activeByDefault>true</activeByDefault>
		<jdk>11</jdk>
	</activation>
	<properties>
		<maven.compiler.source>11</maven.compiler.source>
		<maven.compiler.target>11</maven.compiler.target>
		<maven.compiler.compilerVersion>11</maven.compiler.compilerVersion>
	</properties>
</profile>	

Insert picture description here
Insert picture description here
Write the main code
Create a package in the src/main/java directory and create a Hello.java file to
Insert picture description here
write test code Create a package
in the src/test/java directory and create a HelloTest.java file
Insert picture description here
Use Maven to run the Maven project
in the project name Hello or Right click on pom.xml→Run As to run Maven project
Insert picture description here
Note: In this way, it will not be saved to the local warehouse

Maven install: This method will be added to the warehouse

8: Create a parent project

The packaging method of the parent project is pom. The parent project only needs to keep the pom.xml file to create a simple Maven project. For the packaging method, select pom
Insert picture description here
and use the dependencyManagement></dependencyManagement tag in the pom.xml file for dependency management
Insert picture description here

8: Inherit the parent project in the subproject

<!-- 继承 --> 
<parent>
 <!-- 在此配置父工程的坐标 -->
</parent>

Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/m0_51755061/article/details/114677564