Build
Spring Boot 包含用于 Maven 和 Gradle 的构建插件。此部分回答了关于这些插件的常见问题。
Spring Boot includes build plugins for Maven and Gradle. This section answers common questions about these plugins.
Generate Build Information
Maven 插件和 Gradle 插件都允许生成构建信息,其中包含项目的坐标、名称和版本。还可以通过配置将插件配置为添加其他属性。当此类文件存在时,Spring Boot 会自动配置 BuildProperties
bean。
Both the Maven plugin and the Gradle plugin allow generating build information containing the coordinates, name, and version of the project.
The plugins can also be configured to add additional properties through configuration.
When such a file is present, Spring Boot auto-configures a BuildProperties
bean.
要使用 Maven 生成构建信息,请添加 build-info
目标执行,如下面的示例所示:
To generate build information with Maven, add an execution for the build-info
goal, as shown in the following example:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>{version-spring-boot}</version>
<executions>
<execution>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
有关更多详细信息,请参阅 Spring Boot Maven Plugin documentation。 |
See the Spring Boot Maven Plugin documentation for more details. |
以下示例使用 Gradle 执行相同的任务:
The following example does the same with Gradle:
springBoot {
buildInfo()
}
有关更多详细信息,请参阅 Spring Boot Gradle Plugin documentation。 |
See the Spring Boot Gradle Plugin documentation for more details. |
Generate Git Information
Maven 和 Gradle 都允许生成 git.properties
文件,其中包含在构建项目时 git
源代码存储库状态的信息。
Both Maven and Gradle allow generating a git.properties
file containing information about the state of your git
source code repository when the project was built.
对于 Maven 用户,spring-boot-starter-parent
POM 包含预先配置的用于生成 git.properties
文件的插件。要使用它,请将 Git Commit Id Plugin
声明添加到您的 POM:
For Maven users, the spring-boot-starter-parent
POM includes a pre-configured plugin to generate a git.properties
file.
To use it, add the following declaration for the Git Commit Id Plugin
to your POM:
<build>
<plugins>
<plugin>
<groupId>io.github.git-commit-id</groupId>
<artifactId>git-commit-id-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Gradle 用户可以使用 gradle-git-properties
插件达到相同目的,如下面的示例所示:
Gradle users can achieve the same result by using the gradle-git-properties
plugin, as shown in the following example:
plugins {
id "com.gorylenko.gradle-git-properties" version "2.4.1"
}
Maven 和 Gradle 插件都允许在 git.properties
中包含的属性进行配置。
Both the Maven and Gradle plugins allow the properties that are included in git.properties
to be configured.
期望 |
The commit time in |
Customize Dependency Versions
spring-boot-dependencies
POM 管理常规依赖的版本。Maven 和 Gradle 的 Spring Boot 插件可以允许使用构建属性自定义这些托管的依赖版本。
The spring-boot-dependencies
POM manages the versions of common dependencies.
The Spring Boot plugins for Maven and Gradle allow these managed dependency versions to be customized using build properties.
每个 Spring Boot 版本根据这些特定第三方依赖进行设计和测试。覆盖版本可能会导致兼容性问题。
Each Spring Boot release is designed and tested against this specific set of third-party dependencies. Overriding versions may cause compatibility issues.
要覆盖 Maven 中的依赖版本,请参阅 this sectionMaven 插件文档。
To override dependency versions with Maven, see this section of the Maven plugin’s documentation.
要覆盖 Gradle 中的依赖版本,请参阅 this sectionGradle 插件文档。
To override dependency versions in Gradle, see this section of the Gradle plugin’s documentation.
Create an Executable JAR with Maven
spring-boot-maven-plugin
可以用来创建一个可执行的 “fat” JAR。如果你使用 spring-boot-starter-parent
POM,则可以声明插件,并且你的 JAR 将重新打包如下:
The spring-boot-maven-plugin
can be used to create an executable “fat” JAR.
If you use the spring-boot-starter-parent
POM, you can declare the plugin and your jars are repackaged as follows:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
如果你不使用父 POM,仍然可以使用插件。但是,你必须另外添加一个 <executions>
部分,如下所示:
If you do not use the parent POM, you can still use the plugin.
However, you must additionally add an <executions>
section, as follows:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>{version-spring-boot}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
有关完整的使用详情,请参见 plugin documentation。
See the plugin documentation for full usage details.
Use a Spring Boot Application as a Dependency
Spring Boot 应用程序不能像 war 文件一样被用作依赖项。如果你的应用程序包含你想与其他项目共享的类,则推荐的做法是将该代码迁入一个独立的模块。然后,独立的模块由你的应用程序和其他项目所依赖。
Like a war file, a Spring Boot application is not intended to be used as a dependency. If your application contains classes that you want to share with other projects, the recommended approach is to move that code into a separate module. The separate module can then be depended upon by your application and other projects.
如果你不能按照上述建议重新安排你的代码,则必须配置 Spring Boot 的 Maven 和 Gradle 插件以生成适合用作依赖项的独立制品。可执行归档不能用作依赖项,因为 executable jar format 以 BOOT-INF/classes
封装应用程序类。这意味着当可执行 JAR 用作依赖项时,将无法找到它们。
If you cannot rearrange your code as recommended above, Spring Boot’s Maven and Gradle plugins must be configured to produce a separate artifact that is suitable for use as a dependency.
The executable archive cannot be used as a dependency as the executable jar format packages application classes in BOOT-INF/classes
.
This means that they cannot be found when the executable jar is used as a dependency.
为了生成这两个制品,一个可以用作依赖项,一个可执行,必须指定一个分类器。这个分类器将应用于可执行归档的名称,该名称将默认归档用作依赖项。
To produce the two artifacts, one that can be used as a dependency and one that is executable, a classifier must be specified. This classifier is applied to the name of the executable archive, leaving the default archive for use as a dependency.
要在 Maven 中配置分类器 exec
,你可以使用以下配置:
To configure a classifier of exec
in Maven, you can use the following configuration:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<classifier>exec</classifier>
</configuration>
</plugin>
</plugins>
</build>
Extract Specific Libraries When an Executable Jar Runs
可执行 jar 中大多数嵌套库不必解包以便运行。但是,某些库可能会有问题。例如,JRuby 包含自己的嵌套 jar 支持,它假定 jruby-complete.jar
总作为其自己的文件直接可用。
Most nested libraries in an executable jar do not need to be unpacked in order to run.
However, certain libraries can have problems.
For example, JRuby includes its own nested jar support, which assumes that the jruby-complete.jar
is always directly available as a file in its own right.
为了处理任何有问题的库,你可以标记特定的嵌套 jar,以便当可执行 jar 首次运行时自动解包。这些嵌套 jar 写在 java.io.tmpdir
系统属性所识别的临时目录的下面。
To deal with any problematic libraries, you can flag that specific nested jars should be automatically unpacked when the executable jar first runs.
Such nested jars are written beneath the temporary directory identified by the java.io.tmpdir
system property.
应小心确保你的操作系统已配置成在应用程序仍在运行时不会删除已被解包到临时目录的 jar。
Care should be taken to ensure that your operating system is configured so that it will not delete the jars that have been unpacked to the temporary directory while the application is still running.
例如,为了表明应该通过使用 Maven 插件来标记 JRuby 进行解包,你可以添加以下配置:
For example, to indicate that JRuby should be flagged for unpacking by using the Maven Plugin, you would add the following configuration:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<requiresUnpack>
<dependency>
<groupId>org.jruby</groupId>
<artifactId>jruby-complete</artifactId>
</dependency>
</requiresUnpack>
</configuration>
</plugin>
</plugins>
</build>
Create a Non-executable JAR with Exclusions
通常,如果你有可执行 jar 和不可执行 jar 作为两个独立的构建产品,则可执行版本将具有库 jar 中不需要的其他配置文件。例如,application.yaml
配置文件可能会从不可执行 JAR 中排除。
Often, if you have an executable and a non-executable jar as two separate build products, the executable version has additional configuration files that are not needed in a library jar.
For example, the application.yaml
configuration file might be excluded from the non-executable JAR.
在 Maven 中,可执行 jar 必须为主制品,你可以为该库添加分类 jar,如下所示:
In Maven, the executable jar must be the main artifact and you can add a classified jar for the library, as follows:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>lib</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>lib</classifier>
<excludes>
<exclude>application.yaml</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Remote Debug a Spring Boot Application Started with Maven
要将远程调试器附加到使用 Maven 启动的 Spring Boot 应用程序,你可以使用 maven plugin 的`jvmArguments` 属性。
To attach a remote debugger to a Spring Boot application that was started with Maven, you can use the jvmArguments
property of the maven plugin.
有关更多详细信息,请参阅 this example。
See this example for more details.
Build an Executable Archive From Ant without Using spring-boot-antlib
要通过 Ant 进行构建,您需要获取依赖项、编译,然后创建 jar 或 war 存档。要使其可执行,您可以使用 spring-boot-antlib
模块,也可以按照以下说明进行操作:
To build with Ant, you need to grab dependencies, compile, and then create a jar or war archive.
To make it executable, you can either use the spring-boot-antlib
module or you can follow these instructions:
-
如果您正在构建 jar,请将应用程序的类和资源打包到嵌套的
BOOT-INF/classes
目录中。如果您正在构建 war,请像往常一样将应用程序的类打包到嵌套的WEB-INF/classes
目录中。 -
If you are building a jar, package the application’s classes and resources in a nested
BOOT-INF/classes
directory. If you are building a war, package the application’s classes in a nestedWEB-INF/classes
directory as usual. -
将运行时依赖项添加到嵌套
BOOT-INF/lib
目录(用于 jar)或WEB-INF/lib
目录(用于 war)。记住 not 压缩存档中的条目。 -
Add the runtime dependencies in a nested
BOOT-INF/lib
directory for a jar orWEB-INF/lib
for a war. Remember not to compress the entries in the archive. -
将
provided
(嵌入式容器)依赖项添加到嵌套BOOT-INF/lib
目录(用于 jar)或WEB-INF/lib-provided
目录(用于 war)。记住 not 压缩存档中的条目。 -
Add the
provided
(embedded container) dependencies in a nestedBOOT-INF/lib
directory for a jar orWEB-INF/lib-provided
for a war. Remember not to compress the entries in the archive. -
将
spring-boot-loader
类添加到存档的根目录(以便Main-Class
可用)。 -
Add the
spring-boot-loader
classes at the root of the archive (so that theMain-Class
is available). -
使用适当的启动程序(例如
JarLauncher
,用于 jar 文件)作为清单中的Main-Class
属性,并指定其所需的其它属性作为清单条目 - 主要通过设置Start-Class
属性。 -
Use the appropriate launcher (such as
JarLauncher
for a jar file) as aMain-Class
attribute in the manifest and specify the other properties it needs as manifest entries — principally, by setting aStart-Class
property.
以下示例显示了如何使用 Ant 构建一个可执行归档:
The following example shows how to build an executable archive with Ant:
<target name="build" depends="compile">
<jar destfile="target/${ant.project.name}-${spring-boot.version}.jar" compress="false">
<mappedresources>
<fileset dir="target/classes" />
<globmapper from="*" to="BOOT-INF/classes/*"/>
</mappedresources>
<mappedresources>
<fileset dir="src/main/resources" erroronmissingdir="false"/>
<globmapper from="*" to="BOOT-INF/classes/*"/>
</mappedresources>
<mappedresources>
<fileset dir="${lib.dir}/runtime" />
<globmapper from="*" to="BOOT-INF/lib/*"/>
</mappedresources>
<zipfileset src="${lib.dir}/loader/spring-boot-loader-jar-${spring-boot.version}.jar" />
<manifest>
<attribute name="Main-Class" value="org.springframework.boot.loader.launch.JarLauncher" />
<attribute name="Start-Class" value="${start-class}" />
</manifest>
</jar>
</target>