满满的 maven 干货,你肯定不来看看吗?

前言

不管你是刚刚接触 maven 的新手,仍是熟练掌握 maven 的大神,相信都能在博主的这篇博客中有所收获。java

maven 的做用

仍然记得我在刚刚开始学习 java ,须要引入外部依赖的时候,在网络上处处去寻找 jar 包,那份无奈感直到如今依然刻在个人脑海里。不过,自从了解到 maven 这个工具,我再也没有寻找 jar 包的黑历史了。由于,maven 能够对项目中的 jar 包依赖进行统一管理啊!这个也是 maven 最大的用途了。程序员

若是在项目中咱们不使用 maven 或与之功能类似的工具,在项目中的依赖便须要咱们手动下载,十分的麻烦;可是当咱们使用 maven 构建项目时,maven 为项目提供的 pom 文件能够对项目中全部的依赖进行统一管理,项目中须要的依赖在 pom 文件中直接引入便可,由于 maven 会自动从远程仓库中下载 jar 包到本地仓库。web

什么是 maven?

在你们了解 maven 的功能以后,咱们也是时候给 maven 下一个定义了。spring

maven 是一个项目管理工具,它具备较高的可重用性,仅仅几行 maven 构建脚本就能够构建简单的项目。maven 中多个项目共享一个本地仓库,每一个项目的 jar 包都依赖于此,并且须要的依赖须要在 pom 文件中进行指定。apache

经常使用的 maven 命令

maven 的命令数其实并很多,咱们这里挑选几个经常使用的来说解一下,毕竟把所有的 maven 命令列举在这里你们也是过眼云烟对不对,不如记住几个经常使用的更有价值。(所有的 maven 命令,博主哪会啊!)tomcat

那么哪几个 maven 命令算常见呢?在 idea 中能够直接使用的这几个 maven 命令,多是各位程序员大神在工做中会常常用到的。网络

在这里插入图片描述

  1. mvn clean:会对项目进行清理,删除 target 目录下编译的内容
  2. mvn validate:验证工程是否正确,全部须要的资源是否可用
  3. mvn compile:编译项目代码
  4. mvn test:运行单元测试
  5. mvn package:打包项目
  6. mvn verify:运行任何检查,验证包是否有效
  7. mvn install:打包后将其安装在本地仓库
  8. mvn site:生成站点目录
  9. mvn deploy:打包后将其安装到 pom 文件中配置的远程仓库

pom 文件的一个示例

咱们前面说过,pom 文件能够对项目中全部的依赖进行统一管理,项目中须要的依赖在 pom 文件中直接引入便可。那么,什么是 pom 文件呢,又或者说,pom 文件长啥样呢?下面咱们来一块儿看看一个 Spring Boot 项目的 pom 文件吧。maven

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>ok</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>ok</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
  1. modelVersion:模型版本
  2. groupId:公司或者组织的惟一标志,配置时生成的路径也是由今生成
  3. artifactId:本项目的惟一 ID,一个 groupId 下可能有多个项目,其依靠 artifactId 进行区分
  4. version:本项目当前的版本号
  5. packaging:打包机制
  6. properties:定义一些常量,在 pom 中的其它地方能够直接引用
  7. dependencies:定义本项目的依赖关系
  8. dependency:每个 dependency 对应一个 jar 包
  9. scope:域,包括 compile(编译范围)、provided(已提供范围)、runtime(运行时范围)、test(测试范围)、system(系统范围)
  10. exclusions:屏蔽依赖关系
  11. plugins:插件

总结一下,pom 文件包含 maven 项目的依赖关系,打包机制及一些项目的详细信息,它也能够称得上是 maven 中最重要的文件了。ide

三种项目打包方式

三种方式分别为 pom 打包,jar 打包和 war 打包,其中打包方式须要在 pom.xml 中进行指定。svg

  1. pom:聚合工程,即父工程,作统一的依赖管理,例如管理 jar 包的版本等
  2. jar:最常规的打包方式,能够是 pom 工程的子工程
  3. war:web 工程,便可以在 tomcat 中直接运行的工程

参考:maven的用法和几个经常使用的命令