打包可执行归档文件

该插件可以创建可执行的归档文件(JAR 文件和 WAR 文件),其中包含应用程序的所有依赖项,然后可以通过 java -jar 命令运行。spring-doc.cadn.net.cn

打包可执行 Jars

可执行 JAR 可以使用 bootJar 任务构建。 当应用 java 插件时,该任务会自动创建,并且是 BootJar 的一个实例。 assemble 任务会自动配置为依赖于 bootJar 任务,因此运行 assemble(或 build)也会运行 bootJar 任务。spring-doc.cadn.net.cn

打包可执行的 Wars

可执行的 war 包可以使用 bootWar 任务构建。 当应用 war 插件时,该任务会自动创建,并且是 BootWar 的一个实例。 assemble 任务会自动配置为依赖于 bootWar 任务,因此运行 assemble(或 build)也会运行 bootWar 任务。spring-doc.cadn.net.cn

打包可执行和可部署的 War 包

可以将 WAR 文件打包成能够使用 java -jar 命令执行并部署到外部容器的形式。 为此,应将内嵌的 Servlet 容器运行时添加到 providedRuntime 配置中,例如:spring-doc.cadn.net.cn

dependencies {
	implementation('org.springframework.boot:spring-boot-starter-webmvc')
	providedRuntime('org.springframework.boot:spring-boot-starter-tomcat-runtime')
}
dependencies {
	implementation("org.springframework.boot:spring-boot-starter-web")
	providedRuntime("org.springframework.boot:spring-boot-starter-tomcat-runtime")
}

这确保了运行时的 JAR 包会被打包到 WAR 文件的 WEB-INF/lib-provided 目录中,从而避免与外部容器自身的类发生冲突。spring-doc.cadn.net.cn

providedRuntime 优于 Gradle 的 compileOnly 配置,因为 compileOnly 存在诸多限制,其中之一是其依赖项不会包含在测试类路径中,因此任何基于 Web 的集成测试都会失败。

打包可执行档案和普通档案

默认情况下,当配置了 bootJarbootWar 任务时,jarwar 任务会配置为使用 plain 作为其归档分类器的约定。 这确保了 bootJarjarbootWarwar 具有不同的输出位置,从而允许同时构建可执行归档和普通归档。spring-doc.cadn.net.cn

如果你希望可执行归档文件(而非普通归档文件)使用分类器(classifier),请按照以下示例为 jarbootJar 任务配置分类器:spring-doc.cadn.net.cn

tasks.named("bootJar") {
	archiveClassifier = 'boot'
}

tasks.named("jar") {
	archiveClassifier = ''
}
tasks.named<BootJar>("bootJar") {
	archiveClassifier.set("boot")
}

tasks.named<Jar>("jar") {
	archiveClassifier.set("")
}

或者,如果你更倾向于完全不构建普通的归档文件,可以像下面示例中针对 jar 任务所示的那样,禁用其对应的任务:spring-doc.cadn.net.cn

tasks.named("jar") {
	enabled = false
}
tasks.named<Jar>("jar") {
	enabled = false
}
创建原生镜像时,请勿禁用 jar 任务。 详情请参见 #33238

配置可执行归档打包

BootJarBootWar 任务分别是 Gradle 的 JarWar 任务的子类。 因此,打包普通 jar 或 war 时所有可用的标准配置选项,在打包可执行 jar 或 war 时同样可用。 此外,还提供了一些专用于可执行 jar 和 war 的配置选项。spring-doc.cadn.net.cn

配置主类

默认情况下,可执行归档文件的主类将通过在主源代码集的输出中查找包含 public static void main(String[]) 方法的类来自动配置。spring-doc.cadn.net.cn

也可以使用任务的 mainClass 属性显式配置主类:spring-doc.cadn.net.cn

tasks.named("bootJar") {
	mainClass = 'com.example.ExampleApplication'
}
tasks.named<BootJar>("bootJar") {
	mainClass.set("com.example.ExampleApplication")
}

或者,可以使用 Spring Boot DSL 的 mainClass 属性在项目范围内配置主类名称:spring-doc.cadn.net.cn

springBoot {
	mainClass = 'com.example.ExampleApplication'
}
springBoot {
	mainClass.set("com.example.ExampleApplication")
}

