Web Application - Introduction to Maven

  • Maven's main services are based on java project, web project construction, project information management and dependency management.

  • Based on the project object model (abbreviation: POM) concept, Maven uses a central piece of information to manage the construction, reporting and documentation steps of a project.

  • The main function:

  • Provides a standardized project structure.

The problem of inconsistency and incompatibility of different integrated development environment (IDE) project structures is solved by creating maven projects.
  • Provides a set of dependency management mechanisms.

In short, jar package dependencies and third-party resources (jar packages, plug-ins...) can be managed through the pom.xml file
  • Provides a standardized build process (compile, test, package, release...).

Maven provides a set of simple commands to complete project building, testing, packaging, deploying projects, etc., to reduce tedious operations.

Maven's dependency management model

  • The warehouse here is divided into:

  • Local warehouse: a directory on the local machine.

  • Remote warehouse (private server): Generally, a private warehouse built by a company team.

  • When the coordinates are used in the project to import the corresponding jar package,

  • First, it will check whether there is a corresponding jar package in the local warehouse:

  • If there is, it is directly referenced in the project;

  • If not, go to the central warehouse to download the corresponding jar package to the local warehouse, and then pass the reference.

  • If a remote warehouse is built, the search order of jar packages in the future will become:

  • Local warehouse -> remote warehouse -> central warehouse

Maven life cycle

  • The Maven life cycle refers to the life cycle of a Maven build project, including project cleanup, initialization, compilation, testing, packaging, integration testing, verification, deployment, and site generation .

  • Maven's life cycle is to abstract and unify all construction processes.

  • Maven provides three independent life cycles:

  • clean : processing of project cleanup

  • default (or build) : processing of project deployment

  • site : Handling of project site document creation

These three sets of life cycles are independent of each other and have no dependencies. Each set of life cycles has multiple stages, and the multiple stages in each set are sequential, and the latter stages depend on the previous stages.
Users can directly use the mvn command to call these stages to complete specific operations in the project life cycle.
The command format is: mvn life cycle phase.

clean life cycle

  • The purpose of the clean life cycle is to clean up the project,

  • It consists of three stages:

1

pre-clean

Perform some work that needs to be done before clean

2

clean

Remove all files generated by the previous build

3

post-clean

Perform some work that needs to be done immediately after clean

Enter the command mvn pre-clean in the terminal to invoke the operations that need to be performed in the pre-clean phase of the clean life cycle.

Calling mvn post-clean will execute all the operations in the above three stages. The later stage in each life cycle will depend on the previous stage. When a certain stage is executed, the previous stage will be executed first.

default (or build) life cycle

  • The default ( or build ) life cycle is the main life cycle of maven, mainly used to build applications,

  • It consists of 23 stages:

1

validate

Verify: Verify that the project is correct and that all necessary information is available to complete the project's build process.

2

initialize

Initialization: Initialize the build state, such as setting property values.

3

generate-sources

Generate Source Code: Generates any source code included in the compile phase.

4

process-sources

Process source code: process source code, say, filter arbitrary values.

5

generate-resources

Generate resource files: Generate resource files that will be included in the project package.

6

process-resources

编译:复制和处理资源到目标目录,为打包阶段最好准备。

7

compile

处理类文件:编译项目的源代码。

8

process-classes

处理类文件:处理编译生成的文件,比如说对Java class文件做字节码改善优化

9

generate-test-sources

生成测试源代码:生成包含在编译阶段中的任何测试源代码。

10

process-test-sources

处理测试源代码:处理测试源代码,比如说,过滤任意值。

11

generate-test-resources

生成测试源文件:为测试创建资源文件。

12

process-test-resources

处理测试源文件:复制和处理测试资源到目标目录。

13

test-compile

编译测试源码:编译测试源代码到测试目标目录

14

process-test-classes

处理测试类文件:处理测试源码编译生成的文件

15

test

使用合适的单元测试框架运行测试(Juint是其中之一)。

16

prepare-package

准备打包:在实际打包之前,执行任何的必要的操作为打包做准备。

17

package

打包:将编译后的代码打包成可分发格式的文件,比如JAR、WAR或者EAR文件。

18

pre-integration-test

集成测试前:在执行集成测试前进行必要的动作。比如说,搭建需要的环境。

19

integration-test

集成测试:处理和部署项目到可以运行集成测试环境中。

20

post-integration-test

集成测试后:在执行集成测试完成后进行必要的动作。比如说,清理集成测试环境。

21

verify

验证:运行任意的检查来验证项目包有效且达到质量标准

22

install

安装:安装项目包到本地仓库,这样项目包可以用作其他本地项目的依赖。

23

deploy

部署:将最终的项目包复制到远程仓库中与其他开发者和项目共享。

site生命周期

  • site生命周期的目的是建立和发布项目站点。

  • Maven能够基于pom.xml所包含的信息,自动生成一个友好的站点,方便团队交流和发布项目信息。

  • 主要包含以下4个阶段:

1

pre-site

执行一些需要在生成站点文档之前完成的工作

2

site

生成项目的站点文档

3

post-site

执行一些需要在生成站点文档之后完成的工作,并且为部署做准备

4

site-deploy

将生成的站点文档部署到特定的服务器上。

