Spring 启动 banner.txt 中未显示应用程序版本

Application version does not show up in Spring Boot banner.txt

banner.txt 中定义的应用程序版本未显示在控制台上,当 运行 应用程序时。它是根据 docs of Spring Boot

定义的
${application.version}

该项目使用 spring-boot-starter-parent 作为父 pom(来自 start.spring.io 的基本项目设置)

Ok, the version gets printed if i build the project and run it via java -jar. But if i start the application within my IDE (IntelliJ IDEA) the version will not be printed.

根据 Customizing the Banner 上的 Spring 引导文档,${application.version} 的值取自 jar 清单。

The version number of your application as declared in MANIFEST.MF. For example Implementation-Version: 1.0 is printed as 1.0.

当 运行 来自 IDE 时,通常会针对 IDE 编译的 class 文件执行。 IDE 通常不会经历使用清单构建整个 jar 的完整周期。因此,在运行时没有可用的 MANIFEST.MF 来替换 ${application.version} 的值,您只剩下空标记。

这不是您的代码中的错误,您已经看到它在进行完整的 jar 构建时可以正常工作。如果在 运行 到 IDE 期间修复此问题真的很重要,那么您可以考虑设置一个自定义构建步骤,该步骤首先完成完整的 jar 构建和清单生成。不过,这可能有点矫枉过正。稍后可以通过针对 jar 的真实发布版本进行测试,在 IDE 之外验证横幅。

在我的例子中,我查看 ide 由 spring-boot-maven-plugin 创建的清单 并且没有实现版本 inside.

为了添加它,我在 pom.xml 的 build.plugins 部分添加了插件 maven-jar-plugin。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>3.0.2</version>
    <configuration>
         <archive>
              <manifestEntries>
                    <Implementation-Version>${project.version}</Implementation-Version>
              </manifestEntries>
         </archive>
    </configuration>
</plugin>

在那之后,正如之前已经提到的,只有当我执行 java -jar 而不是我的 ide

时,我才能看到带有应用程序版本的横幅

仅供参考,这是我发现在 Spring Boot 2 中的命令行 和基于 Gradle 的项目(使用Spring 引导 Gradle 插件)。 Intellij 的控制台仍然不适合我,但是 problem has been around for several years now.

使用 jar 任务在标准 2.0 上对我不起作用。5.RELEASE build.gradle, because the bootJar task takes precedence:

By default, when the bootJar or bootWar tasks are configured, the jar or war tasks are disabled.

所以我尝试了 bootJar 任务,它成功了:

version = '0.0.1-SNAPSHOT'

bootJar {
    mainClassName = 'com.demo.Application'
    manifest {
        attributes('Implementation-Title':   'Demo Application',
                   'Implementation-Version': version)
    }
}

注意:不需要 mainClassName 及其等效项 if you have only one main class。发现或配置的主要 class 自动添加到 MANIFEST.MF 作为 'Start-Class'。

一旦生效,您就可以像往常一样在 Spring Boot banner.txt file 中使用 ${application.title}${application.version}

对我而言,mvn build 上的文本完美替换为:

com.google.code.maven-replacer-plugin

  <plugin>
    <groupId>com.google.code.maven-replacer-plugin</groupId>
    <artifactId>replacer</artifactId>
    <version>1.5.3</version>
    <executions>
      <execution>
        <phase>prepare-package</phase>
        <goals>
          <goal>replace</goal>
        </goals>
      </execution>
    </executions>
    <configuration>
      <file>target/classes/banner.txt</file>
      <replacements>
        <replacement>
          <token>application.title</token>
          <value>${artifactId}</value>
        </replacement>
        <replacement>
          <token>application.version</token>
          <value>${version}</value>
        </replacement>
      </replacements>
    </configuration>
  </plugin>

是的,我确实从 banner.txt

中删除了 ${}

另一个解决方案:

使用 maven 资源插件"filter"(替换)资源文件中的属性。

在 pom 中,使用以下定义激活资源过滤:

<resources>
    <resource>
        <filtering>true</filtering>
        <directory>src/main/resources</directory>
        <includes>
            <include>application.properties</include>
        </includes>
    </resource>
</resources>

在 application.properties 文件中:

info.app.name=@project.artifactId@
info.app.version=@project.version@

在 banner.txt 文件中:

${info.app.name} (${info.app.version})

基本上 ${application.title} ${application.formatted-version} 值是从打包的 jar 文件的清单文件中挑选的。所以我不确定我们是否真的可以在项目的构建生命周期中打印它们。enter link description here

你可以参考下面的例子

您还可以在banner.txt中使用资源过滤。只需在 banner.txt

中使用 ${project.version}

maven 示例:

<resources>
            <resource>
                <filtering>true</filtering>
                <directory>src/main/resources</directory>
                <includes>
                    <include>banner.txt</include>
                </includes>
            </resource>
        </resources>