From b466fb4ed8655d0b6212c7343594886753a0c21a Mon Sep 17 00:00:00 2001 From: "Lukas Rieger (Blue)" Date: Sun, 24 Jul 2022 12:08:13 +0200 Subject: [PATCH] Make version file json again --- build.gradle.kts | 17 ++++++++++++++++ .../bluecolored/bluemap/api/BlueMapAPI.java | 20 ++++++++++++------- .../de.bluecolored.bluemap.api/version | 3 --- .../de.bluecolored.bluemap.api/version.json | 5 +++++ 4 files changed, 35 insertions(+), 10 deletions(-) delete mode 100644 src/main/resources/de.bluecolored.bluemap.api/version create mode 100644 src/main/resources/de.bluecolored.bluemap.api/version.json diff --git a/build.gradle.kts b/build.gradle.kts index 71e6eec..db1d725 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -2,8 +2,12 @@ plugins { java `java-library` id("com.diffplug.spotless") version "6.1.2" + id ("com.palantir.git-version") version "0.12.3" } +val versionDetails: groovy.lang.Closure by extra +val git = versionDetails() + group = "de.bluecolored.bluemap.api" val apiVersion: String by project @@ -56,3 +60,16 @@ tasks.javadoc { } } } + +tasks.processResources { + from("src/main/resources") { + include("de/bluecolored/bluemap/api/version.json") + duplicatesStrategy = DuplicatesStrategy.INCLUDE + + expand ( + "version" to project.version, + "gitHash" to git.gitHashFull, + "gitClean" to git.isCleanTag + ) + } +} diff --git a/src/main/java/de/bluecolored/bluemap/api/BlueMapAPI.java b/src/main/java/de/bluecolored/bluemap/api/BlueMapAPI.java index 869704d..4659984 100644 --- a/src/main/java/de/bluecolored/bluemap/api/BlueMapAPI.java +++ b/src/main/java/de/bluecolored/bluemap/api/BlueMapAPI.java @@ -24,10 +24,14 @@ */ package de.bluecolored.bluemap.api; +import com.google.gson.Gson; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; import de.bluecolored.bluemap.api.debug.DebugDump; -import java.io.IOException; import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; import java.net.URL; import java.nio.file.Path; import java.util.*; @@ -44,13 +48,15 @@ public abstract class BlueMapAPI { private static final String VERSION, GIT_HASH, GIT_CLEAN; static { String version = "DEV", gitHash = "DEV", gitClean = "DEV"; - URL url = BlueMapAPI.class.getResource("/de/bluecolored/bluemap/api/version"); + URL url = BlueMapAPI.class.getResource("/de/bluecolored/bluemap/api/version.json"); if (url != null) { - try (InputStream in = url.openStream(); Scanner scanner = new Scanner(in)) { - version = scanner.nextLine(); - gitHash = scanner.nextLine(); - gitClean = scanner.nextLine(); - } catch (IOException ex) { + Gson gson = new Gson(); + try (InputStream in = url.openStream(); Reader reader = new InputStreamReader(in)) { + JsonObject element = gson.fromJson(reader, JsonElement.class).getAsJsonObject(); + version = element.get("version").getAsString(); + gitHash = element.get("git-hash").getAsString(); + gitClean = element.get("git-clean").getAsString(); + } catch (Exception ex) { System.err.println("Failed to load version from resources!"); ex.printStackTrace(); } diff --git a/src/main/resources/de.bluecolored.bluemap.api/version b/src/main/resources/de.bluecolored.bluemap.api/version deleted file mode 100644 index f048a44..0000000 --- a/src/main/resources/de.bluecolored.bluemap.api/version +++ /dev/null @@ -1,3 +0,0 @@ -${version} -${gitHash} -${gitClean} \ No newline at end of file diff --git a/src/main/resources/de.bluecolored.bluemap.api/version.json b/src/main/resources/de.bluecolored.bluemap.api/version.json new file mode 100644 index 0000000..d1d2c53 --- /dev/null +++ b/src/main/resources/de.bluecolored.bluemap.api/version.json @@ -0,0 +1,5 @@ +{ + "version": "${version}", + "git-hash": "${gitHash}", + "git-clean": "${gitClean}" +} \ No newline at end of file