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