【SpringBoot】pom文件中不使用默认的parent

95 阅读1分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第8天,点击查看活动详情

SpringBoot骨架的生成方法

  1. 去官网生成:start.spring.io/
  2. 通过idea生成。

SpringBoot默认的parent依赖

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.4</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

去掉默认的parent依赖

  1. 添加一个dependencyManagement下的dependency
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.7.4</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
  1. 修改spring-boot-maven-plugin插件
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>2.7.4</version>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

简单测试

@SpringBootApplication
@RestController
public class Learn02Application {

    public static void main(String[] args) {
        SpringApplication.run(Learn02Application.class, args);
    }

    @RequestMapping("/hello")
    public String hello(){
        return "hello";
    }
}
  1. 使用curl命令访问。

  1. 浏览器访问 http://localhost:8080/hello