Spring-boot-starter-parent Example

本博客是春季启动教程,介绍了spring-boot-starter-parent依赖关系,它是父POM,为Spring Boot应用提供依赖和插件管理,包含Java、依赖项及Maven插件的默认配置。还说明了该依赖继承自spring-boot-dependencies,以及如何在项目的pom.xml文件中覆盖默认依赖版本。

在这个春季启动教程中,我们将了解 spring-boot-starter-parent 依赖关系,它由所有 Spring启动依赖项在内部使用。我们还将了解此依赖项提供的所有配置以及如何覆盖它们。

What is spring-boot-starter-parent dependency?

spring-boot-starter-parent依赖项是父POM,它为基于Spring Boot的应用程序提供依赖性和插件管理。它包含要使用的Java的默认版本,Spring Boot使用的依赖项的默认版本以及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 http://maven.apache.org/xsd/maven-4.0.0.xsd;
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>${revision}</version>
        <relativePath>../../spring-boot-dependencies</relativePath>
    </parent>
    <artifactId>spring-boot-starter-parent</artifactId>
    <packaging>pom</packaging>
    <name>Spring Boot Starter Parent</name>
    <description>Parent pom providing dependency and plugin management for applications
        built with Maven</description>
    <properties>
        <java.version>1.8</java.version>
        <resource.delimiter>@</resource.delimiter> <!-- delimiter that doesn't clash with Spring ${} placeholders -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>
    </properties>
     
    ...
 
    <resource>
        <directory>${basedir}/src/main/resources</directory>
        <filtering>true</filtering>
        <includes>
            <include>**/application*.yml</include>
            <include>**/application*.yaml</include>
            <include>**/application*.properties</include>
        </includes>
    </resource>
 
</project>

spring-boot-starter-parent依赖项继承自spring-boot-dependencies,它在上面的POM文件的顶部定义。

此文件是实际文件,其中包含用于所有库的默认版本的信息。以下代码显示了在spring-boot-dependencies中配置的各种依赖项的不同版本:

<properties>
    <!-- Dependency versions -->
    <activemq.version>5.15.3</activemq.version>
    <antlr2.version>2.7.7</antlr2.version>
    <appengine-sdk.version>1.9.63</appengine-sdk.version>
    <artemis.version>2.4.0</artemis.version>
    <aspectj.version>1.8.13</aspectj.version>
    <assertj.version>3.9.1</assertj.version>
    <atomikos.version>4.0.6</atomikos.version>
    <bitronix.version>2.1.4</bitronix.version>
    <byte-buddy.version>1.7.11</byte-buddy.version>
    <caffeine.version>2.6.2</caffeine.version>
    <cassandra-driver.version>3.4.0</cassandra-driver.version>
    <classmate.version>1.3.4</classmate.version>
 
    ...
    ...
</properties>

上面的列表很长,您可以在此链接中阅读完整列表。

如何覆盖默认依赖版本?

如您所见,spring boot具有用于大多数依赖项的默认版本。您可以在项目的pom.xml文件的properties标记中覆盖您选择或项目需要的版本。

例如Spring boot使用google GSON库的默认版本为2.8.2。

<groovy.version>2.4.14</groovy.version>
<gson.version>2.8.2</gson.version>
<h2.version>1.4.197</h2.version>

我想使用2.7的gson依赖。所以我会在属性标签中提供这些信息。

<properties>
    <java.version>1.8</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <gson.version>2.7</gson.version>
</properties>

现在在您的eclipse编辑器中,您可以看到以下消息:托管版本为2.7该工件在org.springframework.boot中管理:spring-boot-dependencies:2.0.0.RELEASE。

tips

