入行的时候,构建项目的工具为Ant,后来在新项目中引入了Maven。
Ant类似于C语言,构建过程的每个环节,都需要详细指定,虽然功能很强大,但是写构建脚本的过程,开发体验和维护体验比较差。
Maven类似于C++语言,基于项目模型、约定大于配置等理念,重新定义了构建过程,分离框架和插件的交互和职责,同时基于坐标和软件仓库提供了依赖管理能力,因此在迅速在各类项目中得到大力推广。
Maven对于细节的掌控力没有Ant好,对于已有项目,从Ant迁移到Maven时,涉及很多改造,比如源码位置、构建过程等迁移操作,初期工作量比较大,但后期改造完成之后,对于开发、维护的体验有极大的提升。
依据官方文档Build Tool Plugins,Spring Boot提供了对Maven、Gradle、Ant的官方支持。
本文是对官方文档的阅读笔记,摘取了一些关键内容,后续再补充验证的结果。
Maven
-
Maven Plugin
支持3.6.3及以后的版本。
支持构建jar和war两种发布形态。 -
Getting Started
修改
pom.xml
,增加如下配置:<build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins> </build>
-
Using the Plugin
增加属性的定义,样例配置,如下:<properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><java.version>17</java.version><maven.compiler.source>${java.version}</maven.compiler.source><maven.compiler.target>${java.version}</maven.compiler.target><spring-boot-starter.version>3.3.2</spring-boot-starter.version> </properties>
导入SpringBoot的依赖配置。
方法一:继承SpringBoot的父POM,配置如下:<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>${spring-boot-starter.version}</version> </parent>
方法二:导入依赖,配置如下:
<dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>${spring-boot-starter.version}</version><type>pom</type><scope>import</scope></dependency></dependencies> </dependencyManagement>
-
Goals
-
Packaging Executable Archives
使用spring-boot-starter-parent
作为父POM时,只需要修改pom.xml
,增加如下配置:<build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins> </build>
导入
spring-boot-dependencies
依赖时,则需要手工指定构建目标。<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-maven-plugin
构建得到的jar文件可以直接使用命令java -jar
方式启动,和构建目标repackage相关,官方说明如下:Repackage existing JAR and WAR archives so that they can be executed from the command line using java -jar.
-
Packaging OCI Images
-
Running your Application with Maven
执行命令mvn spring-boot:run
,运行程序。
从来没有这么玩过,暂时没有应用场景。 -
Ahead-of-Time Processing
基于Spring AOT技术,依赖GraalVM。For instance, if you want to opt-in or opt-out for certain features, you need to configure the environment used at build time to do so.
使用本特性,依据上述描述,推测需要放弃一些运行时的动态能力。
使用方面的细节和注意事项,待后续有机会再深入体验。 -
Running Integration Tests
-
Integrating with Actuator
在构建目标文件中增加META-INF/build-info.properties
,使用spring-boot-starter-parent
作为父POM时,只需要修改pom.xml
,增加如下配置:<build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><executions><execution><goals><goal>build-info</goal></goals><configuration><additionalProperties><encoding.source>UTF-8</encoding.source><encoding.reporting>UTF-8</encoding.reporting><java.version>${java.version}</java.version></additionalProperties></configuration></execution></executions></plugin></plugins> </build>
-
Help Information
执行命令mvn spring-boot:help -Ddetail=true -Dgoal=<goal-name>
,可以查看指定构建目标的帮助信息。
Gradle
- Gradle Plugin
Apache Ant
- Spring Boot AntLib Module
其它构建系统
- Supporting Other Build Systems
参考资料
源码的编码格式
- 【Maven】修改编码格式的多种方式
- Maven ,命令行中,字符编码 设置
依赖管理
- Introduction to the Dependency Mechanism
- Maven项目中pom文件中的dependencyManagement,dependencies,dependency有什么区别?
- Maven 中的dependencyManagement和dependency的区别
- Maven:<dependencyManagement>:依赖集中管理
- Maven中的dependencyManagement 意义
- Maven DependencyManagement:掌控依赖的终极利器
- Differences between dependencyManagement and dependencies in Maven
GraalVM
- 官网