This is an automated email from the ASF dual-hosted git repository.

slawrence pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/daffodil.git


The following commit(s) were added to refs/heads/main by this push:
     new 587bae447 Store Daffodil version in a file instead of in the manifest
587bae447 is described below

commit 587bae4474d746395c21be7070aa6e6063e84479
Author: Steve Lawrence <[email protected]>
AuthorDate: Fri Jun 2 07:51:27 2023 -0400

    Store Daffodil version in a file instead of in the manifest
    
    Daffodil has a Misc.getDaffodilVersion() function which gets the
    daffodil version from the package manifest. This is used in places like
    the CLI --version option, when writing the version to a saved parser, or
    checking the version when reloading a parser.
    
    But if Daffodil is assembled into a fat jar, that manifest file is
    usually replaced, which means the wrong version can be found when saving
    or reloading parsers.
    
    To avoid this, this modifies the SBT configuration to write the version
    to a resource file in org/apache/daffodil/lib/VERSION using a resource
    generator. The Misc.getDaffodilVersion() function is modified to read
    the contents of this resource to get the version. This no longer depends
    on the manifest file and uses a resource path that should never be
    discarded or overwritten when creating a fat jar.
    
    Also modifies the genManaged task to use the managedSources/Resources
    tasks so we don't need to update it if we add more source/resource
    generators. And remove the type since it isn't needed--this is just a
    convenience to generate all managed files.
    
    DAFFODIL-2816
---
 build.sbt                                          | 25 ++++++++++++++++++----
 .../scala/org/apache/daffodil/lib/util/Misc.scala  | 10 ++++-----
 2 files changed, 25 insertions(+), 10 deletions(-)

diff --git a/build.sbt b/build.sbt
index deaf5918c..ffc23ce65 100644
--- a/build.sbt
+++ b/build.sbt
@@ -19,10 +19,11 @@ import scala.collection.immutable.ListSet
 
 import sbtcc._
 
-lazy val genManaged = taskKey[Seq[File]]("Generate managed sources and 
resources")
+lazy val genManaged = taskKey[Unit]("Generate managed sources and resources")
 lazy val genProps = taskKey[Seq[File]]("Generate properties scala source")
 lazy val genSchemas = taskKey[Seq[File]]("Generate DFDL schemas")
 lazy val genCExamples = taskKey[Seq[File]]("Generate C example files")
+lazy val genVersion = taskKey[Seq[File]]("Generate VERSION file")
 
 lazy val daffodil = project
   .in(file("."))
@@ -328,7 +329,8 @@ lazy val usesMacros = Seq(
 
 lazy val libManagedSettings = Seq(
   genManaged := {
-    (Compile / genProps).value ++ (Compile / genSchemas).value
+    (Compile / managedSources).value
+    (Compile / managedResources).value
   },
   Compile / genProps := {
     val cp = (propgen / Runtime / dependencyClasspath).value
@@ -374,8 +376,23 @@ lazy val libManagedSettings = Seq(
       }
     cachedFun(filesToWatch).toSeq
   },
-  Compile / sourceGenerators += (Compile / genProps).taskValue,
-  Compile / resourceGenerators += (Compile / genSchemas).taskValue,
+  Compile / genVersion := {
+    val resourceDir = (Compile / resourceManaged).value
+    val outFile = resourceDir / "org" / "apache" / "daffodil" / "lib" / 
"VERSION"
+    if (!outFile.exists || IO.read(outFile) != version.value) {
+      // only write the VERSION file if the version hasn't changed. If we 
always write, then the
+      // mtime changes and sbt thinks it needs to rebuild everything since a 
resource changed.
+      IO.write(outFile, version.value)
+    }
+    Seq(outFile)
+  },
+  Compile / sourceGenerators ++= Seq(
+    (Compile / genProps).taskValue,
+  ),
+  Compile / resourceGenerators ++= Seq(
+    (Compile / genSchemas).taskValue,
+    (Compile / genVersion).taskValue,
+  ),
 )
 
 lazy val ratSettings = Seq(
diff --git 
a/daffodil-lib/src/main/scala/org/apache/daffodil/lib/util/Misc.scala 
b/daffodil-lib/src/main/scala/org/apache/daffodil/lib/util/Misc.scala
index d2ecda9d0..c5798e411 100644
--- a/daffodil-lib/src/main/scala/org/apache/daffodil/lib/util/Misc.scala
+++ b/daffodil-lib/src/main/scala/org/apache/daffodil/lib/util/Misc.scala
@@ -34,6 +34,7 @@ import java.nio.charset.{ Charset => JavaCharset }
 import java.nio.file.Files
 import java.nio.file.Paths
 import scala.collection.JavaConverters._
+import scala.io.Source
 
 import org.apache.daffodil.lib.equality._
 import org.apache.daffodil.lib.exceptions.Assert
@@ -250,12 +251,9 @@ object Misc {
    * Returns the primary version of daffodil from the jar
    */
   def getDaffodilVersion: String = {
-    val implVersion = this.getClass.getPackage.getImplementationVersion
-    if (implVersion == null) {
-      ""
-    } else {
-      implVersion
-    }
+    val uri = getRequiredResource("org/apache/daffodil/lib/VERSION")
+    val version = Source.fromInputStream(uri.toURL.openStream, 
"UTF-8").mkString
+    version
   }
 
   /**

Reply via email to