From a354227f79dacad1c5f8f6bade149affc1c02ee8 Mon Sep 17 00:00:00 2001
From: bclayton-usgs <bclayton@usgs.gov>
Date: Fri, 14 Aug 2020 09:44:36 -0600
Subject: [PATCH] jar makes thin; shadow for fat

---
 build.gradle        |  6 +-----
 gradle/jar.gradle   | 48 ++++++++++++++++++++++++++-------------------
 gradle/tasks.gradle |  7 -------
 3 files changed, 29 insertions(+), 32 deletions(-)

diff --git a/build.gradle b/build.gradle
index d6e1654df..19ed517ca 100644
--- a/build.gradle
+++ b/build.gradle
@@ -22,7 +22,7 @@
  * Then build the JAR file: ./gradlew assemble
  *
  * One can then specify the model to run in the services:
- *    java -jar build/libs/nshmp-haz-v2-all.jar -model=<MODEL>
+ *    java -jar build/libs/nshmp-haz-v2.jar -model=<MODEL>
  *
  * Where <MODEL> is one of the model enums in nshmp.www.Model
  */
@@ -121,10 +121,6 @@ jacocoTestReport {
 }
 check.dependsOn jacocoTestReport
 
-shadowJar {
-  mergeServiceFiles()
-}
-
 tasks.withType(JavaCompile) {
   options.encoding = "UTF-8"
   options.compilerArgs.add("-parameters")
diff --git a/gradle/jar.gradle b/gradle/jar.gradle
index 12b7c1815..be5167f45 100644
--- a/gradle/jar.gradle
+++ b/gradle/jar.gradle
@@ -1,28 +1,17 @@
+apply plugin: "com.github.johnrengelman.shadow"
+
 /*
- * The default jar task automatically builds a fat jar and adds
- * a properties file with the application version. Note that
- * 'git describe' only works when running gradle from the command
+ * Possibly record app version. The creation of this file
+ * on each build causes the classpath to appear changed so
+ * Gradle reruns tests, even though no code has changed.
+ *
+ * Note that 'git describe' only works when running gradle from the command
  * line so version values in eclipse builds will empty. Use thinJar
  * task to skip dependencies.
  */
-jar {
+tasks.register("propertyFile") {
+  description = "Create a property file with application version"
   doFirst {
-    /* possible fat jar */
-    if (rootProject.name == projectName && !thinJar) {
-      from {
-        configurations.compileClasspath.collect {
-          it.isDirectory() ? it : zipTree(it).matching {
-            exclude { it.path.contains('META-INF') }
-          }
-        }
-      }
-    }
-
-    /*
-     * Possibly record app version. The creation of this file
-     * on each build causes the classpath to appear changed so
-     * Gradle reruns tests, even though no code has changed.
-     */
     def props = new Properties()
     def propsFile = new File(project.buildDir.toString() + propsPath)
     if (propsFile.exists()) {
@@ -36,3 +25,22 @@ jar {
     }
   }
 }
+
+/*
+ * Build a thin jar and add a properties file with the application version.
+ */
+jar {
+  archiveBaseName = "${projectName}-thin"
+  dependsOn propertyFile
+}
+
+/*
+ * Build a fat jar to be used for running programs and Micronaut services.
+ */
+shadowJar {
+  dependsOn propertyFile
+  baseName = projectName
+  classifier = ''
+  archiveVersion = ''
+  mergeServiceFiles()
+}
diff --git a/gradle/tasks.gradle b/gradle/tasks.gradle
index 24480005a..5375745fc 100644
--- a/gradle/tasks.gradle
+++ b/gradle/tasks.gradle
@@ -1,11 +1,4 @@
 
-task thinJar(type: Jar) {
-  doFirst {
-    thinJar = true
-  }
-  with jar
-}
-
 /*
  * Create a zip file of all dependencies
  */
-- 
GitLab