<think> 我们正在解决的核心问题是:在IntelliJ IDEA中通过File > New > Project > Maven(只有Maven Archetype选项)创建项目时,如何添加spring-boot-starter-parent。 回顾之前的对话: - 用户最初的问题是在创建Maven项目时只有Archetype选项,无法直接添加Spring Boot依赖。 - 我们给出了两种方法:一种是创建基础Maven项目后手动配置pom.xml;另一种是使用Spring Initializr。 - 现在用户明确要求解决在只有Maven Archetype选项的情况下无法添加spring-boot-starter-parent的问题。 因此,我们需要提供一个明确的步骤,在创建项目时(尽管只有Archetype选项)如何后续添加spring-boot-starter-parent。 解决方案如下: ### ▶ 解决步骤:在Maven Archetype项目创建后添加Spring Boot Parent #### 步骤1:创建基础Maven项目(不使用Archetype) 1. 选择 `File > New > Project > Maven` 2. **关键步骤**:取消勾选 `Create from archetype`(这样就不会使用任何原型模板) 3. 填写项目坐标(GroupId, ArtifactId, Version)后点击 `Finish` - 此时生成的是一个空的Maven项目,没有Spring Boot依赖 #### 步骤2:手动添加Spring Boot Parent到pom.xml 1. 打开项目中的 `pom.xml` 文件 2. 在 `<project>` 标签内添加如下配置: ```xml <!-- 添加Spring Boot Parent作为项目的父POM --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.3.0</version> <!-- 使用最新稳定版本 --> <relativePath/> <!-- 从仓库查找,避免本地查找失败 --> </parent> ``` #### 步骤3:添加Spring Boot Starter依赖 在 `<dependencies>` 部分添加(如果没有则创建): ```xml <dependencies> <!-- 添加Spring Boot Web起步依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> ``` #### 步骤4:添加Spring Boot Maven插件(可选但推荐) 在 `<build>` 部分添加: ```xml <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> ``` #### 步骤5:创建Spring Boot主类 1. 在 `src/main/java` 下创建包(例如 `com.example.demo`) 2. 在该包中创建主启动类: ```java package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } ``` #### 步骤6:验证项目 1. 运行 `DemoApplication` 的 `main` 方法 2. 查看控制台输出,确认Spring Boot启动成功 > ⚠️ **注意**:如果创建项目时不小心选择了Archetype(如`maven-archetype-quickstart`),则需要手动调整目录结构(添加`resources`目录并标记)[^1]。 --- ### 替代方案:使用Spring Initializr(强烈推荐) 如果遇到问题,更简单的方式是: 1. 选择 `File > New > Project` 2. 左侧选择 **Spring Initializr**(而不是Maven) 3. 配置Spring Boot版本和依赖(如Spring Web) 4. 点击 `Create` 自动生成完整项目 > 此方法会自动配置好`spring-boot-starter-parent`和目录结构,避免手动操作[^1]。 --- ### 常见问题处理 1. **依赖下载失败**: - 在pom.xml中添加阿里云镜像: ```xml <repositories> <repository> <id>aliyun-maven</id> <url>https://maven.aliyun.com/repository/public</url> </repository> </repositories> ``` 2. **目录未识别**: - 右键 `src/main/java` → `Mark Directory as` → `Sources Root` - 右键 `src/main/resources` → `Mark Directory as` → `Resources Root` 3. **Maven刷新**: - 点击IDEA右侧Maven面板的刷新按钮 🔄 - 或执行命令行:`mvn clean install` > 通过以上步骤,即使最初只有Maven Archetype选项,也能成功添加`spring-boot-starter-parent`并构建Spring Boot项目[^1]。 --- §§相关问题§§ 1. 为什么在Maven项目中需要指定`spring-boot-starter-parent`? 2. 如何解决IntelliJ IDEA中Maven依赖下载缓慢的问题? 3. Spring Boot项目启动时出现“无法找到主类”如何解决? 4. 不使用`spring-boot-starter-parent`能否创建Spring Boot项目? </think> ### 解决在 Maven Archetype 项目中添加 spring-boot-starter-parent 的问题 尽管 IntelliJ IDEA 的 Maven 创建界面只显示 Archetype 选项,您仍然可以通过后续手动配置添加 `spring-boot-starter-parent`。以下是详细解决方案: --- ### ▶ 完整操作流程(无需 Spring Initializr) #### 步骤 1:创建基础 Maven 项目 1. `File > New > Project > Maven` 2. **取消勾选** `Create from archetype`(跳过原型选择) 3. 填写坐标: ```plaintext GroupId: com.example ArtifactId: demo Version: 1.0-SNAPSHOT ``` 4. 点击 `Finish` 生成项目 ✅ #### 步骤 2:手动添加 spring-boot-starter-parent 打开 `pom.xml` 文件,**在 `<project>` 标签内添加**: ```xml <!-- 添加 Spring Boot Parent --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.3.0</version> <!-- 使用最新版本 --> <relativePath/> <!-- 强制从仓库获取 --> </parent> ``` #### 步骤 3:补全目录结构 1. 右键创建目录: ``` src/main/java → Mark as Sources Root src/main/resources → Mark as Resources Root src/test/java → Mark as Test Sources Root ``` 2. 创建主启动类 `src/main/java/com/example/DemoApplication.java`: ```java package com.example; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } ``` #### 步骤 4:添加必要依赖 在 `<dependencies>` 中添加: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> ``` #### 步骤 5:验证配置 最终 `pom.xml` 结构示例: ```xml <?xml version="1.0" encoding="UTF-8"?> <project ...> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>1.0-SNAPSHOT</version> <!-- 关键:添加的Spring Boot Parent --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.3.0</version> <relativePath/> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project> ``` --- ### ▶ 加速依赖下载(解决卡顿问题) 在 `pom.xml` 中添加阿里云镜像: ```xml <repositories> <repository> <id>aliyun-maven</id> <url>https://maven.aliyun.com/repository/public</url> </repository> </repositories> ``` --- ### ▶ 为什么需要手动添加? - IntelliJ 的 Maven 创建界面**默认不提供 Spring Boot 模板**,因此需要后续配置 - `spring-boot-starter-parent` 作为父 POM 提供: - 预定义依赖版本管理 - 默认插件配置 - 资源过滤规则 - 统一编译目标(默认 Java 17)[^1] > 💡 **提示**:创建完成后点击 **Maven 面板的刷新按钮**(🔄)加载依赖 --- ### ▶ 替代方案:命令行创建(无需 IDE 界面) ```bash mvn archetype:generate \ -DgroupId=com.example \ -DartifactId=demo \ -DarchetypeArtifactId=maven-archetype-quickstart \ -DinteractiveMode=false ``` 然后在生成的 `pom.xml` 中手动添加 `spring-boot-starter-parent` ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值