springboot pom.xml添加依赖、分析依赖(2种方式)

被 https://www.malaoshi.top/show_1EF5qXjMmlZR.html 引用,本文不要乱改动

介绍

本文介绍 springboot 依赖

依据官网:

https://docs.spring.io/spring-boot/docs/2.2.8.RELEASE/reference/html/getting-started.html#getting-started

springboot添加依赖有2种方式:

  • 添加2个依赖,如:spring-boot-dependencies

  • 继承父工程spring-boot-starter-parent

方式一:2个依赖(推荐)

这种方式不需要继承父工程,便于以后扩展

修改pom.xml文件

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.2.2.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <!-- 不需要加版本号 -->
    </dependency>
</dependencies>

上面依赖不需要加版本号

完整pom.xml

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>top.malaoshi</groupId>
    <artifactId>myblog</artifactId>
    <version>1.0-SNAPSHOT</version>


    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.2.2.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

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

分析 spring-boot-dependencies

spring-boot-dependencies 用于 管理 springboot应用的 依赖的版本号,以后添加依赖时,就 不需要指定版本号

注意: 只是定义依赖版本号,没有导入依赖

如下图,定义 mysql 驱动的版本号,当添加mysql依赖 但 不指定版本号时,默认使用 8.0.18 版本:

如下图,也定义了 spring-boot-starter-web 的版本号,所以添加该依赖时,不需要指定版本号:

分析 spring-boot-starter-web

导入了 web 项目中需要的依赖,最终 导入 jar包,如下图

主要依赖:

  • spring-boot-starter:springboot启动器
  • org.apache.tomcat.embed:嵌入式tomcat
  • spring-webmvc:spring-mvc

xxx-starter-xxx的作用

springboot xxx-starter-xxx是什么

方式二:继承spring-boot-starter-parent

需要继承spring-boot-starter-parent父工程,该父工程继承了spring-boot-dependencies

在聚合工程中不适合

修改pom.xml文件

添加父工程

<!-- 继承父工程 -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.2.RELEASE</version>
</parent>

添加依赖,不需要加版本号

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

完整配置pom.xml

<?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 http://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.2.2.RELEASE</version>
    </parent>
    <groupId>top.malaoshi</groupId>
    <artifactId>myblog</artifactId>
    <version>1.0-SNAPSHOT</version>


    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

</project>

感谢:
https://www.cnblogs.com/ld-mars/p/11714151.html


原文出处:/show_1EF5qXhI66uM.html