应用程序版本未显示在Spring Boot banner.txt中

问题描述 投票:14回答:6

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

${application.version}

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

java spring maven spring-boot banner
6个回答
19
投票

好吧,如果我构建项目并通过java -jar运行它,则会打印该版本。但是,如果我在IDE(IntelliJ IDEA)中启动应用程序,则不会打印该版本。

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

在MANIFEST.MF中声明的应用程序的版本号。例如,Implementation-Version:1.0打印为1.0。

从IDE运行时,通常会针对IDE编译的类文件执行。 IDE通常不会经历使用清单构建整个jar的完整周期。因此,在运行时没有MANIFEST.MF可用于替换${application.version}的值,并且您将留下裸令牌。

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


5
投票

在我的例子中,我查看了spring-boot-maven-plugin创建的清单,里面没有Implementation-version。

要添加它,我在我的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时,我才能看到带有应用程序版本的横幅


1
投票

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

使用jar任务对我来说不适用于标准2.0.5.RELEASE build.gradlebecause the bootJar task takes precedence

默认情况下,配置bootJar或bootWar任务时,将禁用jar或war任务。

所以我尝试了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。已发现或已配置的主类将自动添加到MANIFEST.MF中作为“Start-Class”。

一旦这个工作,你可以像往常一样在${application.title}中使用${application.version}Spring Boot banner.txt file


0
投票

对我而言,完美取代maven build上的文本:

com.google.code.maven替换-插件

  <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中删除了$ {}


0
投票

另一种方案:

使用maven资源插件来“过滤”(替换)资源文件中的属性。

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

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

在application.properties文件中:

[email protected]@
[email protected]@

在banner.txt文件中:

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

0
投票

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

你可以参考下面的例子

© www.soinside.com 2019 - 2024. All rights reserved.