JAL-3577 Changed Windows internal ID to a new value which is backward compatible...
[jalview.git] / build.gradle
1 import org.apache.tools.ant.filters.ReplaceTokens
2 import org.gradle.internal.os.OperatingSystem
3 import org.gradle.plugins.ide.eclipse.model.Output
4 import org.gradle.plugins.ide.eclipse.model.Library
5 import java.security.MessageDigest
6 import groovy.transform.ExternalizeMethods
7 import groovy.util.XmlParser
8 import groovy.xml.XmlUtil
9
10
11 buildscript {
12   repositories {
13     mavenCentral()
14     mavenLocal()
15   }
16   dependencies {
17     classpath 'org.openclover:clover:4.4.1'
18   }
19 }
20
21
22 plugins {
23   id 'java'
24   id 'application'
25   id 'eclipse'
26   id 'com.github.johnrengelman.shadow' version '4.0.3'
27   id 'com.install4j.gradle' version '8.0.4'
28   id 'com.dorongold.task-tree' version '1.5' // only needed to display task dependency tree with  gradle task1 [task2 ...] taskTree
29 }
30
31 repositories {
32   jcenter()
33   mavenCentral()
34   mavenLocal()
35 }
36
37
38 // in ext the values are cast to Object. Ensure string values are cast as String (and not GStringImpl) for later use
39 def string(Object o) {
40   return o == null ? "" : o.toString()
41 }
42
43
44 ext {
45   jalviewDirAbsolutePath = file(jalviewDir).getAbsolutePath()
46   jalviewDirRelativePath = jalviewDir
47
48   // local build environment properties
49   // can be "projectDir/local.properties"
50   def localProps = "${projectDir}/local.properties"
51   def propsFile = null;
52   if (file(localProps).exists()) {
53     propsFile = localProps
54   }
55   // or "../projectDir_local.properties"
56   def dirLocalProps = projectDir.getParent() + "/" + projectDir.getName() + "_local.properties"
57   if (file(dirLocalProps).exists()) {
58     propsFile = dirLocalProps
59   }
60   if (propsFile != null) {
61     try {
62       def p = new Properties()
63       def localPropsFIS = new FileInputStream(propsFile)
64       p.load(localPropsFIS)
65       localPropsFIS.close()
66       p.each {
67         key, val -> 
68           def oldval = findProperty(key)
69           setProperty(key, val)
70           if (oldval != null) {
71             println("Overriding property '${key}' ('${oldval}') with ${file(propsFile).getName()} value '${val}'")
72           } else {
73             println("Setting unknown property '${key}' with ${file(propsFile).getName()}s value '${val}'")
74           }
75       }
76     } catch (Exception e) {
77       System.out.println("Exception reading local.properties")
78     }
79   }
80
81   // this property set when running Eclipse headlessly
82   j2sHeadlessBuildProperty = string("net.sf.j2s.core.headlessbuild")
83   // this property set by Eclipse
84   eclipseApplicationProperty = string("eclipse.application")
85   // CHECK IF RUNNING FROM WITHIN ECLIPSE
86   def eclipseApplicationPropertyVal = System.properties[eclipseApplicationProperty]
87   IN_ECLIPSE = eclipseApplicationPropertyVal != null && eclipseApplicationPropertyVal.startsWith("org.eclipse.ui.")
88   // BUT WITHOUT THE HEADLESS BUILD PROPERTY SET
89   if (System.properties[j2sHeadlessBuildProperty].equals("true")) {
90     println("Setting IN_ECLIPSE to ${IN_ECLIPSE} as System.properties['${j2sHeadlessBuildProperty}'] == '${System.properties[j2sHeadlessBuildProperty]}'")
91     IN_ECLIPSE = false
92   }
93   if (IN_ECLIPSE) {
94     println("WITHIN ECLIPSE IDE")
95   } else {
96     println("HEADLESS BUILD")
97   }
98   /* *-/
99   System.properties.sort { it.key }.each {
100     key, val -> println("SYSTEM PROPERTY ${key}='${val}'")
101   }
102   /-* *-/
103   if (false && IN_ECLIPSE) {
104     jalviewDir = jalviewDirAbsolutePath
105   }
106   */
107
108   // essentials
109   bareSourceDir = string(source_dir)
110   sourceDir = string("${jalviewDir}/${bareSourceDir}")
111   resourceDir = string("${jalviewDir}/${resource_dir}")
112   bareTestSourceDir = string(test_source_dir)
113   testSourceDir = string("${jalviewDir}/${bareTestSourceDir}")
114
115   // clover
116   cloverInstrDir = file("${buildDir}/${cloverSourcesInstrDir}")
117   cloverDb = string("${buildDir}/clover/clover.db")
118   classesDir = string("${jalviewDir}/${classes_dir}")
119   if (clover.equals("true")) {
120     use_clover = true
121     classesDir = string("${buildDir}/${cloverClassesDir}")
122   } else {
123     use_clover = false
124     classesDir = string("${jalviewDir}/${classes_dir}")
125   }
126
127   classes = classesDir
128
129   getdownWebsiteDir = string("${jalviewDir}/${getdown_website_dir}/${JAVA_VERSION}")
130   buildDist = true
131
132   // the following values might be overridden by the CHANNEL switch
133   getdownChannelName = CHANNEL.toLowerCase()
134   getdownDir = string("${getdownChannelName}/${JAVA_VERSION}")
135   getdownAppBase = string("${getdown_channel_base}/${getdownDir}")
136   getdownLauncher = string("${jalviewDir}/${getdown_lib_dir}/${getdown_launcher}")
137   getdownAppDistDir = getdown_app_dir_alt
138   buildProperties = string("${classesDir}/${build_properties_file}")
139   reportRsyncCommand = false
140   jvlChannelName = CHANNEL.toLowerCase()
141   install4jSuffix = CHANNEL.substring(0, 1).toUpperCase() + CHANNEL.substring(1).toLowerCase(); // BUILD -> Build
142   install4jDSStore = "DS_Store-NON-RELEASE"
143   install4jDMGBackgroundImage = "jalview_dmg_background-NON-RELEASE.png"
144   install4jInstallerName = "${jalview_name} Non-Release Installer"
145   install4jExecutableName = jalview_name.replaceAll("[^\\w]+", "_").toLowerCase()
146   install4jExtraScheme = "jalviewx"
147   switch (CHANNEL) {
148
149     case "BUILD":
150     // TODO: get bamboo build artifact URL for getdown artifacts
151     getdown_channel_base = bamboo_channelbase
152     getdownChannelName = string("${bamboo_planKey}/${JAVA_VERSION}")
153     getdownAppBase = string("${bamboo_channelbase}/${bamboo_planKey}${bamboo_getdown_channel_suffix}/${JAVA_VERSION}")
154     jvlChannelName += "_${getdownChannelName}"
155     // automatically add the test group Not-bamboo for exclusion 
156     if ("".equals(testngExcludedGroups)) { 
157       testngExcludedGroups = "Not-bamboo"
158     }
159     install4jExtraScheme = "jalviewb"
160     break
161
162     case "RELEASE":
163     getdownAppDistDir = getdown_app_dir_release
164     reportRsyncCommand = true
165     install4jSuffix = ""
166     install4jDSStore = "DS_Store"
167     install4jDMGBackgroundImage = "jalview_dmg_background.png"
168     install4jInstallerName = "${jalview_name} Installer"
169     break
170
171     case "ARCHIVE":
172     getdownChannelName = CHANNEL.toLowerCase()+"/${JALVIEW_VERSION}"
173     getdownDir = string("${getdownChannelName}/${JAVA_VERSION}")
174     getdownAppBase = string("${getdown_channel_base}/${getdownDir}")
175     if (!file("${ARCHIVEDIR}/${packageDir}").exists()) {
176       throw new GradleException("Must provide an ARCHIVEDIR value to produce an archive distribution")
177     } else {
178       packageDir = string("${ARCHIVEDIR}/${packageDir}")
179       buildProperties = string("${ARCHIVEDIR}/${classes_dir}/${build_properties_file}")
180       buildDist = false
181     }
182     reportRsyncCommand = true
183     install4jExtraScheme = "jalviewa"
184     break
185
186     case "ARCHIVELOCAL":
187     getdownChannelName = string("archive/${JALVIEW_VERSION}")
188     getdownDir = string("${getdownChannelName}/${JAVA_VERSION}")
189     getdownAppBase = file(getdownWebsiteDir).toURI().toString()
190     if (!file("${ARCHIVEDIR}/${packageDir}").exists()) {
191       throw new GradleException("Must provide an ARCHIVEDIR value to produce an archive distribution")
192     } else {
193       packageDir = string("${ARCHIVEDIR}/${packageDir}")
194       buildProperties = string("${ARCHIVEDIR}/${classes_dir}/${build_properties_file}")
195       buildDist = false
196     }
197     reportRsyncCommand = true
198     getdownLauncher = string("${jalviewDir}/${getdown_lib_dir}/${getdown_launcher_local}")
199     install4jSuffix = "Archive"
200     install4jExtraScheme = "jalviewa"
201     break
202
203     case "DEVELOP":
204     reportRsyncCommand = true
205     install4jExtraScheme = "jalviewd"
206     break
207
208     case "TEST-RELEASE":
209     reportRsyncCommand = true
210     JALVIEW_VERSION = "TEST"
211     install4jSuffix = "Test"
212     install4jDSStore = "DS_Store-TEST-RELEASE"
213     install4jDMGBackgroundImage = "jalview_dmg_background.png"
214     install4jExtraScheme = "jalviewt"
215     install4jInstallerName = "${jalview_name} Test Installer"
216     break
217
218     case ~/^SCRATCH(|-[-\w]*)$/:
219     getdownChannelName = CHANNEL
220     getdownDir = string("${getdownChannelName}/${JAVA_VERSION}")
221     getdownAppBase = string("${getdown_channel_base}/${getdownDir}")
222     reportRsyncCommand = true
223     install4jSuffix = "Scratch"
224     break
225
226     case "TEST-LOCAL":
227     if (!file("${LOCALDIR}").exists()) {
228       throw new GradleException("Must provide a LOCALDIR value to produce a local distribution")
229     } else {
230       getdownAppBase = file(file("${LOCALDIR}").getAbsolutePath()).toURI().toString()
231       getdownLauncher = string("${jalviewDir}/${getdown_lib_dir}/${getdown_launcher_local}")
232     }
233     JALVIEW_VERSION = "TEST"
234     install4jSuffix = "Test-Local"
235     install4jDSStore = "DS_Store-TEST-RELEASE"
236     install4jDMGBackgroundImage = "jalview_dmg_background.png"
237     install4jExtraScheme = "jalviewt"
238     install4jInstallerName = "${jalview_name} Test Installer"
239     break
240
241     case "LOCAL":
242     getdownAppBase = file(getdownWebsiteDir).toURI().toString()
243     getdownLauncher = string("${jalviewDir}/${getdown_lib_dir}/${getdown_launcher_local}")
244     install4jExtraScheme = "jalviewl"
245     break
246
247     default: // something wrong specified
248     throw new GradleException("CHANNEL must be one of BUILD, RELEASE, ARCHIVE, DEVELOP, TEST-RELEASE, SCRATCH-..., LOCAL [default]")
249     break
250
251   }
252   // override getdownAppBase if requested
253   if (findProperty("getdown_appbase_override") != null) {
254     getdownAppBase = string(getProperty("getdown_appbase_override"))
255     println("Overriding getdown appbase with '${getdownAppBase}'")
256   }
257   // sanitise file name for jalview launcher file for this channel
258   jvlChannelName = jvlChannelName.replaceAll("[^\\w\\-]+", "_")
259   // install4j application and folder names
260   if (install4jSuffix == "") {
261     install4jApplicationName = "${jalview_name}"
262     install4jBundleId = "${install4j_bundle_id}"
263   } else {
264     install4jApplicationName = "${jalview_name} ${install4jSuffix}"
265     install4jBundleId = "${install4j_bundle_id}-" + install4jSuffix.toLowerCase()
266   }
267   // sanitise folder and id names
268   // install4jApplicationFolder = e.g. "Jalview Build"
269   install4jApplicationFolder = install4jApplicationName
270                                     .replaceAll("[\"'~:/\\\\]", "_") // replace all awkward filename chars " ' ~ : / \
271                                     .replaceAll("_+", "_") // collapse __
272   install4jInternalId = install4jApplicationFolder
273                                     .replaceAll(" ","_")
274                                     .replaceAll("[^\\w\\-\\.]", "_") // replace other non [alphanumeric,_,-,.]
275                                     .replaceAll("_+", "_") // collapse __
276                                     .replaceAll("_*-_*", "-") // collapse _-_
277   install4jUnixApplicationFolder = install4jInternalId
278                                     .toLowerCase()
279
280   getdownAppDir = string("${getdownWebsiteDir}/${getdownAppDistDir}")
281   //getdownJ11libDir = "${getdownWebsiteDir}/${getdown_j11lib_dir}"
282   getdownResourceDir = string("${getdownWebsiteDir}/${getdown_resource_dir}")
283   getdownInstallDir = string("${getdownWebsiteDir}/${getdown_install_dir}")
284   getdownFilesDir = string("${jalviewDir}/${getdown_files_dir}/${JAVA_VERSION}/")
285   getdownFilesInstallDir = string("${getdownFilesDir}/${getdown_install_dir}")
286   /* compile without modules -- using classpath libraries
287   modules_compileClasspath = fileTree(dir: "${jalviewDir}/${j11modDir}", include: ["*.jar"])
288   modules_runtimeClasspath = modules_compileClasspath
289   */
290   gitHash = string("")
291   gitBranch = string("")
292
293   println("Using a ${CHANNEL} profile.")
294
295   additional_compiler_args = []
296   // configure classpath/args for j8/j11 compilation
297   if (JAVA_VERSION.equals("1.8")) {
298     JAVA_INTEGER_VERSION = string("8")
299     //libDir = j8libDir
300     libDir = j11libDir
301     libDistDir = j8libDir
302     compile_source_compatibility = 1.8
303     compile_target_compatibility = 1.8
304     // these are getdown.txt properties defined dependent on the JAVA_VERSION
305     getdownAltJavaMinVersion = string(findProperty("getdown_alt_java8_min_version"))
306     getdownAltJavaMaxVersion = string(findProperty("getdown_alt_java8_max_version"))
307     // this property is assigned below and expanded to multiple lines in the getdown task
308     getdownAltMultiJavaLocation = string(findProperty("getdown_alt_java8_txt_multi_java_location"))
309     // this property is for the Java library used in eclipse
310     eclipseJavaRuntimeName = string("JavaSE-1.8")
311   } else if (JAVA_VERSION.equals("11")) {
312     JAVA_INTEGER_VERSION = string("11")
313     libDir = j11libDir
314     libDistDir = j11libDir
315     compile_source_compatibility = 11
316     compile_target_compatibility = 11
317     getdownAltJavaMinVersion = string(findProperty("getdown_alt_java11_min_version"))
318     getdownAltJavaMaxVersion = string(findProperty("getdown_alt_java11_max_version"))
319     getdownAltMultiJavaLocation = string(findProperty("getdown_alt_java11_txt_multi_java_location"))
320     eclipseJavaRuntimeName = string("JavaSE-11")
321     /* compile without modules -- using classpath libraries
322     additional_compiler_args += [
323     '--module-path', modules_compileClasspath.asPath,
324     '--add-modules', j11modules
325     ]
326      */
327   } else if (JAVA_VERSION.equals("12") || JAVA_VERSION.equals("13")) {
328     JAVA_INTEGER_VERSION = JAVA_VERSION
329     libDir = j11libDir
330     libDistDir = j11libDir
331     compile_source_compatibility = JAVA_VERSION
332     compile_target_compatibility = JAVA_VERSION
333     getdownAltJavaMinVersion = string(findProperty("getdown_alt_java11_min_version"))
334     getdownAltJavaMaxVersion = string(findProperty("getdown_alt_java11_max_version"))
335     getdownAltMultiJavaLocation = string(findProperty("getdown_alt_java11_txt_multi_java_location"))
336     eclipseJavaRuntimeName = string("JavaSE-11")
337     /* compile without modules -- using classpath libraries
338     additional_compiler_args += [
339     '--module-path', modules_compileClasspath.asPath,
340     '--add-modules', j11modules
341     ]
342      */
343   } else {
344     throw new GradleException("JAVA_VERSION=${JAVA_VERSION} not currently supported by Jalview")
345   }
346
347
348   // for install4j
349   JAVA_MIN_VERSION = JAVA_VERSION
350   JAVA_MAX_VERSION = JAVA_VERSION
351   def jreInstallsDir = string(jre_installs_dir)
352   if (jreInstallsDir.startsWith("~/")) {
353     jreInstallsDir = System.getProperty("user.home") + jreInstallsDir.substring(1)
354   }
355   macosJavaVMDir = string("${jreInstallsDir}/jre-${JAVA_INTEGER_VERSION}-mac-x64/jre")
356   macosJavaVMTgz = string("${jreInstallsDir}/tgz/jre-${JAVA_INTEGER_VERSION}-mac-x64.tar.gz")
357   windowsJavaVMDir = string("${jreInstallsDir}/jre-${JAVA_INTEGER_VERSION}-windows-x64/jre")
358   windowsJavaVMTgz = string("${jreInstallsDir}/tgz/jre-${JAVA_INTEGER_VERSION}-windows-x64.tar.gz")
359   linuxJavaVMDir = string("${jreInstallsDir}/jre-${JAVA_INTEGER_VERSION}-linux-x64/jre")
360   linuxJavaVMTgz = string("${jreInstallsDir}/tgz/jre-${JAVA_INTEGER_VERSION}-linux-x64.tar.gz")
361   install4jDir = string("${jalviewDir}/${install4j_utils_dir}")
362   install4jConfFileName = string("jalview-install4j-conf.install4j")
363   install4jConfFile = file("${install4jDir}/${install4jConfFileName}")
364   install4jHomeDir = install4j_home_dir
365   if (install4jHomeDir.startsWith("~/")) {
366     install4jHomeDir = System.getProperty("user.home") + install4jHomeDir.substring(1)
367   }
368
369
370
371   buildingHTML = string("${jalviewDir}/${docDir}/building.html")
372   helpFile = string("${classesDir}/${help_dir}/help.jhm")
373   helpParentDir = string("${jalviewDir}/${help_parent_dir}")
374   helpSourceDir = string("${helpParentDir}/${help_dir}")
375
376
377   relativeBuildDir = file(jalviewDirAbsolutePath).toPath().relativize(buildDir.toPath())
378   jalviewjsBuildDir = string("${relativeBuildDir}/jalviewjs")
379   jalviewjsSiteDir = string("${jalviewjsBuildDir}/${jalviewjs_site_dir}")
380   if (IN_ECLIPSE) {
381     jalviewjsTransferSiteJsDir = string(jalviewjsSiteDir)
382   } else {
383     jalviewjsTransferSiteJsDir = string("${jalviewjsBuildDir}/tmp/${jalviewjs_site_dir}_js")
384   }
385   jalviewjsTransferSiteLibDir = string("${jalviewjsBuildDir}/tmp/${jalviewjs_site_dir}_lib")
386   jalviewjsTransferSiteSwingJsDir = string("${jalviewjsBuildDir}/tmp/${jalviewjs_site_dir}_swingjs")
387   jalviewjsTransferSiteCoreDir = string("${jalviewjsBuildDir}/tmp/${jalviewjs_site_dir}_core")
388   jalviewjsJalviewCoreHtmlFile = string("")
389   jalviewjsJalviewCoreName = string(jalviewjs_core_name)
390   jalviewjsCoreClasslists = []
391   jalviewjsJalviewTemplateName = string(jalviewjs_name)
392   jalviewjsJ2sSettingsFileName = string("${jalviewDir}/${jalviewjs_j2s_settings}")
393   jalviewjsJ2sProps = null
394
395   eclipseWorkspace = null
396   eclipseBinary = string("")
397   eclipseVersion = string("")
398   eclipseDebug = false
399   // ENDEXT
400 }
401
402
403 sourceSets {
404   main {
405     java {
406       srcDirs sourceDir
407       outputDir = file(classesDir)
408     }
409
410     resources {
411       srcDirs resourceDir
412       srcDirs += helpParentDir
413     }
414
415     jar.destinationDir = file("${jalviewDir}/${packageDir}")
416
417     compileClasspath = files(sourceSets.main.java.outputDir)
418     //compileClasspath += files(sourceSets.main.resources.srcDirs)
419     compileClasspath += fileTree(dir: "${jalviewDir}/${libDir}", include: ["*.jar"])
420
421     runtimeClasspath = compileClasspath
422   }
423
424   clover {
425     java {
426       srcDirs = [ cloverInstrDir ]
427       outputDir = file("${buildDir}/${cloverClassesDir}")
428     }
429
430     resources {
431       srcDirs = sourceSets.main.resources.srcDirs
432     }
433     compileClasspath = configurations.cloverRuntime + files( sourceSets.clover.java.outputDir )
434     compileClasspath += files(sourceSets.main.java.outputDir)
435     compileClasspath += sourceSets.main.compileClasspath
436     compileClasspath += fileTree(dir: "${jalviewDir}/${utilsDir}", include: ["**/*.jar"])
437     compileClasspath += fileTree(dir: "${jalviewDir}/${libDir}", include: ["*.jar"])
438
439     runtimeClasspath = compileClasspath
440   }
441
442   test {
443     java {
444       srcDirs testSourceDir
445       outputDir = file("${jalviewDir}/${testOutputDir}")
446     }
447
448     resources {
449       srcDirs = sourceSets.main.resources.srcDirs
450     }
451
452     compileClasspath = files( sourceSets.test.java.outputDir )
453
454     if (use_clover) {
455       compileClasspath = sourceSets.clover.compileClasspath
456     } else {
457       compileClasspath += files(sourceSets.main.java.outputDir)
458     }
459
460     compileClasspath += fileTree(dir: "${jalviewDir}/${libDir}", include: ["*.jar"])
461     compileClasspath += fileTree(dir: "${jalviewDir}/${utilsDir}/testnglibs", include: ["**/*.jar"])
462     compileClasspath += fileTree(dir: "${jalviewDir}/${utilsDir}/testlibs", include: ["**/*.jar"])
463
464     runtimeClasspath = compileClasspath
465   }
466 }
467
468
469 // clover bits
470 dependencies {
471   if (use_clover) {
472     cloverCompile 'org.openclover:clover:4.4.1'
473     testCompile 'org.openclover:clover:4.4.1'
474   }
475 }
476
477 configurations {
478   cloverRuntime
479   cloverRuntime.extendsFrom cloverCompile
480 }
481
482
483 // eclipse project and settings files creation, also used by buildship
484 eclipse {
485   project {
486     name = eclipse_project_name
487
488     natures 'org.eclipse.jdt.core.javanature',
489     'org.eclipse.jdt.groovy.core.groovyNature',
490     'org.eclipse.buildship.core.gradleprojectnature'
491
492     buildCommand 'org.eclipse.jdt.core.javabuilder'
493     buildCommand 'org.eclipse.buildship.core.gradleprojectbuilder'
494   }
495
496   classpath {
497     //defaultOutputDir = sourceSets.main.java.outputDir
498     def removeThese = []
499     configurations.each{
500       if (it.isCanBeResolved()) {
501         removeThese += it
502       }
503     }
504
505     minusConfigurations += removeThese
506     plusConfigurations = [ ]
507     file {
508
509       whenMerged { cp ->
510         def removeTheseToo = []
511         HashMap<String, Boolean> alreadyAddedSrcPath = new HashMap<>();
512         cp.entries.each { entry ->
513           // This conditional removes all src classpathentries that a) have already been added or b) aren't "src" or "test".
514           // e.g. this removes the resources dir being copied into bin/main, bin/test AND bin/clover
515           // we add the resources and help/help dirs in as libs afterwards (see below)
516           if (entry.kind == 'src') {
517             if (alreadyAddedSrcPath.getAt(entry.path) || !(entry.path == bareSourceDir || entry.path == bareTestSourceDir)) {
518               removeTheseToo += entry
519             } else {
520               alreadyAddedSrcPath.putAt(entry.path, true)
521             }
522           }
523
524         }
525         cp.entries.removeAll(removeTheseToo)
526
527         //cp.entries += new Output("${eclipse_bin_dir}/main")
528         if (file(helpParentDir).isDirectory()) {
529           cp.entries += new Library(fileReference(helpParentDir))
530         }
531         if (file(resourceDir).isDirectory()) {
532           cp.entries += new Library(fileReference(resourceDir))
533         }
534
535         HashMap<String, Boolean> alreadyAddedLibPath = new HashMap<>();
536
537         sourceSets.main.compileClasspath.findAll { it.name.endsWith(".jar") }.any {
538           //don't want to add outputDir as eclipse is using its own output dir in bin/main
539           if (it.isDirectory() || ! it.exists()) {
540             // don't add dirs to classpath, especially if they don't exist
541             return false // groovy "continue" in .any closure
542           }
543           def itPath = it.toString()
544           if (itPath.startsWith("${jalviewDirAbsolutePath}/")) {
545             // make relative path
546             itPath = itPath.substring(jalviewDirAbsolutePath.length()+1)
547           }
548           if (alreadyAddedLibPath.get(itPath)) {
549             //println("Not adding duplicate entry "+itPath)
550           } else {
551             //println("Adding entry "+itPath)
552             cp.entries += new Library(fileReference(itPath))
553             alreadyAddedLibPath.put(itPath, true)
554           }
555         }
556
557         sourceSets.test.compileClasspath.findAll { it.name.endsWith(".jar") }.any {
558           //no longer want to add outputDir as eclipse is using its own output dir in bin/main
559           if (it.isDirectory() || ! it.exists()) {
560             // don't add dirs to classpath
561             return false // groovy "continue" in .any closure
562           }
563
564           def itPath = it.toString()
565           if (itPath.startsWith("${jalviewDirAbsolutePath}/")) {
566             itPath = itPath.substring(jalviewDirAbsolutePath.length()+1)
567           }
568           if (alreadyAddedLibPath.get(itPath)) {
569             // don't duplicate
570           } else {
571             def lib = new Library(fileReference(itPath))
572             lib.entryAttributes["test"] = "true"
573             cp.entries += lib
574             alreadyAddedLibPath.put(itPath, true)
575           }
576         }
577
578       } // whenMerged
579
580     } // file
581
582     containers 'org.eclipse.buildship.core.gradleclasspathcontainer'
583
584   } // classpath
585
586   jdt {
587     // for the IDE, use java 11 compatibility
588     sourceCompatibility = compile_source_compatibility
589     targetCompatibility = compile_target_compatibility
590     javaRuntimeName = eclipseJavaRuntimeName
591
592     // add in jalview project specific properties/preferences into eclipse core preferences
593     file {
594       withProperties { props ->
595         def jalview_prefs = new Properties()
596         def ins = new FileInputStream("${jalviewDirAbsolutePath}/${eclipse_extra_jdt_prefs_file}")
597         jalview_prefs.load(ins)
598         ins.close()
599         jalview_prefs.forEach { t, v ->
600           if (props.getAt(t) == null) {
601             props.putAt(t, v)
602           }
603         }
604       }
605     }
606
607   } // jdt
608
609   if (IN_ECLIPSE) {
610     // Don't want these to be activated if in headless build
611     synchronizationTasks "eclipseSynchronizationTask"
612     autoBuildTasks "eclipseAutoBuildTask"
613
614   }
615 }
616
617
618 task cloverInstr {
619   // only instrument source, we build test classes as normal
620   inputs.files files (sourceSets.main.allJava,sourceSets.test.allJava) // , fileTree(dir:"$jalviewDir/$testSourceDir", include: ["**/*.java"]))
621   outputs.dir cloverInstrDir
622
623   doFirst {
624     delete cloverInstrDir
625     def argsList = [
626       "--initstring",
627       cloverDb,
628       "-d",
629       cloverInstrDir.getPath(),
630     ]
631     argsList.addAll(
632       inputs.files.files.collect(
633         { file -> file.absolutePath }
634       )
635     )
636     String[] args = argsList.toArray()
637     println("About to instrument "+args.length +" files")
638     com.atlassian.clover.CloverInstr.mainImpl(args)
639   }
640 }
641
642
643 cloverClasses.dependsOn cloverInstr
644
645
646 task cloverReport {
647   group = "Verification"
648   description = "Creates the Clover report"
649   inputs.dir "${buildDir}/clover"
650   outputs.dir "${reportsDir}/clover"
651   onlyIf {
652     file(cloverDb).exists()
653   }
654   doFirst {
655     def argsList = [
656       "--initstring",
657       cloverDb,
658       "-o",
659       "${reportsDir}/clover"
660     ]
661     String[] args = argsList.toArray()
662     com.atlassian.clover.reporters.html.HtmlReporter.runReport(args)
663
664     // and generate ${reportsDir}/clover/clover.xml
665     args = [
666       "--initstring",
667       cloverDb,
668       "-o",
669       "${reportsDir}/clover/clover.xml"
670     ].toArray()
671     com.atlassian.clover.reporters.xml.XMLReporter.runReport(args)
672   }
673 }
674
675
676 compileCloverJava {
677
678   doFirst {
679     sourceCompatibility = compile_source_compatibility
680     targetCompatibility = compile_target_compatibility
681     options.compilerArgs += additional_compiler_args
682     print ("Setting target compatibility to "+targetCompatibility+"\n")
683   }
684   classpath += configurations.cloverRuntime
685 }
686
687
688 task cleanClover {
689   doFirst {
690     delete cloverInstrDir
691     delete cloverDb
692   }
693 }
694 // end clover bits
695
696
697 compileJava {
698
699   doFirst {
700     sourceCompatibility = compile_source_compatibility
701     targetCompatibility = compile_target_compatibility
702     options.compilerArgs = additional_compiler_args
703     print ("Setting target compatibility to "+targetCompatibility+"\n")
704   }
705
706 }
707
708
709 compileTestJava {
710   if (use_clover) {
711     dependsOn compileCloverJava
712     classpath += configurations.cloverRuntime
713   } else {
714     classpath += sourceSets.main.runtimeClasspath
715   }
716   doFirst {
717     sourceCompatibility = compile_source_compatibility
718     targetCompatibility = compile_target_compatibility
719     options.compilerArgs = additional_compiler_args
720     print ("Setting target compatibility to "+targetCompatibility+"\n")
721   }
722 }
723
724
725 clean {
726   doFirst {
727     delete sourceSets.main.java.outputDir
728   }
729 }
730
731
732 cleanTest {
733   dependsOn cleanClover
734   doFirst {
735     delete sourceSets.test.java.outputDir
736   }
737 }
738
739
740 // format is a string like date.format("dd MMMM yyyy")
741 def getDate(format) {
742   def date = new Date()
743   return date.format(format)
744 }
745
746
747 task setGitVals {
748   def hashStdOut = new ByteArrayOutputStream()
749   exec {
750     commandLine "git", "rev-parse", "--short", "HEAD"
751     standardOutput = hashStdOut
752     ignoreExitValue true
753   }
754
755   def branchStdOut = new ByteArrayOutputStream()
756   exec {
757     commandLine "git", "rev-parse", "--abbrev-ref", "HEAD"
758     standardOutput = branchStdOut
759     ignoreExitValue true
760   }
761
762   gitHash = hashStdOut.toString().trim()
763   gitBranch = branchStdOut.toString().trim()
764
765   outputs.upToDateWhen { false }
766 }
767
768
769 task createBuildProperties(type: WriteProperties) {
770   dependsOn setGitVals
771   inputs.dir(sourceDir)
772   inputs.dir(resourceDir)
773   file(buildProperties).getParentFile().mkdirs()
774   outputFile (buildProperties)
775   // taking time specific comment out to allow better incremental builds
776   comment "--Jalview Build Details--\n"+getDate("yyyy-MM-dd HH:mm:ss")
777   //comment "--Jalview Build Details--\n"+getDate("yyyy-MM-dd")
778   property "BUILD_DATE", getDate("HH:mm:ss dd MMMM yyyy")
779   property "VERSION", JALVIEW_VERSION
780   property "INSTALLATION", INSTALLATION+" git-commit:"+gitHash+" ["+gitBranch+"]"
781   outputs.file(outputFile)
782 }
783
784
785 task cleanBuildingHTML(type: Delete) {
786   doFirst {
787     delete buildingHTML
788   }
789 }
790
791
792 task convertBuildingMD(type: Exec) {
793   dependsOn cleanBuildingHTML
794   def buildingMD = "${jalviewDir}/${docDir}/building.md"
795   def css = "${jalviewDir}/${docDir}/github.css"
796
797   def pandoc = null
798   pandoc_exec.split(",").each {
799     if (file(it.trim()).exists()) {
800       pandoc = it.trim()
801       return true
802     }
803   }
804
805   def hostname = "hostname".execute().text.trim()
806   def buildtoolsPandoc = System.getProperty("user.home")+"/buildtools/pandoc/bin/pandoc"
807   if ((pandoc == null || ! file(pandoc).exists()) && file(buildtoolsPandoc).exists()) {
808     pandoc = System.getProperty("user.home")+"/buildtools/pandoc/bin/pandoc"
809   }
810
811   doFirst {
812     if (pandoc != null && file(pandoc).exists()) {
813         commandLine pandoc, '-s', '-o', buildingHTML, '--metadata', 'pagetitle="Building Jalview from Source"', '--toc', '-H', css, buildingMD
814     } else {
815         println("Cannot find pandoc. Skipping convert building.md to HTML")
816         throw new StopExecutionException("Cannot find pandoc. Skipping convert building.md to HTML")
817     }
818   }
819
820   ignoreExitValue true
821
822   inputs.file(buildingMD)
823   inputs.file(css)
824   outputs.file(buildingHTML)
825 }
826
827
828 clean {
829   doFirst {
830     delete buildingHTML
831   }
832 }
833
834
835 task syncDocs(type: Sync) {
836   dependsOn convertBuildingMD
837   def syncDir = "${classesDir}/${docDir}"
838   from fileTree("${jalviewDir}/${docDir}")
839   into syncDir
840
841 }
842
843
844 task copyHelp(type: Copy) {
845   def inputDir = helpSourceDir
846   def outputDir = "${classesDir}/${help_dir}"
847   from(inputDir) {
848     exclude '**/*.gif'
849     exclude '**/*.jpg'
850     exclude '**/*.png'
851     filter(ReplaceTokens,
852       beginToken: '$$',
853       endToken: '$$',
854       tokens: [
855         'Version-Rel': JALVIEW_VERSION,
856         'Year-Rel': getDate("yyyy")
857       ]
858     )
859   }
860   from(inputDir) {
861     include '**/*.gif'
862     include '**/*.jpg'
863     include '**/*.png'
864   }
865   into outputDir
866
867   inputs.dir(inputDir)
868   outputs.files(helpFile)
869   outputs.dir(outputDir)
870 }
871
872
873 task syncLib(type: Sync) {
874   def syncDir = "${classesDir}/${libDistDir}"
875   from fileTree("${jalviewDir}/${libDistDir}")
876   into syncDir
877 }
878
879
880 task syncResources(type: Sync) {
881   from resourceDir
882   include "**/*.*"
883   into "${classesDir}"
884   preserve {
885     include "**"
886   }
887 }
888
889
890 task prepare {
891   dependsOn syncResources
892   dependsOn syncDocs
893   dependsOn copyHelp
894 }
895
896
897 //testReportDirName = "test-reports" // note that test workingDir will be $jalviewDir
898 test {
899   dependsOn prepare
900   dependsOn compileJava
901   if (use_clover) {
902     dependsOn cloverInstr
903   }
904
905   if (use_clover) {
906     print("Running tests " + (use_clover?"WITH":"WITHOUT") + " clover [clover="+use_clover+"]\n")
907   }
908
909   useTestNG() {
910     includeGroups testngGroups
911     excludeGroups testngExcludedGroups
912     preserveOrder true
913     useDefaultListeners=true
914   }
915
916   maxHeapSize = "1024m"
917
918   workingDir = jalviewDir
919   //systemProperties 'clover.jar' System.properties.clover.jar
920   sourceCompatibility = compile_source_compatibility
921   targetCompatibility = compile_target_compatibility
922   jvmArgs += additional_compiler_args
923
924 }
925
926
927 task buildIndices(type: JavaExec) {
928   dependsOn copyHelp
929   classpath = sourceSets.main.compileClasspath
930   main = "com.sun.java.help.search.Indexer"
931   workingDir = "${classesDir}/${help_dir}"
932   def argDir = "html"
933   args = [ argDir ]
934   inputs.dir("${workingDir}/${argDir}")
935
936   outputs.dir("${classesDir}/doc")
937   outputs.dir("${classesDir}/help")
938   outputs.file("${workingDir}/JavaHelpSearch/DOCS")
939   outputs.file("${workingDir}/JavaHelpSearch/DOCS.TAB")
940   outputs.file("${workingDir}/JavaHelpSearch/OFFSETS")
941   outputs.file("${workingDir}/JavaHelpSearch/POSITIONS")
942   outputs.file("${workingDir}/JavaHelpSearch/SCHEMA")
943   outputs.file("${workingDir}/JavaHelpSearch/TMAP")
944 }
945
946
947 task compileLinkCheck(type: JavaCompile) {
948   options.fork = true
949   classpath = files("${jalviewDir}/${utilsDir}")
950   destinationDir = file("${jalviewDir}/${utilsDir}")
951   source = fileTree(dir: "${jalviewDir}/${utilsDir}", include: ["HelpLinksChecker.java", "BufferedLineReader.java"])
952
953   inputs.file("${jalviewDir}/${utilsDir}/HelpLinksChecker.java")
954   inputs.file("${jalviewDir}/${utilsDir}/HelpLinksChecker.java")
955   outputs.file("${jalviewDir}/${utilsDir}/HelpLinksChecker.class")
956   outputs.file("${jalviewDir}/${utilsDir}/BufferedLineReader.class")
957 }
958
959
960 task linkCheck(type: JavaExec) {
961   dependsOn prepare, compileLinkCheck
962
963   def helpLinksCheckerOutFile = file("${jalviewDir}/${utilsDir}/HelpLinksChecker.out")
964   classpath = files("${jalviewDir}/${utilsDir}")
965   main = "HelpLinksChecker"
966   workingDir = jalviewDir
967   args = [ "${classesDir}/${help_dir}", "-nointernet" ]
968
969   def outFOS = new FileOutputStream(helpLinksCheckerOutFile, false) // false == don't append
970   def errFOS = outFOS
971   standardOutput = new org.apache.tools.ant.util.TeeOutputStream(
972     outFOS,
973     standardOutput)
974   errorOutput = new org.apache.tools.ant.util.TeeOutputStream(
975     outFOS,
976     errorOutput)
977
978   inputs.dir("${classesDir}/${help_dir}")
979   outputs.file(helpLinksCheckerOutFile)
980 }
981
982 // import the pubhtmlhelp target
983 ant.properties.basedir = "${jalviewDir}"
984 ant.properties.helpBuildDir = "${jalviewDirAbsolutePath}/${classes_dir}/${help_dir}"
985 ant.importBuild "${utilsDir}/publishHelp.xml"
986
987
988 task cleanPackageDir(type: Delete) {
989   doFirst {
990     delete fileTree(dir: "${jalviewDir}/${packageDir}", include: "*.jar")
991   }
992 }
993
994 jar {
995   dependsOn linkCheck
996   dependsOn buildIndices
997   dependsOn createBuildProperties
998
999   manifest {
1000     attributes "Main-Class": mainClass,
1001     "Permissions": "all-permissions",
1002     "Application-Name": "Jalview Desktop",
1003     "Codebase": application_codebase
1004   }
1005
1006   destinationDir = file("${jalviewDir}/${packageDir}")
1007   archiveName = rootProject.name+".jar"
1008
1009   exclude "cache*/**"
1010   exclude "*.jar"
1011   exclude "*.jar.*"
1012   exclude "**/*.jar"
1013   exclude "**/*.jar.*"
1014
1015   inputs.dir(classesDir)
1016   outputs.file("${jalviewDir}/${packageDir}/${archiveName}")
1017 }
1018
1019
1020 task copyJars(type: Copy) {
1021   from fileTree(dir: classesDir, include: "**/*.jar").files
1022   into "${jalviewDir}/${packageDir}"
1023 }
1024
1025
1026 // doing a Sync instead of Copy as Copy doesn't deal with "outputs" very well
1027 task syncJars(type: Sync) {
1028   from fileTree(dir: "${jalviewDir}/${libDistDir}", include: "**/*.jar").files
1029   into "${jalviewDir}/${packageDir}"
1030   preserve {
1031     include jar.archiveName
1032   }
1033 }
1034
1035
1036 task makeDist {
1037   group = "build"
1038   description = "Put all required libraries in dist"
1039   // order of "cleanPackageDir", "copyJars", "jar" important!
1040   jar.mustRunAfter cleanPackageDir
1041   syncJars.mustRunAfter cleanPackageDir
1042   dependsOn cleanPackageDir
1043   dependsOn syncJars
1044   dependsOn jar
1045   outputs.dir("${jalviewDir}/${packageDir}")
1046 }
1047
1048
1049 task cleanDist {
1050   dependsOn cleanPackageDir
1051   dependsOn cleanTest
1052   dependsOn clean
1053 }
1054
1055 shadowJar {
1056   group = "distribution"
1057   if (buildDist) {
1058     dependsOn makeDist
1059   }
1060   from ("${jalviewDir}/${libDistDir}") {
1061     include("*.jar")
1062   }
1063   manifest {
1064     attributes 'Implementation-Version': JALVIEW_VERSION
1065   }
1066   mainClassName = shadowJarMainClass
1067   mergeServiceFiles()
1068   classifier = "all-"+JALVIEW_VERSION+"-j"+JAVA_VERSION
1069   minimize()
1070 }
1071
1072
1073 task getdownWebsite() {
1074   group = "distribution"
1075   description = "Create the getdown minimal app folder, and website folder for this version of jalview. Website folder also used for offline app installer"
1076   if (buildDist) {
1077     dependsOn makeDist
1078   }
1079
1080   def getdownWebsiteResourceFilenames = []
1081   def getdownTextString = ""
1082   def getdownResourceDir = getdownResourceDir
1083   def getdownResourceFilenames = []
1084
1085   doFirst {
1086     // clean the getdown website and files dir before creating getdown folders
1087     delete getdownWebsiteDir
1088     delete getdownFilesDir
1089
1090     copy {
1091       from buildProperties
1092       rename(build_properties_file, getdown_build_properties)
1093       into getdownAppDir
1094     }
1095     getdownWebsiteResourceFilenames += "${getdownAppDistDir}/${getdown_build_properties}"
1096
1097     // set some getdown_txt_ properties then go through all properties looking for getdown_txt_...
1098     def props = project.properties.sort { it.key }
1099     if (getdownAltJavaMinVersion != null && getdownAltJavaMinVersion.length() > 0) {
1100       props.put("getdown_txt_java_min_version", getdownAltJavaMinVersion)
1101     }
1102     if (getdownAltJavaMaxVersion != null && getdownAltJavaMaxVersion.length() > 0) {
1103       props.put("getdown_txt_java_max_version", getdownAltJavaMaxVersion)
1104     }
1105     if (getdownAltMultiJavaLocation != null && getdownAltMultiJavaLocation.length() > 0) {
1106       props.put("getdown_txt_multi_java_location", getdownAltMultiJavaLocation)
1107     }
1108
1109     props.put("getdown_txt_title", jalview_name)
1110     props.put("getdown_txt_ui.name", install4jApplicationName)
1111
1112     // start with appbase
1113     getdownTextString += "appbase = ${getdownAppBase}\n"
1114     props.each{ prop, val ->
1115       if (prop.startsWith("getdown_txt_") && val != null) {
1116         if (prop.startsWith("getdown_txt_multi_")) {
1117           def key = prop.substring(18)
1118           val.split(",").each{ v ->
1119             def line = "${key} = ${v}\n"
1120             getdownTextString += line
1121           }
1122         } else {
1123           // file values rationalised
1124           if (val.indexOf('/') > -1 || prop.startsWith("getdown_txt_resource")) {
1125             def r = null
1126             if (val.indexOf('/') == 0) {
1127               // absolute path
1128               r = file(val)
1129             } else if (val.indexOf('/') > 0) {
1130               // relative path (relative to jalviewDir)
1131               r = file( "${jalviewDir}/${val}" )
1132             }
1133             if (r.exists()) {
1134               val = "${getdown_resource_dir}/" + r.getName()
1135               getdownWebsiteResourceFilenames += val
1136               getdownResourceFilenames += r.getPath()
1137             }
1138           }
1139           if (! prop.startsWith("getdown_txt_resource")) {
1140             def line = prop.substring(12) + " = ${val}\n"
1141             getdownTextString += line
1142           }
1143         }
1144       }
1145     }
1146
1147     getdownWebsiteResourceFilenames.each{ filename ->
1148       getdownTextString += "resource = ${filename}\n"
1149     }
1150     getdownResourceFilenames.each{ filename ->
1151       copy {
1152         from filename
1153         into getdownResourceDir
1154       }
1155     }
1156
1157     def codeFiles = []
1158     fileTree(file(packageDir)).each{ f ->
1159       if (f.isDirectory()) {
1160         def files = fileTree(dir: f, include: ["*"]).getFiles()
1161         codeFiles += files
1162       } else if (f.exists()) {
1163         codeFiles += f
1164       }
1165     }
1166     codeFiles.sort().each{f ->
1167       def name = f.getName()
1168       def line = "code = ${getdownAppDistDir}/${name}\n"
1169       getdownTextString += line
1170       copy {
1171         from f.getPath()
1172         into getdownAppDir
1173       }
1174     }
1175
1176     // NOT USING MODULES YET, EVERYTHING SHOULD BE IN dist
1177     /*
1178     if (JAVA_VERSION.equals("11")) {
1179     def j11libFiles = fileTree(dir: "${jalviewDir}/${j11libDir}", include: ["*.jar"]).getFiles()
1180     j11libFiles.sort().each{f ->
1181     def name = f.getName()
1182     def line = "code = ${getdown_j11lib_dir}/${name}\n"
1183     getdownTextString += line
1184     copy {
1185     from f.getPath()
1186     into getdownJ11libDir
1187     }
1188     }
1189     }
1190      */
1191
1192     // getdown-launcher.jar should not be in main application class path so the main application can move it when updated.  Listed as a resource so it gets updated.
1193     //getdownTextString += "class = " + file(getdownLauncher).getName() + "\n"
1194     getdownTextString += "resource = ${getdown_launcher_new}\n"
1195     getdownTextString += "class = ${mainClass}\n"
1196
1197     def getdown_txt = file("${getdownWebsiteDir}/getdown.txt")
1198     getdown_txt.write(getdownTextString)
1199
1200     def getdownLaunchJvl = getdown_launch_jvl_name + ( (jvlChannelName != null && jvlChannelName.length() > 0)?"-${jvlChannelName}":"" ) + ".jvl"
1201     def launchJvl = file("${getdownWebsiteDir}/${getdownLaunchJvl}")
1202     launchJvl.write("appbase=${getdownAppBase}")
1203
1204     copy {
1205       from getdownLauncher
1206       rename(file(getdownLauncher).getName(), getdown_launcher_new)
1207       into getdownWebsiteDir
1208     }
1209
1210     copy {
1211       from getdownLauncher
1212       if (file(getdownLauncher).getName() != getdown_launcher) {
1213         rename(file(getdownLauncher).getName(), getdown_launcher)
1214       }
1215       into getdownWebsiteDir
1216     }
1217
1218     if (! (CHANNEL.startsWith("ARCHIVE") || CHANNEL.startsWith("DEVELOP"))) {
1219       copy {
1220         from getdown_txt
1221         from getdownLauncher
1222         from "${getdownWebsiteDir}/${getdown_build_properties}"
1223         if (file(getdownLauncher).getName() != getdown_launcher) {
1224           rename(file(getdownLauncher).getName(), getdown_launcher)
1225         }
1226         into getdownInstallDir
1227       }
1228
1229       copy {
1230         from getdownInstallDir
1231         into getdownFilesInstallDir
1232       }
1233     }
1234
1235     copy {
1236       from getdown_txt
1237       from launchJvl
1238       from getdownLauncher
1239       from "${getdownWebsiteDir}/${getdown_build_properties}"
1240       if (file(getdownLauncher).getName() != getdown_launcher) {
1241         rename(file(getdownLauncher).getName(), getdown_launcher)
1242       }
1243       into getdownFilesDir
1244     }
1245
1246     copy {
1247       from getdownResourceDir
1248       into "${getdownFilesDir}/${getdown_resource_dir}"
1249     }
1250   }
1251
1252   if (buildDist) {
1253     inputs.dir("${jalviewDir}/${packageDir}")
1254   }
1255   outputs.dir(getdownWebsiteDir)
1256   outputs.dir(getdownFilesDir)
1257 }
1258
1259
1260 // a helper task to allow getdown digest of any dir: `gradle getdownDigestDir -PDIGESTDIR=/path/to/my/random/getdown/dir
1261 task getdownDigestDir(type: JavaExec) {
1262   def digestDirPropertyName = "DIGESTDIR"
1263   description = "Digest a local dir (-P${digestDirPropertyName}=...) for getdown"
1264   doFirst {
1265     classpath = files(getdownLauncher)
1266     def digestDir = findProperty(digestDirPropertyName)
1267     if (digestDir == null) {
1268       throw new GradleException("Must provide a DIGESTDIR value to produce an alternative getdown digest")
1269     }
1270     args digestDir
1271   }
1272   main = "com.threerings.getdown.tools.Digester"
1273 }
1274
1275
1276 task getdownDigest(type: JavaExec) {
1277   group = "distribution"
1278   description = "Digest the getdown website folder"
1279   dependsOn getdownWebsite
1280   doFirst {
1281     classpath = files(getdownLauncher)
1282   }
1283   main = "com.threerings.getdown.tools.Digester"
1284   args getdownWebsiteDir
1285   inputs.dir(getdownWebsiteDir)
1286   outputs.file("${getdownWebsiteDir}/digest2.txt")
1287 }
1288
1289
1290 task getdown() {
1291   group = "distribution"
1292   description = "Create the minimal and full getdown app folder for installers and website and create digest file"
1293   dependsOn getdownDigest
1294   doLast {
1295     if (reportRsyncCommand) {
1296       def fromDir = getdownWebsiteDir + (getdownWebsiteDir.endsWith('/')?'':'/')
1297       def toDir = "${getdown_rsync_dest}/${getdownDir}" + (getdownDir.endsWith('/')?'':'/')
1298       println "LIKELY RSYNC COMMAND:"
1299       println "mkdir -p '$toDir'\nrsync -avh --delete '$fromDir' '$toDir'"
1300       if (RUNRSYNC == "true") {
1301         exec {
1302           commandLine "mkdir", "-p", toDir
1303         }
1304         exec {
1305           commandLine "rsync", "-avh", "--delete", fromDir, toDir
1306         }
1307       }
1308     }
1309   }
1310 }
1311
1312
1313 clean {
1314   doFirst {
1315     delete getdownWebsiteDir
1316     delete getdownFilesDir
1317   }
1318 }
1319
1320
1321 install4j {
1322   if (file(install4jHomeDir).exists()) {
1323     // good to go!
1324   } else if (file(System.getProperty("user.home")+"/buildtools/install4j").exists()) {
1325     install4jHomeDir = System.getProperty("user.home")+"/buildtools/install4j"
1326   } else if (file("/Applications/install4j.app/Contents/Resources/app").exists()) {
1327     install4jHomeDir = "/Applications/install4j.app/Contents/Resources/app"
1328   }
1329   installDir(file(install4jHomeDir))
1330
1331   mediaTypes = Arrays.asList(install4j_media_types.split(","))
1332 }
1333
1334
1335 task copyInstall4jTemplate {
1336   def install4jTemplateFile = file("${install4jDir}/${install4j_template}")
1337   def install4jFileAssociationsFile = file("${install4jDir}/${install4j_installer_file_associations}")
1338   inputs.file(install4jTemplateFile)
1339   inputs.file(install4jFileAssociationsFile)
1340   outputs.file(install4jConfFile)
1341
1342   doLast {
1343     def install4jConfigXml = new XmlParser().parse(install4jTemplateFile)
1344
1345     // turn off code signing if no OSX_KEYPASS
1346     if (OSX_KEYPASS == "") {
1347       install4jConfigXml.'**'.codeSigning.each { codeSigning ->
1348         codeSigning.'@macEnabled' = "false"
1349       }
1350       install4jConfigXml.'**'.windows.each { windows ->
1351         windows.'@runPostProcessor' = "false"
1352       }
1353     }
1354
1355     // turn off checksum creation for LOCAL channel
1356     def e = install4jConfigXml.application[0]
1357     if (CHANNEL == "LOCAL") {
1358       e.'@createChecksums' = "false"
1359     } else {
1360       e.'@createChecksums' = "true"
1361     }
1362
1363     // put file association actions where placeholder action is
1364     def install4jFileAssociationsText = install4jFileAssociationsFile.text
1365     def fileAssociationActions = new XmlParser().parseText("<actions>${install4jFileAssociationsText}</actions>")
1366     install4jConfigXml.'**'.action.any { a ->
1367       if (a.'@name' == 'EXTENSIONS_REPLACED_BY_GRADLE') {
1368         def parent = a.parent()
1369         parent.remove(a)
1370         fileAssociationActions.each { faa ->
1371             parent.append(faa)
1372         }
1373         // don't need to continue in .any loop once replacements have been made
1374         return true
1375       }
1376     }
1377
1378     // write install4j file
1379     install4jConfFile.text = XmlUtil.serialize(install4jConfigXml)
1380   }
1381 }
1382
1383
1384 clean {
1385   doFirst {
1386     delete install4jConfFile
1387   }
1388 }
1389
1390
1391 task installers(type: com.install4j.gradle.Install4jTask) {
1392   group = "distribution"
1393   description = "Create the install4j installers"
1394   dependsOn setGitVals
1395   dependsOn getdown
1396   dependsOn copyInstall4jTemplate
1397
1398   projectFile = install4jConfFile
1399
1400   // create an md5 for the input files to use as version for install4j conf file
1401   def digest = MessageDigest.getInstance("MD5")
1402   digest.update(
1403     (file("${install4jDir}/${install4j_template}").text + 
1404     file("${install4jDir}/${install4j_info_plist_file_associations}").text +
1405     file("${install4jDir}/${install4j_installer_file_associations}").text).bytes)
1406   def filesMd5 = new BigInteger(1, digest.digest()).toString(16)
1407   if (filesMd5.length() >= 8) {
1408     filesMd5 = filesMd5.substring(0,8)
1409   }
1410   def install4jTemplateVersion = "${JALVIEW_VERSION}_F${filesMd5}_C${gitHash}"
1411   // make install4jBuildDir relative to jalviewDir
1412   def install4jBuildDir = "${install4j_build_dir}/${JAVA_VERSION}"
1413
1414   variables = [
1415     'JALVIEW_NAME': jalview_name,
1416     'JALVIEW_APPLICATION_NAME': install4jApplicationName,
1417     'JALVIEW_DIR': "../..",
1418     'OSX_KEYSTORE': OSX_KEYSTORE,
1419     'JSIGN_SH': JSIGN_SH,
1420     'JRE_DIR': getdown_app_dir_java,
1421     'INSTALLER_TEMPLATE_VERSION': install4jTemplateVersion,
1422     'JALVIEW_VERSION': JALVIEW_VERSION,
1423     'JAVA_MIN_VERSION': JAVA_MIN_VERSION,
1424     'JAVA_MAX_VERSION': JAVA_MAX_VERSION,
1425     'JAVA_VERSION': JAVA_VERSION,
1426     'JAVA_INTEGER_VERSION': JAVA_INTEGER_VERSION,
1427     'VERSION': JALVIEW_VERSION,
1428     'MACOS_JAVA_VM_DIR': macosJavaVMDir,
1429     'WINDOWS_JAVA_VM_DIR': windowsJavaVMDir,
1430     'LINUX_JAVA_VM_DIR': linuxJavaVMDir,
1431     'MACOS_JAVA_VM_TGZ': macosJavaVMTgz,
1432     'WINDOWS_JAVA_VM_TGZ': windowsJavaVMTgz,
1433     'LINUX_JAVA_VM_TGZ': linuxJavaVMTgz,
1434     'COPYRIGHT_MESSAGE': install4j_copyright_message,
1435     'BUNDLE_ID': install4jBundleId,
1436     'INTERNAL_ID': install4jInternalId,
1437     'MACOS_DS_STORE': install4jDSStore,
1438     'MACOS_DMG_BG_IMAGE': install4jDMGBackgroundImage,
1439     'INSTALLER_NAME': install4jInstallerName,
1440     'INSTALL4J_UTILS_DIR': install4j_utils_dir,
1441     'GETDOWN_WEBSITE_DIR': getdown_website_dir,
1442     'GETDOWN_FILES_DIR': getdown_files_dir,
1443     'GETDOWN_RESOURCE_DIR': getdown_resource_dir,
1444     'GETDOWN_DIST_DIR': getdownAppDistDir,
1445     'GETDOWN_ALT_DIR': getdown_app_dir_alt,
1446     'GETDOWN_INSTALL_DIR': getdown_install_dir,
1447     'INFO_PLIST_FILE_ASSOCIATIONS_FILE': install4j_info_plist_file_associations,
1448     'BUILD_DIR': install4jBuildDir,
1449     'UNIX_DESKTOP_ADDITIONS': install4j_unix_desktop_additions,
1450     'APPLICATION_FOLDER': install4jApplicationFolder,
1451     'UNIX_APPLICATION_FOLDER': install4jUnixApplicationFolder,
1452     'EXECUTABLE_NAME': install4jExecutableName,
1453     'EXTRA_SCHEME': install4jExtraScheme,
1454   ]
1455
1456   println("INSTALL4J VARIABLES:")
1457   variables.each{k,v->println("${k}=${v}")}
1458
1459   destination = "${jalviewDir}/${install4jBuildDir}"
1460   buildSelected = true
1461
1462   if (install4j_faster.equals("true") || CHANNEL.startsWith("LOCAL")) {
1463     faster = true
1464     disableSigning = true
1465   }
1466
1467   if (OSX_KEYPASS) {
1468     macKeystorePassword = OSX_KEYPASS
1469   }
1470
1471   doFirst {
1472     println("Using projectFile "+projectFile)
1473   }
1474
1475   inputs.dir(getdownWebsiteDir)
1476   inputs.file(install4jConfFile)
1477   inputs.file("${install4jDir}/${install4j_info_plist_file_associations}")
1478   inputs.dir(macosJavaVMDir)
1479   inputs.dir(windowsJavaVMDir)
1480   outputs.dir("${jalviewDir}/${install4j_build_dir}/${JAVA_VERSION}")
1481 }
1482
1483
1484 task sourceDist(type: Tar) {
1485   
1486   def VERSION_UNDERSCORES = JALVIEW_VERSION.replaceAll("\\.", "_")
1487   def outputFileName = "${project.name}_${VERSION_UNDERSCORES}.tar.gz"
1488   // cater for buildship < 3.1 [3.0.1 is max version in eclipse 2018-09]
1489   try {
1490     archiveFileName = outputFileName
1491   } catch (Exception e) {
1492     archiveName = outputFileName
1493   }
1494   
1495   compression Compression.GZIP
1496   
1497   into project.name
1498
1499   def EXCLUDE_FILES=[
1500     "build/*",
1501     "bin/*",
1502     "test-output/",
1503     "test-reports",
1504     "tests",
1505     "clover*/*",
1506     ".*",
1507     "benchmarking/*",
1508     "**/.*",
1509     "*.class",
1510     "**/*.class","$j11modDir/**/*.jar","appletlib","**/*locales",
1511     "*locales/**",
1512     "utils/InstallAnywhere",
1513     "**/*.log",
1514   ] 
1515   def PROCESS_FILES=[
1516     "AUTHORS",
1517     "CITATION",
1518     "FEATURETODO",
1519     "JAVA-11-README",
1520     "FEATURETODO",
1521     "LICENSE",
1522     "**/README",
1523     "RELEASE",
1524     "THIRDPARTYLIBS",
1525     "TESTNG",
1526     "build.gradle",
1527     "gradle.properties",
1528     "**/*.java",
1529     "**/*.html",
1530     "**/*.xml",
1531     "**/*.gradle",
1532     "**/*.groovy",
1533     "**/*.properties",
1534     "**/*.perl",
1535     "**/*.sh",
1536   ]
1537   def INCLUDE_FILES=[
1538     ".settings/org.eclipse.jdt.core.jalview.prefs",
1539   ]
1540
1541   from(jalviewDir) {
1542     exclude (EXCLUDE_FILES)
1543     include (PROCESS_FILES)
1544     filter(ReplaceTokens,
1545       beginToken: '$$',
1546       endToken: '$$',
1547       tokens: [
1548         'Version-Rel': JALVIEW_VERSION,
1549         'Year-Rel': getDate("yyyy")
1550       ]
1551     )
1552   }
1553   from(jalviewDir) {
1554     exclude (EXCLUDE_FILES)
1555     exclude (PROCESS_FILES)
1556     exclude ("appletlib")
1557     exclude ("**/*locales")
1558     exclude ("*locales/**")
1559     exclude ("utils/InstallAnywhere")
1560
1561     exclude (getdown_files_dir)
1562     exclude (getdown_website_dir)
1563
1564     // exluding these as not using jars as modules yet
1565     exclude ("$j11modDir/**/*.jar")
1566   }
1567   from(jalviewDir) {
1568     include(INCLUDE_FILES)
1569   }
1570 //  from (jalviewDir) {
1571 //    // explicit includes for stuff that seemed to not get included
1572 //    include(fileTree("test/**/*."))
1573 //    exclude(EXCLUDE_FILES)
1574 //    exclude(PROCESS_FILES)
1575 //  }
1576 }
1577
1578
1579 task helppages {
1580   dependsOn copyHelp
1581   dependsOn pubhtmlhelp
1582   
1583   inputs.dir("${classesDir}/${help_dir}")
1584   outputs.dir("${buildDir}/distributions/${help_dir}")
1585 }
1586
1587 // LARGE AMOUNT OF JALVIEWJS STUFF DELETED HERE
1588 task eclipseAutoBuildTask {}
1589 task eclipseSynchronizationTask {}