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