Spring Boot学习--spring-boot-starter-parent及starters

在官方文档的第三部分的13块讲述了引用的管理,官方推荐的是使用Maven和Gradle。

我一直在用的是maven,而且使用maven有些优势–spring-boot-starter-parent,这个部件是maven独有的。

这次我们从这里开始学习。

Maven的用户可以通过继承spring-boot-starter-parent项目来获得一些合理的默认配置。这个parent提供了以下特性:

spring-boot-starter-parent的引用

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

如果dependencies中的一些引用不想使用默认的版本,可以直接加上version信息,把默认的覆盖掉。

另外官方提供的覆盖默认配置的方式如下:

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

在properties中注明某个引用要使用的版本。具体使用哪种方式还是看个人习惯。

如果不想使用spring-boot-starter-parent,也可以自己来配置所要使用的版本:

<dependencyManagement>
     <dependencies>
        <dependency>
            <!-- Import dependency management from Spring Boot -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.0.0.BUILD-SNAPSHOT</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>
            <scope>import</scope>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.0.0.BUILD-SNAPSHOT</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
这里的引用声明是借助scope=import 来实现的。

如果想要把项目打包成一个可执行的jar包,需要添加maven的一下组件:

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

这里前边文章中都有说过,位置一般都是放在dependencies之后。

经验分享 程序员 微信小程序 职场和发展