Here you have – the source code of Java Vaadin App Version Component with a main method that executes Self-Changing functionality.
package com.programtom.my_app.views;
import com.vaadin.flow.component.html.Div;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
public class AppVersionComponent extends Div {
static final String version = "2";
static final String date = "2024-06-23 19:46:59";
static final DateFormat df = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
public AppVersionComponent() {
super("Version: " + version + " Released: " + date);
}
public static void main(String[] args) throws IOException {
File file = new File(System.getProperty("user.dir") + File.separator+"src"+ File.separator+"main"+ File.separator+"java"+ File.separator+"com"+ File.separator+"programtom"
+ File.separator+"my_app"+ File.separator+"views"+ File.separator+"AppVersionComponent.java");
List<String> strings = Files.readAllLines(file.toPath());
for (int i = 0; i < strings.size(); i++) {
if (strings.get(i).startsWith(" static final String version = ")) {
String substring = strings.get(i).substring(strings.get(i).indexOf("\"") + 1, strings.get(i).lastIndexOf("\""));
int vInt = Integer.parseInt(substring);
strings.set(i, " static final String version = \"" + (vInt + 1) + "\";");
}
if (strings.get(i).startsWith(" static final String date = ")) {
strings.set(i, " static final String date = \"" + df.format(new Date()) + "\";");
}
}
Files.write(file.toPath(), strings.stream().collect(Collectors.joining("\n")).getBytes(StandardCharsets.UTF_8));
}
}
Change my_app with your package and file path – according to your needs. you could plug this in
java.exe -cp compiledversionofyourapp.jar -Dloader.main=com.programtom.my_app.views.AppVersionComponent org.springframework.boot.loader.PropertiesLauncher
I’m planning to integrate it in some of my apps: https://programtom.com/dev/product-category/technologies/spring-boot-framework/?orderby=date-desc
I’ve also developed flutter_upgradable_plugin: https://github.com/tomavelev/flutter_updateable_plugin some time ago. I’m planning to update in near future.
