Add Build Properties to a Spring Boot Application – 向Spring Boot应用程序添加构建属性

最后修改: 2020年 2月 12日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.介绍

Usually, our project’s build configuration contains quite a lot of information about our application. Some of this information might be needed in the application itself. So, rather than hard-code this information, we can use it from the existing build configuration.

通常,我们项目的构建配置包含相当多的关于我们应用程序的信息。其中一些信息可能是应用程序本身需要的。因此,与其硬编码这些信息,我们不如从现有的构建配置中使用它。

In this article, we’ll see how to use information from the project’s build configuration in a Spring Boot application.

在这篇文章中,我们将看到如何在Spring Boot应用程序中使用项目构建配置的信息。

2. The Build Information

2.建设信息

Let’s say we want to display the application description and version on our website’s home page.

比方说,我们想在网站的主页上显示应用程序的描述和版本。

Usually, this information is present in pom.xml:

通常情况下,这些信息存在于pom.xml中。

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <artifactId>spring-boot</artifactId>
    <name>spring-boot</name>
    <packaging>war</packaging>
    <description>This is simple boot application for Spring boot actuator test</description>
    <version>0.0.1-SNAPSHOT</version>
...
</project>

3. Referencing the Information in the Application Properties File

3.参考应用程序属性文件中的信息

Now, to use the above information in our application, we’ll have to first reference it in one of our application properties files:

现在,为了在我们的应用程序中使用上述信息,我们必须首先在我们的一个应用程序属性文件中引用它。

application-description=@project.description@
application-version=@project.version@

Here, we’ve used the value of the build property project.description to set the application property application-description. Similarly, application-version is set using project.version.

这里,我们使用构建属性project.description的值来设置应用程序属性application-description。同样地,application-version也是用project.version来设置。

The most significant bit here is the use of the @ character around the property name. This tells Spring to expand the named property from the Maven project.

这里最重要的一点是在属性名周围使用了@字符。这告诉Spring从Maven项目中展开命名的属性。

Now, when we build our project, these properties will be replaced with their values from pom.xml.

现在,当我们构建我们的项目时,这些属性将被替换成pom.xml中的值。

This expansion is also referred to as resource filtering. It’s worth noting that this kind of filtering is only applied to the production configuration. Consequently, we cannot use the build properties in the files under src/test/resources.

这种扩展也被称为资源过滤。值得注意的是,这种过滤只适用于生产配置。因此,我们不能使用src/test/resources下文件中的构建属性。

Another thing to note is that if we use the addResources flag, the spring-boot:run goal adds src/main/resources directly to the classpath. Although this is useful for hot reloading purposes, it circumvents resource filtering and, consequently, this feature, too.

另外需要注意的是,如果我们使用addResources标志,spring-boot:run目标会将src/main/resources直接添加到classpath。尽管这对于热重载的目的很有用,但它规避了资源过滤,因此也规避了这个功能。

Now, the above property expansion works out-of-the-box only if we use spring-boot-starter-parent.

现在,只有当我们使用spring-boot-starter-parent时,上述属性扩展才会开箱即用。

3.1. Expanding Properties Without spring-boot-starter-parent

3.1.在没有spring-boot-starter-parent的情况下扩展属性

Let’s see how we can enable this feature without using the spring-boot-starter-parent dependency.

让我们看看如何在不使用spring-boot-starter-parent依赖的情况下启用这个功能。

First, we have to enable resource filtering inside the <build/> element in our pom.xml:

首先,我们必须在<build/>中的pom.xml元素中启用资源过滤功能。

<resources>
    <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
    </resource>
</resources>

Here, we’ve enabled resource filtering under src/main/resources only.

这里,我们只在src/main/resources下启用了资源过滤功能。

Then, we can add the delimiter configuration for the maven-resources-plugin:

然后,我们可以为maven-resources-plugin添加分隔符配置。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <configuration>
        <delimiters>
            <delimiter>@</delimiter>
        </delimiters>
        <useDefaultDelimiters>false</useDefaultDelimiters>
    </configuration>
</plugin>

Note that we’ve specified the useDefaultDelimiters property as false. This ensures that the standard Spring placeholders such as ${placeholder} are not expanded by the build.

注意,我们将useDefaultDelimiters属性指定为false。这可以确保标准的Spring占位符(如${placeholder})不会被构建时扩展。

4. Using the Build Information in YAML Files

4.使用YAML文件中的构建信息

If we’re using YAML to store application properties, we might not be able to use @ to specify the build properties. This is because @ is a reserved character in YAML.

如果我们使用YAML来存储应用程序属性,我们可能无法使用@来指定构建属性。这是因为@是YAML中的一个保留字符。

But, we can overcome this by either configuring a different delimiter in maven-resources-plugin:

但是,我们可以通过maven-resources-plugin中配置一个不同的分隔符来解决这个问题。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <configuration>
        <delimiters>
            <delimiter>^</delimiter>
        </delimiters>
        <useDefaultDelimiters>false</useDefaultDelimiters>
    </configuration>
</plugin>

Or, simply by overriding the resource.delimiter property in the properties block of our pom.xml:

或者,仅仅通过覆盖我们的pom.xml的属性块中的resource.delimiter属性

<properties>
    <resource.delimiter>^</resource.delimiter>
</properties>

Then, we can use ^ in our YAML file:

然后,我们可以在YAML文件中使用^

application-description: ^project.description^
application-version: ^project.version^

5. Conclusion

5.结论

In this article, we saw how we could use Maven project information in our application. This can help us to avoid hardcoding the information that’s already present in the project build configuration in our application properties files.

在这篇文章中,我们看到了如何在我们的应用程序中使用Maven项目信息。这可以帮助我们避免在应用程序属性文件中硬编码已经存在于项目构建配置中的信息。

And of course, the code that accompanies this tutorial can be found over on GitHub.

当然,与本教程配套的代码可以在GitHub上找到over