From 1b89315f88c57671f860c439098b1fe4d409d3fa Mon Sep 17 00:00:00 2001
From: dgelessus <dgelessus@users.noreply.github.com>
Date: Tue, 28 Apr 2020 14:31:59 +0200
Subject: [PATCH] Store version number in properties instead of generating
 Version class

---
 .gitignore                                    |  1 -
 build.gradle                                  | 28 ++++-----------
 .../java/org/sablecc/sablecc/Version.java     | 35 +++++++++++++++++++
 .../org/sablecc/sablecc/build.properties      |  3 ++
 4 files changed, 45 insertions(+), 22 deletions(-)
 create mode 100644 src/main/java/org/sablecc/sablecc/Version.java
 create mode 100644 src/main/resources/org/sablecc/sablecc/build.properties

diff --git a/.gitignore b/.gitignore
index 6aa4928..81007ca 100644
--- a/.gitignore
+++ b/.gitignore
@@ -17,4 +17,3 @@ PROB_LOGFILE_IS_UNDEFINED
 src/main/resources/build.properties
 config.groovy
 local.*
-Version.java
diff --git a/build.gradle b/build.gradle
index 2cf3b7f..5a89903 100644
--- a/build.gradle
+++ b/build.gradle
@@ -30,33 +30,19 @@ java {
   withJavadocJar()
 }
 
-jar {
-  manifest {
-    attributes 'Main-Class': mainClassName
+processResources {
+  inputs.property("project.version", project.version)
+  filesMatching("org/sablecc/sablecc/build.properties") {
+    expand(version: project.version)
   }
 }
 
-task writeVersion() {
-
-  doFirst {
-    def buildconstants_class = """
-package org.sablecc.sablecc;
-
-public class Version
-{
-  public static final String VERSION = "${project.version}";
-}
-"""
-    File f = file("src/main/java/org/sablecc/sablecc/Version.java")
-    f.delete()
-    f <<  buildconstants_class
+jar {
+  manifest {
+    attributes 'Main-Class': mainClassName
   }
 }
 
-compileJava {
-  dependsOn = ['writeVersion']
-}
-
 publishing {
   publications {
     mavenJava(MavenPublication) {
diff --git a/src/main/java/org/sablecc/sablecc/Version.java b/src/main/java/org/sablecc/sablecc/Version.java
new file mode 100644
index 0000000..2925b77
--- /dev/null
+++ b/src/main/java/org/sablecc/sablecc/Version.java
@@ -0,0 +1,35 @@
+
+package org.sablecc.sablecc;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.nio.charset.StandardCharsets;
+import java.util.Properties;
+
+public class Version
+{
+  public static final String VERSION;
+  static
+  {
+    final Properties buildProperties = new Properties();
+    final InputStream is = Version.class.getResourceAsStream("build.properties");
+    if(is == null)
+    {
+      throw new IllegalStateException("Build properties not found, this should never happen!");
+    }
+    else
+    {
+      try(final Reader r = new InputStreamReader(is, StandardCharsets.UTF_8))
+      {
+        buildProperties.load(r);
+      }
+      catch(final IOException e)
+      {
+        throw new IllegalStateException("IOException while loading build properties, this should never happen!", e);
+      }
+    }
+    VERSION = buildProperties.getProperty("version");
+  }
+}
diff --git a/src/main/resources/org/sablecc/sablecc/build.properties b/src/main/resources/org/sablecc/sablecc/build.properties
new file mode 100644
index 0000000..178150d
--- /dev/null
+++ b/src/main/resources/org/sablecc/sablecc/build.properties
@@ -0,0 +1,3 @@
+# The values for these properties are generated during the build.
+# See the configuration of the processResources task in build.gradle.
+version=${version}
-- 
GitLab