Spring Boot - Set & Get Application Name and Version

This tutorial explains how to set the application name and version of a Spring Boot application, as well as how to get those values programmatically.

An application usually has a name along with the version. How to set the application name and version depends on the framework. If the framework defines how to do it, you should follow the instructions since the framework may use the values for various purposes.

In the Spring framework, there are several common usages of the application name.

  • Used as the registered name when using a service registri like eureka.
  • Used to lookup application properties files whose name matching {applicationName}[-{profile}].{properties|yml} format.
  • Used to address an instance in Spring Cloud Bus.

Therefore, it's important to set the values appropriately. In this tutorial, I am going to explain how to properly set the application name and version, including how to set the values directly or use the values from Gradle or Maven. In addition, there are also examples of how to get the name and version programmatically.

Set Application Name and Version

Changing the application name and version is as simple as changing the values of application properties. However, sometimes you may also want to use the values defined in the build automation tool that you use, such as Gradle or Maven. In this section, I am going to explain which properties you need to update, either directly set the value or use the values from Gradle or Maven.

Set Application Properties Values

To change the application name, you have to set the spring.application.name property. Meanwhile, the property for the version is spring.application.version. Below is the example of an application.properties file that sets those values.

  spring.application.name=my-application
  spring.application.version=1.2

If you use a YAML file instead, you can do the similar thing.

  spring:
    application:
      name: my-application
      version: 1.2

Using Values from Gradle

If you use Gradle as the build automation tool, you may want to obtain the application name and version from Gradle. By default, the project name for a root Gradle project is defined in the settings.gradle file.

  rootProject.name = 'woolha-tutorial'

For a subproject, the default name is the folder name. If you want to use a different name, it can be changed in the settings.gradle file

  project(":foo").name = "bar"

For the version value, you have to define a version attribute in the build.gradle file.

  version '1.2-SNAPSHOT'

Then, you have to add the following to your build.gradle file. It's used to tell Gradle to perform resource processing on files whose name matches the given pattern. The example below already handles both application.properties and application.yml files.

  processResources {
    filesMatching(["application*.properties", "application*.yml"]) {
      expand(project.properties)
    }
  }

In the property file, in order to make Gradle injects its values, you have to write ${name} for the application name and ${version} for the application version.

  spring.application.name=${name}
  spring.application.version=${version}

Using Values from Maven

If you use Maven, it's also necessary to do the similar thing. First of all, you have to define the <artifactId> and <version> tags right under the <project> tag. If you want to use the name as an identifier, you should use the <artifactId> rather than the <name> tag.

  <project ...> 
    <artifactId>demo</artifactId>
    <version>1.2.3-SNAPSHOT</version>
      ...
  </project>

To make Maven processes the property file, you have to add it as a resource. Under the <build> tag, add a <resources> tag if it doesn't exist. Then, define a new <resource> for the property file to be processed.

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

In the property file, you can refer to a value in the pom.xml file by specifying the path from the root, separated by . (dot). It begins and ends with a @ symbol.

  spring.application.name=@project.artifactId@
  spring.application.version=@project.version@

Get Application Name and Version

The name of the application can be obtained from the ApplicationContext. Alternatively, since the values are defined in a property file, you can obtain it using @Value annotation.

Using ApplicationContext

In a Spring managed class, you can access the ApplicationContext instance since it can be autowired. To get the value defined as the application name, you can call the getId method of the ApplicationContext.

  @Service
  public class MyService {
  
    private final ApplicationContext applicationContext;
  
    public MyService(ApplicationContext applicationContext) {
      this.applicationContext = applicationContext;
    }
  
    public void test() {
      System.out.println("context ID: " + this.applicationContext.getId());
    }
  }

Using @Value Annotation

Below is the example of how to get the application name and version using the @Value annotation.

  @Service
  public class MyService {
  
    @Value("${spring.application.name}")
    private String applicationName;
  
    @Value("${spring.application.version}")
    private String applicationVersion;
  
    public void test() {
      System.out.println("name: " + this.applicationName);
      System.out.println("version: " + this.applicationVersion);
    }
  }

Summary

In this tutorial, we have learned how to set the application name and version of a Spring application. Basically, you have to set the value of spring.application.name and spring.application.version properties. It's also possible to set the values from Gradle or Maven. To get the values, you can use ApplicationContext or @Value annotation.

You can also read about: