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

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

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

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

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

  • 默认使用Java 8
  • 使用UTF-8编码
  • 一个引用管理的功能,在dependencies里的部分配置可以不用填写version信息,这些version信息会从spring-boot-dependencies里得到继承。
  • 识别过来资源过滤(Sensible resource filtering.)
    识别插件的配置(Sensible plugin configuration (exec plugin, surefire, Git commit ID, shade).)
  • 能够识别application.properties和application.yml类型的文件,同时也能支持profile-specific类型的文件(如: application-foo.properties and application-foo.yml,这个功能可以更好的配置不同生产环境下的配置文件)。
  • maven把默认的占位符 @ . . @ s i n c e t h e d e f a u l t c o n f i g f i l e s a c c e p t S p r i n g s t y l e p l a c e h o l d e r s ( {…​}改为了@..@(这点大家还是看下原文自己理解下吧,我个人用的也比较少 since the default config files accept Spring style placeholders ( {…​}) the Maven filtering is changed to use @…@ placeholders (you can override that with a Maven property resource.delimiter).)
    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之后。

spring-boot-starter-parent 包maven依赖报错 - yangqjiayou的专栏
今天从 http://start.spring.io/ 下载的demo项目,导入eclipse后,pom文件一直报 parent包错,然后感觉就是自己maven镜像里面搜不到这个包, 所以改了 ma…

猜你喜欢

转载自blog.csdn.net/yhblog/article/details/83818359