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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34519759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:44:37  来源:igfitidea点击:

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

javaspringmavenspring-bootbanner

提问by pas2al

The application version defined in the banner.txt does not show up on console, when running the application. It is defined according to the docs of Spring Boot

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

${application.version}

The project uses the spring-boot-starter-parent as parent pom (Basic project setup from start.spring.io)

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

采纳答案by Chris Nauroth

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.

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

According to the Spring Boot documentation on Customizing the Banner, the value of ${application.version}is taken from the jar manifest.

根据 Spring Boot 文档Customizing the Banner, 的值${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.

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

When running from an IDE, it's typical for execution to occur against the class files compiled by the IDE. The IDE typically doesn't go through a full cycle of building the whole jar with a manifest. Therefore, there is no MANIFEST.MF available at runtime for substituting the value of ${application.version}, and you're left with the bare token.

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

This is not a bug in your code, and you've already seen that it works correctly when doing a full jar build. If it's really important to fix this while running through the IDE, then you could consider setting up a custom build step that does go through the full jar build and manifest generation first. That's probably overkill though. The banner could be validated later outside the IDE by testing against a real release build of the jar.

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

回答by bolivier

In my case, I look inside the manifest created by spring-boot-maven-plugin and there were no Implementation-version inside.

就我而言,我查看了 spring-boot-maven-plugin 创建的清单,里面没有实现版本。

To add it, I add the plugin maven-jar-plugin in build.plugins section of my pom.xml.

为了添加它,我在 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>

After that as already mention before I can see the banner with the application version only when I do java -jar et not with my ide

在那之后,正如之前已经提到的,只有当我执行 java -jar et 而不是我的 ide 时,我才能看到带有应用程序版本的横幅

回答by ben3000

Just for reference, here's what I found works for the command-linein Spring Boot 2 with a Gradle-based project (using the Spring Boot Gradle plugin). Intellij's console still doesn't work for me, but that problem has been around for several years now.

仅供参考,这是我发现的适用于 Spring Boot 2 中命令行的基于 Gradle 的项目(使用 Spring Boot Gradle 插件)。Intellij 的控制台仍然对我不起作用,但这个问题已经存在好几年了

Using the jartask wasn't working for me on a standard 2.0.5.RELEASE build.gradle, because the bootJartask takes precedence:

jar在标准 2.0.5.RELEASE 上使用该任务对我不起作用build.gradle因为该bootJar任务优先

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

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

So I tried the bootJartask, and it works:

所以我尝试了这个bootJar任务,它有效:

version = '0.0.1-SNAPSHOT'

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

Note: There is no need for mainClassNameand its equivalents if you have only one main class. The discovered or configured main class is automatically added to the MANIFEST.MF as 'Start-Class'.

注意:如果您只有一个 main class ,则不需要mainClassName及其等效。发现或配置的主类将作为“Start-Class”自动添加到 MANIFEST.MF。

Once this is working, you can use ${application.title}and ${application.version}as usual in your Spring Boot banner.txt file.

一旦这样的工作,你可以使用${application.title}${application.version}往常一样在春季启动banner.txt文件

回答by NotLikeNames

For me works perfectly replace texts on mvn build by:

对我来说,通过以下方式完美地替换 mvn build 上的文本:

com.google.code.maven-replacer-plugin

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>

Yes I did remove ${} from banner.txt

是的,我确实从 banner.txt 中删除了 ${}

回答by Nicolas Dos Santos

Another solution:

另一种解决方案:

Use the maven resources plugin to "filter" (replace) properties in resource files.

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

In the pom, activate resources filtering with the following definition:

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

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

In the application.properties file:

在 application.properties 文件中:

[email protected]@
[email protected]@

In the banner.txt file:

在 banner.txt 文件中:

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

回答by ManjuGH

Basically ${application.title} ${application.formatted-version} values are picked from the manifest file of the packaged jar file. so i am not sure we can really print them during build life cycle of the project.enter link description here

基本上 ${application.title} ${application.formatted-version} 值是从打包的 jar 文件的清单文件中选取的。所以我不确定我们真的可以在项目的构建生命周期中打印它们。在此处输入链接描述

you can refer below example

你可以参考下面的例子