基础0-基于IDEA2020.1.2,整合SpringBoot与Maven

整合SpringBoot方式

基于Maven,整合SpringBoot有2种方式:

方式1:继承父级POM

<!-- Inherit defaults from Spring Boot -->

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>2.2.2.RELEASE</version>

</parent>

方式2:自己编写POM

<dependencyManagement>

<dependencies>

<dependency>

<!-- Import dependency management from Spring Boot -->

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

这里,我选择方式1,官方参考资料:

https://docs.spring.io/spring-boot/docs/2.3.2.RELEASE/maven-plugin/reference/html/#using-import

https://docs.spring.io/spring-boot/docs/2.3.2.RELEASE/maven-plugin/reference/html/#using

https://docs.spring.io/spring-boot/docs/2.3.2.RELEASE/gradle-plugin/reference/html/#getting-started

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

https://docs.spring.io/spring-boot/docs/2.3.2.RELEASE/reference/html/build-tool-plugins.html#build-tool-plugins-maven-plugin

spring-boot-starter-parent

通过查看spring-boot-starter-parent-x.x.x.RELEASE.pom文件,我们可以发现其通过<parent/>标签继承自spring-boot-dependencies;

那我们继续打开spring-boot-dependencies-x.x.x.RELEASE.pom文件,然后我们发现该POM文件通过<dependencyManagement/>来声明了大量的<dependencies/>,例如:spring-boot-starter-XXXX;

结合我们在Maven3中总结的<dependencyManagement/>标签功能,该标签只是对jar的version进行管控的管理器,自身并不下载对应的jar包,因为为了使用SpringBoot,开发人员在自己项目中还是要使用<dependencies/>来声明依赖的jar包;

也就是说dependencies.pom中通过<dependencyManagement/>声明了大量的依赖包,当开发者需要某些依赖包时,需要开发者自己再在项目POM文件中通过<dependencies/>去导入,例如:

当我们需要SpringBoot核心启动器时,需要声明spring-boot-starter;

当我们需要web场景时,需要声明spring-boot-starter-web;

当我们需要测试时,需要声明spring-boot-starter-test;

SpringBoot将所有的功能场景都抽取出来,做成一个个的starters(启动器),只需要在项目里面引入这些starter相关场景的所有依赖都会导入进来。要用什么功能就导入什么场景的启动器;

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>com.springboot</groupId>
    <artifactId>springboot.test</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath/>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>

注意事项:<parent/>必须确保镜像Maven中能找到才行,否则一切都是徒劳;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值