[Maven] study notes: automated build tool Maven

[Maven] study notes: automated build tool Maven

1. What is Maven

Maven is an automated build tool organized and maintained by the Apache Software Foundation , focusing on project building and dependency management serving the Java platform

2. Why use Maven

In the past, we had to rely on some jar packages to develop Java applications. We had to find these required jar packages online and copy them to the corresponding project directory (WEB-INF/lib ).

Disadvantages:

1. Every time you have to look for the corresponding jar package online, it wastes time .

2. There are dependencies between jar packages. For example, if you import jar package A, A itself depends on jar package B. At this time, you also need to import jar package B. Moreover, the dependency relationship between jar packages will be more complicated. It is possible to introduce more than a dozen jar packages while introducing several jar packages. Therefore, manually importing jar packages and understanding their dependencies is a waste of time .

3. Every time a new project is created, the jar package needs to be repeatedly copied to the WEB-INF/lib directory, which results in a large number of duplicate files in the work area, which makes our project very bloated .

Maven can solve the above problems perfectly.

3. Introduction to Maven

Nine core concepts of Maven:

①POM ②Contracted directory structure ③Coordinates ④Dependency management ⑤Warehouse management ⑥Life cycle ⑦Plugins and goals ⑧Inheritance⑨Aggregation

3.1 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.

3.2 Conventional directory structure

The agreed directory structure is an indispensable part for Maven to achieve automated construction. Take automatic compilation. Maven must be able to find the Java source files before the next step can be compiled. After compilation, there must be an accurate location to maintain The compiled bytecode file.

In development, it is generally believed that: convention>configuration>coding .

It means that problems that can be solved by configuration are not coded, and those that can be based on conventions are not configured. And Maven can automate the construction of our Java project because it specifies the directory where the specific file is saved.

If we need to let third-party tools or frameworks know where the resources we created during development are, there are basically two ways:
①Tell it clearly through 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

3.3 Coordinates

Maven uses the following three vector coordinates (GAV) to uniquely determine a Maven project in the Maven warehouse.

[1] groupid: the domain name of the company or organization + the current project name

[2]artifactId: the module name of the current project

[3]version: the version of the current module

3.4 Dependency Management

Use the dependency tag to specify the coordinates of the dependent jar package

<dependency>

<groupId>com.atguigu.maven</groupId>

<artifactId>Hello</artifactId>

<version>0.0.1-SNAPSHOT</version>

<scope>compile</scope>

</dependency>

3.5 Warehouse Management

3.5.1 Classification

[1] Local warehouse: Serve all Maven projects on the current local computer.

[2] Remote warehouse:

​ (1) Private server: Set up in the current local area network environment and serve all Maven projects within the current local area network.

​ (2) Central warehouse: set up on the Internet and serve 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.

3.5.2 Contents in the warehouse

[1] Plug-ins needed by Maven itself

[2] Maven 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

3.6 Life Cycle

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

When running any stage, all previous stages will be run . For example, when we run mvn install (installation), the code will be compiled, tested, and packaged. This is why Maven can automate all aspects of the build process. In addition, Maven's plug-in mechanism is completely dependent on the life cycle of Maven, so understanding the life cycle is very important.

A typical Maven build life cycle is composed of a sequence of the following stages:

Insert picture description here

Maven has the following three standard life cycles

  • clean : the processing of project cleaning
  • default (or build) : the processing of project deployment
  • site : Processing of project site document creation

For an overview of the life cycle, you can refer to the rookie tutorial:

https://www.runoob.com/maven/maven-build-life-cycle.html

3.7 Plugins and goals

● The core of Maven only defines an abstract life cycle , and specific tasks are completed by plug-ins .

●Each plug-in can achieve 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 the plug-in maven-compiler-plugin; pre-clean is a goal of the plug-in maven-clean-plugin.

3.8 Inheritance

Suppose there are three projects A, B, C

Junit version of A: 4.0

Junit version of B: 4.0

Junit version of C: 4.9

At this time, if the project needs to unify the junit version of each module to 4.9, then manual modification in each project is undoubtedly very undesirable. Using the inheritance mechanism, such dependency information can be extracted to the parent project module for unified management

3.8.1 Create parent project

The operation of creating a parent project is the same as creating a general Java project. The only thing to note is that the packaging method should be set to pom

<groupId>...</groupId>

<artifactId>...</artifactId>

<version>...</version>

<packaging>pom</packaging>

3.8.2 Introducing the parent project into the sub-project CTS

<parent>

<!-- 父工程坐标 -->

<groupId>...</groupId>

<artifactId>...</artifactId>

<version>...</version>

<relativePath>从当前目录到父项目的 pom.xml 文件的相对路径</relativePath>

</parent>

3.8.3 Manage dependencies in the parent project

Enclose the dependencies tag in the Parent project with the dependencyManagement tag

<dependencies>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.9</version>

<scope>test</scope>

</dependency>

</dependencies>

</dependencyManagement>

Re-specify the required dependencies in the sub-project, delete the scope and version number

<dependencies>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

</dependency>

</dependencies>

3.9 Aggregation

3.9.1 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, and install each module project with one click.

3.9.2 How to configure aggregation

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

<modules>

<module>../Hello</module>

<module>../HelloFriend</module>

<module>../MakeFriends</module>

</modules>

3.10 Common commands

mvn clean: clean up

mvn compile: compile

mvn test-compile: compile test

mvn test: test

mvn package: package

mvn install: install

mvn site: Generate site

Guess you like

Origin blog.csdn.net/hkdhkdhkd/article/details/113262388