Skip to content

2.Maven

使用maven可以从spring-boot-starter-parent项目继承一些默认配置。它提供了以下特性:

  • Java 1.8 as the default compiler level.
  • UTF-8 source encoding.
  • A Dependency Management section, inherited from the spring-boot-dependencies pom, that manages the versions of common dependencies. This dependency management lets you omit tags for those dependencies when used in your own pom.
  • An execution of the repackage goal with a repackage execution id.一个maven的插件
  • Sensible resource filtering.智能字符替换
  • Sensible plugin configuration (exec plugin, Git commit ID, and shade).智能插件配置
  • Sensible resource filtering for application.properties and application.yml including profile-specific files (for example, application-dev.properties andapplication-dev.yml)

注意:因为application.properties and application.yml使用了占位符${…},所以在配置文件中使用Maven的属性时其占位符被替换为@..@。(You can override that by setting a Maven property called resource.delimiter.)

1. 继承自 Starter Parent

pom.xml:

<!-- Inherit defaults from Spring Boot -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.4.RELEASE</version>
</parent>

这里可以统一版本管理。

想要替换某个属性的话,在properties中定义:

<properties>
    <spring-data-releasetrain.version>Fowler-SR2</spring-data-releasetrain.version>
</properties>

spring-boot-dependencies pom 查看有哪些属性。

2. 非继承方式使用 Spring Boot

不使用 spring-boot-starter-parent,通过使用scope=import依赖仍然可以保留依赖管理的好处(但是没有了插件管理):

<dependencyManagement>
    <dependencies>
        <dependency>
            <!-- Import dependency management from Spring Boot -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.1.4.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

想要替换某个属性的话,就需要在该依赖之前覆盖:

<dependencyManagement>
    <dependencies>
        <!-- Override Spring Data release train provided by Spring Boot -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-releasetrain</artifactId>
            <version>Fowler-SR2</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.1.4.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

3. 可执行插件

使用该插件让项目变为可执行的jar包:

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