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