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