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

9

43

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

${application.version}

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

Glasser answered 29/12, 2015 at 21:59 Comment(1)
This worked fine for me. I generated a project from start.spring.io. Then, I added file src/main/resources/banner.txt, with contents referencing ${application.version}. Then, I reran ./mvnw clean package. Then, I ran java -jar target/demo-0.0.1-SNAPSHOT.jar. It printed 0.0.1-SNAPSHOT as expected. If this is not working for you, do you want to provide more details, like perhaps your banner.txt file?Pity
P
43

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.

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

The version number of your application as declared in MANIFEST.MF. For example Implementation-Version: 1.0 is printed as 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.

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.

Pity answered 30/12, 2015 at 0:9 Comment(3)
It doesn't work for me even when running the jar file with java. Any idea why? I have this line inside my banner.txt file: Version ${application.version} which is shown exactly the same in the consoleWera
@AlirezaMirian, did you follow the exact same steps to build and execute that I described in my comment on the question? That's working fine for me. If that still doesn't work for you, then I'm afraid I'm out of ideas. There must be something different in your environment, but I'm not sure what.Pity
If you package the spring-boot application as WebApp and deploy it on Liberty server it does not print the application version etc.Kenwood
O
16

Another solution:

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

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

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

In the application.properties file:

[email protected]@
[email protected]@

In the banner.txt file:

${info.app.name} (${info.app.version})
Odum answered 7/12, 2018 at 13:58 Comment(2)
This worked fine for me with no adaptation in name <include>application.properties</include> even though my properties-file reads application.ymlNeomaneomah
I see the version info in the Banner in IntelliJ without building a JAR, anyhow project is set up to build a WAR.Neomaneomah
P
9

Just for reference, here's what I found works for the command-line in 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.

Using the jar task wasn't working for me on a standard 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.

So I tried the bootJar task, and it works:

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 mainClassName and 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'.

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

Polestar answered 18/9, 2018 at 4:13 Comment(0)
D
6

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

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

<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

Douglass answered 21/10, 2016 at 10:4 Comment(0)
J
6

You can also use resource filtering in banner.txt. Just use ${project.version} in banner.txt

Example for maven:

<resources>
            <resource>
                <filtering>true</filtering>
                <directory>src/main/resources</directory>
                <includes>
                    <include>banner.txt</include>
                </includes>
            </resource>
        </resources>
Joris answered 3/9, 2020 at 6:37 Comment(0)
C
0

For me works perfectly replace texts on mvn build by:

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

Crist answered 5/11, 2018 at 8:16 Comment(0)
P
0

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

you can refer below example

Plast answered 19/2, 2019 at 9:37 Comment(2)
Can you please add an example.Ell
boraji.com/spring-boot-changing-the-default-banner-example please refer this examplePlast
C
0

I used another way may be can be helpful ,You can add a plugin in pom.xml

            <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>3.2.0</version>
            <executions>
                <execution>
                    <id>default-resources</id>
                    <phase>validate</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/classes</outputDirectory>
                        <useDefaultDelimiters>false</useDefaultDelimiters>
                        <delimiters>
                            <delimiter>#</delimiter>
                        </delimiters>
                        <resources>
                            <resource>
                                <directory>src/main/resources/</directory>
                                <filtering>true</filtering>
                                <includes>
                                    <include>*.yml</include>
                                </includes>
                            </resource>
                            <resource>
                                <directory>src/main/resources/</directory>
                                <filtering>false</filtering>
                                <excludes>
                                    <exclude>*.yml</exclude>
                                </excludes>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>

And create a banner.txt under resources like

,------.,--.   ,--.  ,---.  ,--.   ,--.,------. ,--.   ,------. 
|  .---' \  `.'  /  /  O  \ |   `.'   ||  .--. '|  |   |  .---' 
|  `--,   .'    \  |  .-.  ||  |'.'|  ||  '--' ||  |   |  `--,  
|  `---. /  .'.  \ |  | |  ||  |   |  ||  | --' |  '--.|  `---. 
`------''--'   '--'`--' `--'`--'   `--'`--'     `-----'`------' 
                                                                
${application.title} ${application.version}

and assert in your application.yml file you have the properties

application:
  title: Hello
  version: V1
Cricoid answered 7/11, 2023 at 23:54 Comment(0)
G
0

For yet another Gradle user.

  1. Add following to your build.gradle
processResources {
    filesMatching('application.yaml') {
        expand(project.properties)
    }
}
  1. Assign the ${version} to a custom property e.g. app.version
app:
  version: ${version}
  1. Escape all other ${...} with \${...} in your application.yaml if needed

  2. Add ${app.version} to your banner.txt

App version: ${app.version}

Reference: https://docs.spring.io/spring-boot/docs/2.0.0.M1/reference/html/howto-properties-and-configuration.html#howto-automatic-expansion-gradle

Galitea answered 1/7 at 16:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.