小结:

  • 从令行执行maven任务的最主要方式就是调用maven生命周期的阶段。

  • 每套生命周期是相互独立的,但是每套生命周期中阶段是有前后依赖关系的,即执行某个的时候,会按序先执行其前面所有的阶段。

  • mvn执行阶段的命令格式是:mvn 阶段1 [阶段2] [阶段n]  。如:

mvn clean
#调用clean生命周期的clean阶段,实际执行的阶段为clean生命周期中的pre-clean和clean阶段。

mvn test 
#调用default生命周期的test阶段,
#实际上会从default生命周期的第一个阶段( validate )开始执行一直到 test 阶段结束。

mvn clean install
#执行了两个阶段: clean 和 install ,
#clean 位于 clean 生命周期中, install 位于 default 生命周期中,
#所以这个命令会先从 clean 生命周期中的 pre-clean 阶段开始执行一直到 clean 生命周期的 clean 阶段;
#然后会继续从default 生命周期的 validate 阶段开始执行一直到default生命周期的 install 阶段。

mvn clean deploy
#先按顺序执行 clean 生命周期的 [pre-clean,clean] 这个闭区间内所有的阶段,
#然后按序执行 default 生命周期的 [validate,deploy] 这个闭区间内的所有阶段
#(也就是default 生命周期中的所有阶段)。
#这个命令内部包含了清理上次构建的结果、编译代码、运行单元测试、打包、将打好的包安装到本地仓库、将打好的包发布到私服仓库。

每个生命周期中含着的一系列阶段(phase)。

这些 phase 就相当于 Maven 提供的统一的接口,然后这些 phase 的实现由 Maven 的插件来完成。

Maven的插件

  • maven插件主要是服务于maven中生命周期中的阶段。

  • 输入 mvn 命令的时候,比如 mvn clean,clean 对应的就是 Clean 生命周期中的 clean 阶段。但是 clean 的具体操作是由 maven-clean-plugin 来实现的。所以说 Maven 生命周期的每一个阶段的具体实现都是由 Maven 插件实现的。

  • Maven 实际上是一个依赖插件执行的框架,每个任务实际上是由插件完成。Maven 插件通常被用来:

  • 创建 jar 文件

  • 创建 war 文件

  • 编译代码文件

  • 代码单元测试

  • 创建工程文档

  • 创建工程报告

插件通常提供了一个目标的集合,并且可以使用下面的语法执行:

<code>mvn [plugin-name]:[goal-name]</code>

例如,一个 Java 工程可以使用 maven-compiler-plugin 的 compile-goal 编译,使用以下命令:

<code>mvn compiler:compile</code>

Maven 提供了下面两种类型的插件:

类型

描述

Build plugins

在构建时执行,并在 pom.xml 的 元素中配置。

Reporting plugins

在网站生成过程中执行,并在 pom.xml 的 元素中配置。

下面是一些常用插件的列表:

插件

描述

clean

构建之后清理目标文件。删除目标目录。

compiler

编译 Java 源文件。

surefile

运行 JUnit 单元测试。创建测试报告。

jar

从当前工程中构建 JAR 文件。

war

从当前工程中构建 WAR 文件。

javadoc

为工程生成 Javadoc。

antrun

从构建过程的任意一个阶段中运行一个 ant 任务的集合。

实例:在 C:\MVN\project 目录下创建一个 pom.xml 文件。

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
    http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.companyname.projectgroup</groupId>
<artifactId>project</artifactId>
<version>1.0</version>
<build>
<plugins>
   <plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-antrun-plugin</artifactId>
   <version>1.1</version>
   <executions>
      <execution>
         <id>id.clean</id>
         <phase>clean</phase>
         <goals>
            <goal>run</goal>
         </goals>
         <configuration>
            <tasks>
               <echo>clean phase</echo>
            </tasks>
         </configuration>
      </execution>     
   </executions>
   </plugin>
</plugins>
</build>
</project>

接下来,打开命令终端跳转到 pom.xml 所在的目录,并执行下面的 mvn 命令。

mvn clean
Maven 将开始处理并显示 clean 生命周期的 clean 阶段。

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------
[INFO] Building Unnamed - com.companyname.projectgroup:project:jar:1.0
[INFO]    task-segment: [post-clean]
[INFO] ------------------------------------------------------------------
[INFO] [clean:clean {execution: default-clean}]
[INFO] [antrun:run {execution: id.clean}]
[INFO] Executing tasks
     [echo] clean phase
[INFO] Executed tasks
[INFO] ------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------
[INFO] Total time: < 1 second
[INFO] Finished at: Sat Jul 07 13:38:59 IST 2012
[INFO] Final Memory: 4M/44M
[INFO] ------------------------------------------------------------------

上面的例子展示了以下关键概念:

  • 插件是在 pom.xml 中使用 plugins 元素定义的。

  • 每个插件可以有多个目标。

  • 你可以定义阶段,插件会使用它的 phase 元素开始处理。我们已经使用了 clean 阶段。

  • 你可以通过绑定到插件的目标的方式来配置要执行的任务。我们已经绑定了 echo 任务到 maven-antrun-plugin 的 run 目标。

  • 就是这样,Maven 将处理剩下的事情。它将下载本地仓库中获取不到的插件,并开始处理。

Guess you like

Origin blog.csdn.net/weixin_41606115/article/details/129025026