These steps illustrates on how to implement a dedicated application metadata.jar. Configuring a dedicated application metadata jar improves response time and reduce disk utilization when using Spring Cloud Data Flow UI or shell.
1. Create a properties java class file. This file will specify the desired application properties that you wish to expose. In this sample we will expose property called "color".
// filename: MyAppProperties.java // For this sample, we are exposing the property name "color" @ConfigurationProperties public class MyAppProperties { /** * The mytask color, "green" by default. */ private String color = "green"; public String getColor() { Assert.hasText(color, "color must not be empty nor null"); return color; } public void setColor(String color) { this.color = color; } }
Or an empty class if there are no properties to expose.
// No propertied to expose @ConfigurationProperties public class MyAppProperties {}
2. Create META-INF/spring-configuration-metadata-whitelist.properties under resources directory. It will have list all the configuration-properties files you wish to use. If more that one, use comma as the delimiter.
## filename: resources/META-INF/spring-configuration-metadata-whitelist.properties configuration-properties.classes=org.springframework.cloud.task.app.timestamp.MyAppProperties
3. Modify pom.xml to add the spring-boot-configuration-processor dependency.
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
4. Add also the spring-cloud-app-starter-metadata-maven-plugin plugin
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-app-starter-metadata-maven-plugin</artifactId> <version>2.0.0.RELEASE</version> <executions> <execution> <id>aggregate-metadata</id> <phase>compile</phase> <goals> <goal>aggregate-metadata</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
5. Enable Configuration Properties in the application
@EnableConfigurationProperties({ MyAppProperties.class }) public class MyApp {
6. Autowire the configuration property
@Autowired private MyAppProperties config; // use of the bound property @Override public void run(String... strings) throws Exception { DateFormat dateFormat = new SimpleDateFormat(config.getFormat()); logger.info(dateFormat.format(new Date())); }
5. Build / Install app
$ mvn clean install. or $ mvn clean build
6. Verify if metadata jar is created.
$ ls -ltr target/ total 30576 drwxr-xr-x 3 bsamson staff 96 Apr 8 14:47 generated-sources drwxr-xr-x 3 bsamson staff 96 Apr 8 14:47 maven-status drwxr-xr-x 5 bsamson staff 160 Apr 8 14:47 classes -rw-r--r-- 1 bsamson staff 56004 Apr 8 14:47 mytask-0.0.6-SNAPSHOT-metadata.jar drwxr-xr-x 3 bsamson staff 96 Apr 8 14:47 generated-test-sources drwxr-xr-x 3 bsamson staff 96 Apr 8 14:47 test-classes drwxr-xr-x 4 bsamson staff 128 Apr 8 14:47 surefire-reports drwxr-xr-x 3 bsamson staff 96 Apr 8 14:47 maven-archiver -rw-r--r-- 1 bsamson staff 5524 Apr 8 14:47 mytask-0.0.6-SNAPSHOT.jar.original -rw-r--r-- 1 bsamson staff 15587727 Apr 8 14:47 mytask-0.0.6-SNAPSHOT.jar
7. Copy / Move all application binaries to your repository.
8. Define the the metadata jar when registering the application with Spring Cloud Data Flow.
References:
https://dataflow.spring.io/docs/applications/application-metadata/
https://github.com/spring-cloud/spring-cloud-dataflow-samples/tree/main/timestamp-task