如果已应用 application 插件,则必须配置其 mainClass 属性,并可将其用于相同目的:spring-doc.cadn.net.cn

application {
	mainClass = 'com.example.ExampleApplication'
}
application {
	mainClass.set("com.example.ExampleApplication")
}

最后,可以在任务的清单(manifest)中配置 Start-Class 属性:spring-doc.cadn.net.cn

tasks.named("bootJar") {
	manifest {
		attributes 'Start-Class': 'com.example.ExampleApplication'
	}
}
tasks.named<BootJar>("bootJar") {
	manifest {
		attributes("Start-Class" to "com.example.ExampleApplication")
	}
}
如果主类是用 Kotlin 编写的,则应使用生成的 Java 类的名称。 默认情况下,该名称是在 Kotlin 类名后加上 Kt 后缀。 例如,ExampleApplication 会变为 ExampleApplicationKt。 如果通过 @JvmName 注解指定了其他名称,则应使用该指定的名称。

包含仅用于开发的依赖项

默认情况下,所有在 developmentOnly 配置中声明的依赖项都会从可执行的 jar 或 war 文件中排除。spring-doc.cadn.net.cn

如果你想在你的归档文件中包含在 developmentOnly 配置中声明的依赖项,请配置该任务的类路径以包含此配置,如下例所示(针对 bootWar 任务):spring-doc.cadn.net.cn

tasks.named("bootWar") {
	classpath configurations.developmentOnly
}
tasks.named<BootWar>("bootWar") {
	classpath(configurations["developmentOnly"])
}

配置需要解压的库

大多数库在嵌套于可执行归档文件中时可以直接使用,但某些库可能会出现问题。 例如,JRuby 包含其自身的嵌套 JAR 支持,该支持假设 jruby-complete.jar 始终可直接在文件系统上访问。spring-doc.cadn.net.cn

为处理任何存在问题的库,可将可执行归档文件配置为在运行时将特定的嵌套 JAR 解压到临时目录。 可以使用 Ant 风格的模式来标识需要解压的库,这些模式会与源 JAR 文件的绝对路径进行匹配:spring-doc.cadn.net.cn

tasks.named("bootJar") {
	requiresUnpack '**/jruby-complete-*.jar'
}
tasks.named<BootJar>("bootJar") {
	requiresUnpack("**/jruby-complete-*.jar")
}

如需更精细的控制,也可以使用闭包。 该闭包会接收一个 FileTreeElement 参数,并应返回一个 boolean 值,用以指示是否需要解包。spring-doc.cadn.net.cn

使用 PropertiesLauncher

要使用 PropertiesLauncher 启动可执行的 jar 或 war 文件,请配置任务的清单(manifest),以设置 Main-Class 属性:spring-doc.cadn.net.cn

tasks.named("bootWar") {
	manifest {
		attributes 'Main-Class': 'org.springframework.boot.loader.launch.PropertiesLauncher'
	}
}
tasks.named<BootWar>("bootWar") {
	manifest {
		attributes("Main-Class" to "org.springframework.boot.loader.launch.PropertiesLauncher")
	}
}

打包分层 Jar 或 War

默认情况下,bootJar 任务会构建一个归档文件,其中应用程序的类和依赖项分别位于 BOOT-INF/classesBOOT-INF/lib 目录中。 类似地,bootWar 任务构建的归档文件将应用程序的类放在 WEB-INF/classes 中,依赖项则放在 WEB-INF/libWEB-INF/lib-provided 中。 在需要从 JAR 文件内容构建 Docker 镜像的情况下,进一步分离这些目录会很有用,以便将它们写入不同的层中。spring-doc.cadn.net.cn

分层 JAR 文件使用与常规 Boot 打包的 JAR 文件相同的布局,但包含一个额外的元数据文件,用于描述每一层。spring-doc.cadn.net.cn

默认情况下,定义了以下层:spring-doc.cadn.net.cn

层的顺序非常重要,因为它决定了当应用程序的一部分发生变化时,先前的层有多大可能被缓存。 默认顺序为 dependenciesspring-boot-loadersnapshot-dependenciesapplication。 应首先添加最不可能发生变化的内容,然后依次添加更可能发生变更的层。spring-doc.cadn.net.cn

要禁用此功能,您可以按以下方式操作:spring-doc.cadn.net.cn

tasks.named("bootJar") {
	layered {
		enabled = false
	}
}
tasks.named<BootJar>("bootJar") {
	layered {
		enabled.set(false)
	}
}

创建分层的 JAR 或 WAR 文件时,spring-boot-jarmode-tools JAR 将作为依赖项添加到您的归档文件中。 当该 JAR 位于类路径上时,您可以以一种特殊模式启动应用程序,该模式允许引导代码运行与您的应用程序完全不同的内容,例如用于提取各层的工具。 如果您希望排除此依赖项,可以按以下方式操作:spring-doc.cadn.net.cn

tasks.named("bootJar") {
	includeTools = false
}
tasks.named<BootJar>("bootJar") {
	includeTools.set(false)
}

自定义层配置

根据您的应用程序,您可能需要调整图层的创建方式并添加新的图层。spring-doc.cadn.net.cn

这可以通过配置来实现,该配置描述了如何将 JAR 或 WAR 文件划分为多个层,以及这些层的顺序。 以下示例展示了如何显式地定义上述默认的层顺序:spring-doc.cadn.net.cn

tasks.named("bootJar") {
	layered {
		application {
			intoLayer("spring-boot-loader") {
				include "org/springframework/boot/loader/**"
			}
			intoLayer("application")
		}
		dependencies {
			intoLayer("application") {
				includeProjectDependencies()
			}
			intoLayer("snapshot-dependencies") {
				include "*:*:*SNAPSHOT"
			}
			intoLayer("dependencies")
		}
		layerOrder = ["dependencies", "spring-boot-loader", "snapshot-dependencies", "application"]
	}
}
tasks.named<BootJar>("bootJar") {
	layered {
		application {
			intoLayer("spring-boot-loader") {
				include("org/springframework/boot/loader/**")
			}
			intoLayer("application")
		}
		dependencies {
			intoLayer("application") {
				includeProjectDependencies()
			}
			intoLayer("snapshot-dependencies") {
				include("*:*:*SNAPSHOT")
			}
			intoLayer("dependencies")
		}
		layerOrder.set(listOf("dependencies", "spring-boot-loader", "snapshot-dependencies", "application"))
	}
}

layered DSL 由三个部分定义:spring-doc.cadn.net.cn

intoLayerapplication 部分中,使用嵌套的 dependencies 闭包来为某一层声明内容。 这些闭包按照其定义的顺序从上到下依次执行。 任何未被前面的 intoLayer 闭包声明的内容,将保持可用状态,供后续闭包考虑使用。spring-doc.cadn.net.cn

intoLayer 闭包通过嵌套的 includeexclude 调用来声明内容。 application 闭包对 include/exclude 参数使用 Ant 风格的路径匹配。 dependencies 部分使用 group:artifact[:version] 模式。 它还提供了 includeProjectDependencies()excludeProjectDependencies() 方法,可用于包含或排除项目依赖。spring-doc.cadn.net.cn

如果没有调用 include,则所有内容(未被先前的闭包声明的部分)都将被考虑。spring-doc.cadn.net.cn

如果没有调用 exclude,则不会应用任何排除规则。spring-doc.cadn.net.cn

查看上面示例中的 dependencies 闭包,我们可以看到第一个 intoLayer 会将所有项目依赖分配给 application 层。 下一个 intoLayer 会将所有 SNAPSHOT 依赖分配给 snapshot-dependencies 层。 第三个也是最后一个 intoLayer 会将剩余的所有内容(在本例中,即既不是项目依赖也不是 SNAPSHOT 的依赖)分配给 dependencies 层。spring-doc.cadn.net.cn

application 闭包具有类似的规则。 首先将 org/springframework/boot/loader/** 内容分配给 spring-boot-loader 层。 然后将所有剩余的类和资源分配给 application 层。spring-doc.cadn.net.cn

添加 intoLayer 闭包的顺序通常与图层被写入的顺序不同。 因此,必须始终调用 layerOrder 方法,并且该方法必须涵盖所有在 intoLayer 调用中引用的图层。