美文网首页
2019-11-21 文摘

2019-11-21 文摘

作者: ccczyl2006 | 来源:发表于2019-11-21 21:40 被阅读0次

13.2 Maven

Maven用户可以从spring-boot-starter-parent项目继承来获得合理的默认值.父项目提供了以下特性:

  • 编译默认使用Java 1.8版本.
  • 源文件使用UTF-8编码.
  • 合理的资源过滤
  • 合理的插件配置(exec plugin, Git commit Idshade).
  • 合理的资源过滤为application.propertiesapplication.yml以及特点配置文件(例如application-dev.propertiesapplication-dev.yml).

注意,因为application.propertiesapplication.yml文件能接受Spring样式的暂位符(${...}),Maven过滤更改为使用@...@暂位符(你可以通过覆盖Maven中的resource.delimiter属性配置来上述配置).

Inheriting the Starter Parent

为了配置你的项目继承spring-boot-starter-parent,配置parent节点如下:

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

Note

配置父依赖时,你仅仅只需要配置Spring Boot版本即可.如果你导入了其他额外的starter,你可以安全的忽略版本号.

配置父依赖的情况下,你也可以在你的项目中通过覆盖属性的来覆盖父依赖的默认依赖.例如,打算升级到Spring Data的另一个发行版,你需要增加以下配置到你的POM.xml文件中:

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

Using Spring Boot without the Parent POM

并不是每个人都喜欢继承spring-booot-starter-parent POM.您可能有您自己需要继承企业标准父模块,或者您可能倾向于显式地声明所有的Maven配置.

如果您不想使用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.0.2.RELEASE</version>
      <type>pom</type>
      <scope>import</scope>
      </dependency>
  </dependencies>
</dependencyManagement>

如上所述,上述的示例设置没让您使用属性来覆盖单个依赖项.要达到同样的效果,您需要在你项目的dependencyManagement节点定义spring-boot-dependencies依赖条目之前增加一个条目.
例如,需配置升级到另一个Spring Data发行版,您可以将以下元素添加到您的pom.xml中.(注意Spring Data配置需在spring-boot-dependencies之前).

<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>
      <!-- Import dependency management from Spring Boot -->
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-dependencies</artifactId>
      <version>2.0.2.RELEASE</version>
      <type>pom</type>
      <scope>import</scope>
      </dependency>
  </dependencies>
</dependencyManagement>

Note

在上述的例子中,我们配置了BOM,但是任何的依赖配置都可以同样的方式被覆盖.

Using the Spring Boot Maven Plugin

Spring Boot包含了一个Maven plugin,该插件可把项目打包成可执行jar包.
如果你想使用该插件,请配置该插件在<plugins>节点下,如下例子所示:

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

Note

如果你的项目使用Srping Boot Starter父POM, 你只需要增加插件配置即可.除非你想修改父POM定义的插件配置,否则你无需配置.

相关文章

网友评论

      本文标题:2019-11-21 文摘

      本文链接:https://www.haomeiwen.com/subject/gdsxwctx.html