文章目錄
- 一、引入插件
- 二、父項目為spring-boot-starter-parent時插件的功能
- 三、使用spring-boot-dependencies引入SpringBoot
- 四、打包為可執行檔案
SpringBoot對maven提供了Spring Boot Maven插件,它可以用來将項目打包為可自行的jar或者war包,運作SpringBoot應用程式,生成建構資訊和在運作整合測試前啟動SpringBoot應用。
一、引入插件
為了使用Spring Boot maven插件,需要在pom.xml檔案中添加以下代碼引入插件:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
二、父項目為spring-boot-starter-parent時插件的功能
Maven 使用者可以使用 spring-boot-starter-parent為父 項目以獲得合理的預設配置值。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.10</version>
</parent>
該父項目提供的功能包括:
- java1.8為預設編譯級别;
- UTF-8為預設編碼;
- 一個繼承自spring-boot-dependencies的依賴管理版塊,該版塊管理了常見依賴項的版本,以便省略這些依賴項的<version> 标記;
- 一個repackage goal和repackage執行id;
- 合理的資源過濾;
- 智能插件配置,例如maven-complier-plugin插件,配置了parameters,可以達到不寫@Param,mybatis也能正常讀取到參數名稱;
- 對application.properties 和 application.yml 的合理資源過濾,包括特定于配置檔案的檔案
當然,你也可以在項目中對預設的依賴版本進行覆寫,例如:
<properties>
<slf4j.version>1.7.30</slf4j.version>
<spring-data-releasetrain.version>Moore-SR6</spring-data-releasetrain.version>
</properties>
三、使用spring-boot-dependencies引入SpringBoot
如果你不想使用spring-boot-starter-parent,那麼你可以通過使用spring-boot-dependencies來引入springboot相關依賴,不同的是該方式隻會引入依賴,properties和plugins需要自己重新定義:
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.4.10</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
此種方式下不能僅通過定義<properties>來覆寫預設依賴,需要在其之前完整的聲明目标依賴,才能達到覆寫預設依賴的效果,例如:
<dependencyManagement>
<dependencies>
<!-- Override SLF4J provided by Spring Boot -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.30</version>
</dependency>
<!-- Override Spring Data release train provided by Spring Boot -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-releasetrain</artifactId>
<version>2020.0.0-SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.4.10</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
四、打包為可執行檔案
将項目打包為可執行檔案由 repackage goal執行,如以下示例所示:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
如果你使用的是 spring-boot-starter-parent,則此類執行已經預先配置了<goal>repackage</goal>,是以隻應添加插件定義。而spring-boot-dependencies則必須添加完全。
也可以配置打包時項目的主啟動類和打包格式,manifest 中的 Main-Class 由 Spring Boot 插件的 layout 屬性控制,如下例所示:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>${start.class}</mainClass>
<layout>ZIP</layout>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
layout的預設值根據項目情況預設為JAR或WAR,可取的值還有ZIP和NONE