Spring Boot —— 版本自动管理

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_28202661/article/details/85337852

spring-boot-starter-parent

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把默认的占位符${…​}改为了@..@(这点大家还是看下原文自己理解下吧,我个人用的也比较少 

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之前。

如果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的一下组件:

扫描二维码关注公众号,回复: 4701769 查看本文章
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

位置一般都是放在dependencies之后。

参考:Spring Boot学习--spring-boot-starter-parent及starters

猜你喜欢

转载自blog.csdn.net/qq_28202661/article/details/85337852