Make version file json again

This commit is contained in:
Lukas Rieger (Blue) 2022-07-24 12:08:13 +02:00
parent 06359955b5
commit b466fb4ed8
No known key found for this signature in database
GPG Key ID: 2D09EC5ED2687FF2
4 changed files with 35 additions and 10 deletions

View File

@ -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<com.palantir.gradle.gitversion.VersionDetails> 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
)
}
}

View File

@ -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();
}

View File

@ -1,3 +0,0 @@
${version}
${gitHash}
${gitClean}

View File

@ -0,0 +1,5 @@
{
"version": "${version}",
"git-hash": "${gitHash}",
"git-clean": "${gitClean}"
}