JAL-3210 catch exception for eclipse
[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
6 import groovy.transform.ExternalizeMethods
7
8 buildscript {
9   dependencies {
10     classpath 'org.openclover:clover:4.3.1'
11   }
12 }
13
14 plugins {
15   id 'java'
16   id 'application'
17   id 'eclipse'
18   id 'com.github.johnrengelman.shadow' version '4.0.3'
19   id 'com.install4j.gradle' version '7.0.9'
20 }
21
22 // local build environment properties
23 def localProps = "${jalviewDir}/local.properties"
24 if (file(localProps).exists()) {
25   try {
26     def p = new Properties()
27     def localPropsFIS = new FileInputStream(localProps)
28     p.load(localPropsFIS)
29     localPropsFIS.close()
30     p.each {
31       key, val -> 
32         def over = getProperty(key) != null
33         setProperty(key, val)
34         if (over) {
35           println("Overriding property '${key}' with local.properties value '${val}'")
36         }
37     }
38   } catch (Exception e) {
39   }
40 }
41
42 repositories {
43   jcenter()
44   mavenCentral()
45   mavenLocal()
46   flatDir {
47     dirs gradlePluginsDir
48   }
49 }
50
51 dependencies {
52   compile 'org.apache.commons:commons-compress:1.18'
53 }
54
55 mainClassName = launcherClass
56 def cloverInstrDir = file("${buildDir}/${cloverSourcesInstrDir}")
57 def classes = "${jalviewDir}/${classesDir}"
58
59 if (clover.equals("true")) {
60   use_clover = true
61   classes = "${buildDir}/${cloverClassesDir}"
62 } else {
63   use_clover = false
64   classes = "${jalviewDir}/${classesDir}"
65 }
66
67 // configure classpath/args for j8/j11 compilation
68
69 def jalviewDirAbsolutePath = file(jalviewDir).getAbsolutePath()
70 def libDir
71 def libDistDir
72 def compile_source_compatibility
73 def compile_target_compatibility
74
75 ext {
76   getdownWebsiteDir = "${jalviewDir}/${getdown_website_dir}/${JAVA_VERSION}"
77   getdownDir = ""
78   reportRsyncCmd = false
79   buildDist = true
80   buildProperties = buildPropertiesFile
81   getdownLauncher = "${jalviewDir}/${getdown_lib_dir}/${getdown_launcher}"
82   switch (CHANNEL) {
83
84     case "BUILD":
85     // TODO: get bamboo build artifact URL for getdown artifacts
86     getdown_channel_base = bamboo_channelbase
87     getdown_channel_name = "${bamboo_planKey}/${JAVA_VERSION}"
88     getdown_app_base = "${bamboo_channelbase}/${bamboo_planKey}${bamboo_getdown_channel_suffix}/${JAVA_VERSION}"
89     getdown_app_dir = getdown_app_dir_alt
90     buildProperties = "${jalviewDir}/${classesDir}/${buildPropertiesFile}"
91     break
92
93     case "RELEASE":
94     getdown_channel_name = CHANNEL.toLowerCase()
95     getdownDir = "${getdown_channel_name}/${JAVA_VERSION}"
96     getdown_app_base = "${getdown_channel_base}/${getdownDir}"
97     getdown_app_dir = getdown_app_dir_release
98     buildProperties = "${jalviewDir}/${classesDir}/${buildPropertiesFile}"
99     reportRsyncCommand = true
100     break
101
102     case "ARCHIVE":
103     getdown_channel_name = CHANNEL.toLowerCase()+"/${JALVIEW_VERSION}"
104     getdownDir = "${getdown_channel_name}/${JAVA_VERSION}"
105     getdown_app_base = "${getdown_channel_base}/${getdownDir}"
106     getdown_app_dir = getdown_app_dir_alt
107     if (!file("${ARCHIVEDIR}/${packageDir}").exists()) {
108       print "Must provide an ARCHIVEDIR value to produce an archive distribution"
109       exit
110     } else {
111       packageDir = "${ARCHIVEDIR}/${packageDir}"
112       buildProperties = "${ARCHIVEDIR}/${classesDir}/${buildPropertiesFile}"
113       buildDist = false
114     }
115     reportRsyncCommand = true
116     break
117
118     case "ARCHIVELOCAL":
119     getdown_channel_name = "archive/${JALVIEW_VERSION}"
120     getdownDir = "${getdown_channel_name}/${JAVA_VERSION}"
121     getdown_app_base = file(getdownWebsiteDir).toURI().toString()
122     getdown_app_dir = getdown_app_dir_alt
123     if (!file("${ARCHIVEDIR}/${packageDir}").exists()) {
124       print "Must provide an ARCHIVEDIR value to produce an archive distribution"
125       exit
126     } else {
127       packageDir = "${ARCHIVEDIR}/${packageDir}"
128       buildProperties = "${ARCHIVEDIR}/${classesDir}/${buildPropertiesFile}"
129       buildDist = false
130     }
131     reportRsyncCommand = true
132     getdownLauncher = "${jalviewDir}/${getdown_lib_dir}/${getdown_launcher_local}"
133     break
134
135     case "DEVELOP":
136     getdown_channel_name = CHANNEL.toLowerCase()
137     getdownDir = "${getdown_channel_name}/${JAVA_VERSION}"
138     getdown_app_base = "${getdown_channel_base}/${getdownDir}"
139     getdown_app_dir = getdown_app_dir_alt
140     buildProperties = "${jalviewDir}/${classesDir}/${buildPropertiesFile}"
141     reportRsyncCommand = true
142     break
143
144     case "TEST-RELEASE":
145     getdown_channel_name = CHANNEL.toLowerCase()
146     getdownDir = "${getdown_channel_name}/${JAVA_VERSION}"
147     getdown_app_base = "${getdown_channel_base}/${getdownDir}"
148     getdown_app_dir = getdown_app_dir_alt
149     buildProperties = "${jalviewDir}/${classesDir}/${buildPropertiesFile}"
150     reportRsyncCommand = true
151     break
152
153     case ~/^SCRATCH(|-[-\w]*)$/:
154     getdown_channel_name = CHANNEL
155     getdownDir = "${getdown_channel_name}/${JAVA_VERSION}"
156     getdown_app_base = "${getdown_channel_base}/${getdownDir}"
157     getdown_app_dir = getdown_app_dir_alt
158     buildProperties = "${jalviewDir}/${classesDir}/${buildPropertiesFile}"
159     reportRsyncCommand = true
160     break
161
162     case "LOCAL":
163     getdown_app_base = file(getdownWebsiteDir).toURI().toString()
164     getdown_app_dir = getdown_app_dir_alt
165     buildProperties = "${jalviewDir}/${classesDir}/${buildPropertiesFile}"
166     getdownLauncher = "${jalviewDir}/${getdown_lib_dir}/${getdown_launcher_local}"
167     break
168
169     default: // something wrong specified
170     print("CHANNEL must be one of BUILD, RELEASE, ARCHIVE, DEVELOP, TEST-RELEASE, SCRATCH-..., LOCAL [default]")
171     exit
172     break
173
174   }
175
176   getdownAppDir = "${getdownWebsiteDir}/${getdown_app_dir}"
177   //getdownJ11libDir = "${getdownWebsiteDir}/${getdown_j11lib_dir}"
178   getdownResourceDir = "${getdownWebsiteDir}/${getdown_resource_dir}"
179   getdownInstallDir = "${getdownWebsiteDir}/${getdown_install_dir}"
180   getdownFilesDir = "${jalviewDir}/${getdown_files_dir}/${JAVA_VERSION}/"
181   getdownFilesInstallDir = "${getdownFilesDir}/${getdown_install_dir}"
182   /* compile without modules -- using classpath libraries
183   modules_compileClasspath = fileTree(dir: "${jalviewDir}/${j11modDir}", include: ["*.jar"])
184   modules_runtimeClasspath = modules_compileClasspath
185   */
186   gitHash = ""
187   gitBranch = ""
188
189   println("Using a ${CHANNEL} profile.")
190 }
191
192 def JAVA_INTEGER_VERSION
193 def additional_compiler_args = []
194 // these are getdown.txt properties defined dependent on the JAVA_VERSION
195 def getdown_alt_java_min_version
196 def getdown_alt_java_max_version
197 // this property is assigned below and expanded to multiple lines in the getdown task
198 def getdown_alt_multi_java_location
199 // this property is for the Java library used in eclipse
200 def eclipse_java_runtime_name
201 if (JAVA_VERSION.equals("1.8")) {
202   JAVA_INTEGER_VERSION = "8"
203   //libDir = j8libDir
204   libDir = j11libDir
205   libDistDir = j8libDir
206   compile_source_compatibility = 1.8
207   compile_target_compatibility = 1.8
208   getdown_alt_java_min_version = getdown_alt_java8_min_version
209   getdown_alt_java_max_version = getdown_alt_java8_max_version
210   getdown_alt_multi_java_location = getdown_alt_java8_txt_multi_java_location
211   eclipse_java_runtime_name = "JavaSE-1.8"
212 } else if (JAVA_VERSION.equals("11")) {
213   JAVA_INTEGER_VERSION = "11"
214   libDir = j11libDir
215   libDistDir = j11libDir
216   compile_source_compatibility = 11
217   compile_target_compatibility = 11
218   getdown_alt_java_min_version = getdown_alt_java11_min_version
219   getdown_alt_java_max_version = getdown_alt_java11_max_version
220   getdown_alt_multi_java_location = getdown_alt_java11_txt_multi_java_location
221   eclipse_java_runtime_name = "JavaSE-11"
222   /* compile without modules -- using classpath libraries
223   additional_compiler_args += [
224   '--module-path', ext.modules_compileClasspath.asPath,
225   '--add-modules', j11modules
226   ]
227   */
228 } else if (JAVA_VERSION.equals("12") || JAVA_VERSION.equals("13")) {
229   JAVA_INTEGER_VERSION = JAVA_VERSION
230   libDir = j11libDir
231   libDistDir = j11libDir
232   compile_source_compatibility = JAVA_VERSION
233   compile_target_compatibility = JAVA_VERSION
234   getdown_alt_java_min_version = getdown_alt_java11_min_version
235   getdown_alt_java_max_version = getdown_alt_java11_max_version
236   getdown_alt_multi_java_location = getdown_alt_java11_txt_multi_java_location
237   eclipse_java_runtime_name = "JavaSE-11"
238   /* compile without modules -- using classpath libraries
239   additional_compiler_args += [
240   '--module-path', ext.modules_compileClasspath.asPath,
241   '--add-modules', j11modules
242   ]
243   */
244 } else {
245   throw new GradleException("JAVA_VERSION=${JAVA_VERSION} not currently supported by Jalview")
246 }
247
248 sourceSets {
249
250   main {
251     java {
252       srcDirs "${jalviewDir}/${sourceDir}"
253       outputDir = file("${classes}")
254     }
255
256     resources {
257       srcDirs "${jalviewDir}/${resourceDir}"
258     }
259
260     jar.destinationDir = file("${jalviewDir}/${packageDir}")
261
262     compileClasspath = files(sourceSets.main.java.outputDir)
263     compileClasspath += fileTree(dir: "${jalviewDir}/${libDir}", include: ["*.jar"])
264
265     runtimeClasspath = compileClasspath
266   }
267
268   clover {
269     java {
270       srcDirs = [ cloverInstrDir ]
271       outputDir = file("${buildDir}/${cloverClassesDir}")
272     }
273
274     resources {
275       srcDirs = sourceSets.main.resources.srcDirs
276     }
277     compileClasspath = configurations.cloverRuntime + files( sourceSets.clover.java.outputDir )
278     compileClasspath += files(sourceSets.main.java.outputDir)
279     compileClasspath += sourceSets.main.compileClasspath
280     compileClasspath += fileTree(dir: "${jalviewDir}/${utilsDir}", include: ["**/*.jar"])
281     compileClasspath += fileTree(dir: "${jalviewDir}/${libDir}", include: ["*.jar"])
282
283     runtimeClasspath = compileClasspath
284   }
285
286   test {
287     java {
288       srcDirs "${jalviewDir}/${testSourceDir}"
289       outputDir = file("${jalviewDir}/${testOutputDir}")
290     }
291
292     resources {
293       srcDirs = sourceSets.main.resources.srcDirs
294     }
295
296     compileClasspath = files( sourceSets.test.java.outputDir )
297
298     if (use_clover) {
299       compileClasspath += sourceSets.clover.compileClasspath
300     } else {
301       compileClasspath += files(sourceSets.main.java.outputDir)
302     }
303
304     compileClasspath += fileTree(dir: "${jalviewDir}/${libDir}", include: ["*.jar"])
305     compileClasspath += fileTree(dir: "${jalviewDir}/${utilsDir}/testnglibs", include: ["**/*.jar"])
306     compileClasspath += fileTree(dir: "${jalviewDir}/${utilsDir}/testlibs", include: ["**/*.jar"])
307
308     runtimeClasspath = compileClasspath
309   }
310 }
311
312 // clover bits
313 dependencies {
314   if (use_clover) {
315     cloverCompile 'org.openclover:clover:4.3.1'
316     testCompile 'org.openclover:clover:4.3.1'
317   }
318 }
319
320 configurations {
321   cloverRuntime
322   cloverRuntime.extendsFrom cloverCompile
323 }
324
325 eclipse {
326   project {
327     name = eclipse_project_name
328
329     natures 'org.eclipse.jdt.core.javanature',
330     'org.eclipse.jdt.groovy.core.groovyNature',
331     'org.eclipse.buildship.core.gradleprojectnature'
332
333     buildCommand 'org.eclipse.jdt.core.javabuilder'
334     buildCommand 'org.eclipse.buildship.core.gradleprojectbuilder'
335   }
336
337   classpath {
338     //defaultOutputDir = sourceSets.main.java.outputDir
339     def removeThese = []
340     configurations.each{
341       if (it.isCanBeResolved()) {
342         removeThese += it
343       }
344     }
345
346     minusConfigurations += removeThese
347     plusConfigurations = [ ]
348     file {
349
350       whenMerged { cp ->
351         def removeTheseToo = []
352         HashMap<String, Boolean> alreadyAddedSrcPath = new HashMap<>();
353         cp.entries.each { entry ->
354           if (entry.kind == 'src') {
355             if (alreadyAddedSrcPath.getAt(entry.path) || !(entry.path == sourceDir || entry.path == testSourceDir)) {
356               removeTheseToo += entry
357             } else {
358               alreadyAddedSrcPath.putAt(entry.path, true)
359             }
360           }
361         }
362         cp.entries.removeAll(removeTheseToo)
363
364         if (file("${jalviewDir}/${eclipse_bin_dir}/main").isDirectory()) {
365           cp.entries += new Output("${eclipse_bin_dir}/main")
366         }
367         if (file(helpParentDir).isDirectory()) {
368           cp.entries += new Library(fileReference(helpParentDir))
369         }
370         if (file(resourceDir).isDirectory()) {
371           cp.entries += new Library(fileReference(resourceDir))
372         }
373
374         HashMap<String, Boolean> alreadyAddedLibPath = new HashMap<>();
375
376         sourceSets.main.compileClasspath.findAll { it.name.endsWith(".jar") }.each {
377           //don't want to add outputDir as eclipse is using its own output dir in bin/main
378           if (it.isDirectory() || ! it.exists()) {
379             // don't add dirs to classpath
380             return
381           }
382           def itPath = it.toString()
383           if (itPath.startsWith("${jalviewDirAbsolutePath}/")) {
384             // make relative path
385             itPath = itPath.substring(jalviewDirAbsolutePath.length()+1)
386           }
387           if (alreadyAddedLibPath.get(itPath)) {
388             //println("Not adding duplicate entry "+itPath)
389           } else {
390             //println("Adding entry "+itPath)
391             cp.entries += new Library(fileReference(itPath))
392             alreadyAddedLibPath.put(itPath, true)
393           }
394         }
395
396         //fileTree(dir: "$jalviewDir/$utilsDir", include: ["test*/*.jar"]).each {
397         sourceSets.test.compileClasspath.findAll { it.name.endsWith(".jar") }.each {
398           //no longer want to add outputDir as eclipse is using its own output dir in bin/main
399           if (it.isDirectory() || ! it.exists()) {
400             // don't add dirs to classpath
401             return false // groovy "break" in .each closure
402           }
403           def itPath = it.toString()
404           if (itPath.startsWith("${jalviewDirAbsolutePath}/")) {
405             itPath = itPath.substring(jalviewDirAbsolutePath.length()+1)
406           }
407           if (alreadyAddedLibPath.get(itPath)) {
408             // don't duplicate
409           } else {
410             def lib = new Library(fileReference(itPath))
411             lib.entryAttributes["test"] = "true"
412             cp.entries += lib
413             alreadyAddedLibPath.put(itPath, true)
414           }
415         }
416
417       } // whenMerged
418
419     } // file
420
421     containers 'org.eclipse.buildship.core.gradleclasspathcontainer'
422
423   } // classpath
424
425   jdt {
426     // for the IDE, use java 11 compatibility
427     sourceCompatibility = compile_source_compatibility
428     targetCompatibility = compile_target_compatibility
429     javaRuntimeName = eclipse_java_runtime_name
430
431     // add in jalview project specific properties/preferences into eclipse core preferences
432     file {
433       withProperties { props ->
434         def jalview_prefs = new Properties()
435         def ins = new FileInputStream("${jalviewDirAbsolutePath}/${eclipse_extra_jdt_prefs_file}")
436         jalview_prefs.load(ins)
437         ins.close()
438         jalview_prefs.forEach { t, v ->
439           if (props.getAt(t) == null) {
440             props.putAt(t, v)
441           }
442         }
443       }
444     }
445
446   } // jdt
447
448 }
449
450 task cloverInstr() {
451   // only instrument source, we build test classes as normal
452   inputs.files files (sourceSets.main.allJava) // , fileTree(dir:"$jalviewDir/$testSourceDir", include: ["**/*.java"]))
453   outputs.dir cloverInstrDir
454
455   doFirst {
456     delete cloverInstrDir
457     def argsList = ["--initstring", "${buildDir}/clover/clover.db",
458     "-d", "${buildDir}/${cloverSourcesInstrDir}"]
459     argsList.addAll(inputs.files.files.collect({ file ->
460       file.absolutePath
461     }))
462     String[] args = argsList.toArray()
463     println("About to instrument "+args.length +" files")
464     com.atlassian.clover.CloverInstr.mainImpl(args)
465   }
466 }
467
468
469 task cloverReport {
470   group = "Verification"
471     description = "Createst the Clover report"
472     inputs.dir "${buildDir}/clover"
473     outputs.dir "${reportsDir}/clover"
474     onlyIf {
475       file("${buildDir}/clover/clover.db").exists()
476     }
477   doFirst {
478     def argsList = ["--initstring", "${buildDir}/clover/clover.db",
479     "-o", "${reportsDir}/clover"]
480     String[] args = argsList.toArray()
481     com.atlassian.clover.reporters.html.HtmlReporter.runReport(args)
482
483     // and generate ${reportsDir}/clover/clover.xml
484     args = ["--initstring", "${buildDir}/clover/clover.db",
485     "-o", "${reportsDir}/clover/clover.xml"].toArray()
486     com.atlassian.clover.reporters.xml.XMLReporter.runReport(args)
487   }
488 }
489
490 // end clover bits
491
492
493 compileJava {
494
495   doFirst {
496     sourceCompatibility = compile_source_compatibility
497     targetCompatibility = compile_target_compatibility
498     options.compilerArgs = additional_compiler_args
499     print ("Setting target compatibility to "+targetCompatibility+"\n")
500   }
501
502 }
503
504 compileTestJava {
505   if (use_clover) {
506     dependsOn compileCloverJava
507     classpath += configurations.cloverRuntime
508   } else {
509     classpath += sourceSets.main.runtimeClasspath
510   }
511   doFirst {
512     sourceCompatibility = compile_source_compatibility
513     targetCompatibility = compile_target_compatibility
514     options.compilerArgs = additional_compiler_args
515     print ("Setting target compatibility to "+targetCompatibility+"\n")
516   }
517 }
518
519
520 compileCloverJava {
521
522   doFirst {
523     sourceCompatibility = compile_source_compatibility
524     targetCompatibility = compile_target_compatibility
525     options.compilerArgs += additional_compiler_args
526     print ("Setting target compatibility to "+targetCompatibility+"\n")
527   }
528   classpath += configurations.cloverRuntime
529 }
530
531 clean {
532   doFirst {
533     delete sourceSets.main.java.outputDir
534   }
535 }
536
537 cleanTest {
538   doFirst {
539     delete sourceSets.test.java.outputDir
540     delete cloverInstrDir
541   }
542 }
543
544 // format is a string like date.format("dd MMMM yyyy")
545 def getDate(format) {
546   def date = new Date()
547   return date.format(format)
548 }
549
550 task setGitVals {
551   def hashStdOut = new ByteArrayOutputStream()
552   exec {
553     commandLine "git", "rev-parse", "--short", "HEAD"
554     standardOutput = hashStdOut
555     ignoreExitValue true
556   }
557
558   def branchStdOut = new ByteArrayOutputStream()
559   exec {
560     commandLine "git", "rev-parse", "--abbrev-ref", "HEAD"
561     standardOutput = branchStdOut
562     ignoreExitValue true
563   }
564
565   project.ext.gitHash = hashStdOut.toString().trim()
566   project.ext.gitBranch = branchStdOut.toString().trim()
567
568   outputs.upToDateWhen { false }
569 }
570
571 task createBuildProperties(type: WriteProperties) {
572   dependsOn setGitVals
573   inputs.dir("${jalviewDir}/${sourceDir}")
574   inputs.dir("${classes}")
575   inputs.dir("${jalviewDir}/${resourceDir}")
576   outputFile (buildProperties)
577   // taking time specific comment out to allow better incremental builds
578   comment "--Jalview Build Details--\n"+getDate("yyyy-MM-dd HH:mm:ss")
579   //comment "--Jalview Build Details--\n"+getDate("yyyy-MM-dd")
580   property "BUILD_DATE", getDate("HH:mm:ss dd MMMM yyyy")
581   property "VERSION", JALVIEW_VERSION
582   property "INSTALLATION", INSTALLATION+" git-commit:"+project.ext.gitHash+" ["+project.ext.gitBranch+"]"
583   outputs.file(outputFile)
584 }
585
586 def buildingHTML = "${jalviewDir}/${docDir}/building.html"
587 task cleanBuildingHTML(type: Delete) {
588   doFirst {
589     delete buildingHTML
590   }
591 }
592
593 task convertBuildingMD(type: Exec) {
594   dependsOn cleanBuildingHTML
595   def buildingMD = "${jalviewDir}/${docDir}/building.md"
596   def css = "${jalviewDir}/${docDir}/github.css"
597
598   def pandoc = null
599   pandoc_exec.split(",").each {
600     if (file(it.trim()).exists()) {
601       pandoc = it.trim()
602       return true
603     }
604   }
605
606   def hostname = "hostname".execute().text.trim()
607   if ((pandoc == null || ! file(pandoc).exists()) && hostname.equals("jv-bamboo")) {
608     pandoc = System.getProperty("user.home")+"/buildtools/pandoc/bin/pandoc"
609   }
610
611   doFirst {
612     if (pandoc != null && file(pandoc).exists()) {
613         commandLine pandoc, '-s', '-o', buildingHTML, '--metadata', 'pagetitle="Building Jalview from Source"', '--toc', '-H', css, buildingMD
614     } else {
615         println("Cannot find pandoc. Skipping convert building.md to HTML")
616         throw new StopExecutionException()
617     }
618   }
619
620   ignoreExitValue true
621
622   inputs.file(buildingMD)
623   inputs.file(css)
624   outputs.file(buildingHTML)
625 }
626
627 clean {
628   doFirst {
629     delete buildingHTML
630   }
631 }
632
633 task syncDocs(type: Sync) {
634   dependsOn convertBuildingMD
635   def syncDir = "${classes}/${docDir}"
636   from fileTree("${jalviewDir}/${docDir}")
637   into syncDir
638
639 }
640
641 def helpFile = "${classes}/${helpDir}/help.jhm"
642
643 task copyHelp(type: Copy) {
644   def inputDir = "${jalviewDir}/${helpParentDir}/${helpDir}"
645   def outputDir = "${classes}/${helpDir}"
646   from(inputDir) {
647     exclude '**/*.gif'
648     exclude '**/*.jpg'
649     exclude '**/*.png'
650     filter(ReplaceTokens, beginToken: '$$', endToken: '$$', tokens: ['Version-Rel': JALVIEW_VERSION,'Year-Rel': getDate("yyyy")])
651   }
652   from(inputDir) {
653     include '**/*.gif'
654     include '**/*.jpg'
655     include '**/*.png'
656   }
657   into outputDir
658
659   inputs.dir(inputDir)
660   outputs.files(helpFile)
661   outputs.dir(outputDir)
662 }
663
664 task syncLib(type: Sync) {
665   def syncDir = "${classes}/${libDistDir}"
666   from fileTree("${jalviewDir}/${libDistDir}")
667   into syncDir
668 }
669
670 task syncResources(type: Sync) {
671   from "${jalviewDir}/${resourceDir}"
672   include "**/*.*"
673   into "${classes}"
674   preserve {
675     include "**"
676   }
677 }
678
679 task prepare {
680   dependsOn syncResources
681   dependsOn syncDocs
682   dependsOn copyHelp
683 }
684
685
686 //testReportDirName = "test-reports" // note that test workingDir will be $jalviewDir
687 test {
688   dependsOn prepare
689   dependsOn compileJava
690   if (use_clover) {
691     dependsOn cloverInstr
692   }
693
694   if (use_clover) {
695     print("Running tests " + (use_clover?"WITH":"WITHOUT") + " clover [clover="+use_clover+"]\n")
696   }
697
698   useTestNG() {
699     includeGroups testngGroups
700     preserveOrder true
701     useDefaultListeners=true
702   }
703
704   workingDir = jalviewDir
705   //systemProperties 'clover.jar' System.properties.clover.jar
706   sourceCompatibility = compile_source_compatibility
707   targetCompatibility = compile_target_compatibility
708   jvmArgs += additional_compiler_args
709
710 }
711
712 task buildIndices(type: JavaExec) {
713   dependsOn copyHelp
714   classpath = sourceSets.main.compileClasspath
715   main = "com.sun.java.help.search.Indexer"
716   workingDir = "${classes}/${helpDir}"
717   def argDir = "html"
718   args = [ argDir ]
719   inputs.dir("${workingDir}/${argDir}")
720
721   outputs.dir("${classes}/doc")
722   outputs.dir("${classes}/help")
723   outputs.file("${workingDir}/JavaHelpSearch/DOCS")
724   outputs.file("${workingDir}/JavaHelpSearch/DOCS.TAB")
725   outputs.file("${workingDir}/JavaHelpSearch/OFFSETS")
726   outputs.file("${workingDir}/JavaHelpSearch/POSITIONS")
727   outputs.file("${workingDir}/JavaHelpSearch/SCHEMA")
728   outputs.file("${workingDir}/JavaHelpSearch/TMAP")
729 }
730
731 task compileLinkCheck(type: JavaCompile) {
732   options.fork = true
733   classpath = files("${jalviewDir}/${utilsDir}")
734   destinationDir = file("${jalviewDir}/${utilsDir}")
735   source = fileTree(dir: "${jalviewDir}/${utilsDir}", include: ["HelpLinksChecker.java", "BufferedLineReader.java"])
736
737   inputs.file("${jalviewDir}/${utilsDir}/HelpLinksChecker.java")
738   inputs.file("${jalviewDir}/${utilsDir}/HelpLinksChecker.java")
739   outputs.file("${jalviewDir}/${utilsDir}/HelpLinksChecker.class")
740   outputs.file("${jalviewDir}/${utilsDir}/BufferedLineReader.class")
741 }
742
743 task linkCheck(type: JavaExec) {
744   dependsOn prepare, compileLinkCheck
745
746   def helpLinksCheckerOutFile = file("${jalviewDir}/${utilsDir}/HelpLinksChecker.out")
747   classpath = files("${jalviewDir}/${utilsDir}")
748   main = "HelpLinksChecker"
749   workingDir = jalviewDir
750   def help = "${classes}/${helpDir}"
751   args = [ "${classes}/${helpDir}", "-nointernet" ]
752
753   def outFOS = new FileOutputStream(helpLinksCheckerOutFile, false) // false == don't append
754   def errFOS = outFOS
755   standardOutput = new org.apache.tools.ant.util.TeeOutputStream(
756     outFOS,
757     standardOutput)
758   errorOutput = new org.apache.tools.ant.util.TeeOutputStream(
759     outFOS,
760     errorOutput)
761
762   inputs.dir("${classes}/${helpDir}")
763   outputs.file(helpLinksCheckerOutFile)
764 }
765
766 // import the pubhtmlhelp target
767 ant.properties.basedir = "${jalviewDir}"
768 ant.properties.helpBuildDir = "${jalviewDirAbsolutePath}/${classes}/${helpDir}"
769 ant.importBuild "${utilsDir}/publishHelp.xml"
770
771
772 task cleanPackageDir(type: Delete) {
773   doFirst {
774     delete fileTree(dir: "${jalviewDir}/${packageDir}", include: "*.jar")
775   }
776 }
777
778 jar {
779   dependsOn linkCheck
780   dependsOn buildIndices
781   dependsOn createBuildProperties
782
783   manifest {
784     attributes "Main-Class": mainClass,
785     "Permissions": "all-permissions",
786     "Application-Name": "Jalview Desktop",
787     "Codebase": application_codebase
788   }
789
790   destinationDir = file("${jalviewDir}/${packageDir}")
791   archiveName = rootProject.name+".jar"
792
793   exclude "cache*/**"
794   exclude "*.jar"
795   exclude "*.jar.*"
796   exclude "**/*.jar"
797   exclude "**/*.jar.*"
798
799   inputs.dir("${classes}")
800   outputs.file("${jalviewDir}/${packageDir}/${archiveName}")
801 }
802
803 task copyJars(type: Copy) {
804   from fileTree(dir: "${classes}", include: "**/*.jar").files
805   into "${jalviewDir}/${packageDir}"
806 }
807
808 // doing a Sync instead of Copy as Copy doesn't deal with "outputs" very well
809 task syncJars(type: Sync) {
810   from fileTree(dir: "${jalviewDir}/${libDistDir}", include: "**/*.jar").files
811   into "${jalviewDir}/${packageDir}"
812   preserve {
813     include jar.archiveName
814   }
815 }
816
817 task makeDist {
818   group = "build"
819   description = "Put all required libraries in dist"
820   // order of "cleanPackageDir", "copyJars", "jar" important!
821   jar.mustRunAfter cleanPackageDir
822   syncJars.mustRunAfter cleanPackageDir
823   dependsOn cleanPackageDir
824   dependsOn syncJars
825   dependsOn jar
826   outputs.dir("${jalviewDir}/${packageDir}")
827 }
828
829 task cleanDist {
830   dependsOn cleanPackageDir
831   dependsOn cleanTest
832   dependsOn clean
833 }
834
835 shadowJar {
836   group = "distribution"
837   if (buildDist) {
838     dependsOn makeDist
839   }
840   from ("${jalviewDir}/${libDistDir}") {
841     include("*.jar")
842   }
843   manifest {
844     attributes 'Implementation-Version': JALVIEW_VERSION
845   }
846   mainClassName = shadowJarMainClass
847   mergeServiceFiles()
848   classifier = "all-"+JALVIEW_VERSION+"-j"+JAVA_VERSION
849   minimize()
850 }
851
852 task getdownWebsite() {
853   group = "distribution"
854   description = "Create the getdown minimal app folder, and website folder for this version of jalview. Website folder also used for offline app installer"
855   if (buildDist) {
856     dependsOn makeDist
857   }
858
859   def getdownWebsiteResourceFilenames = []
860   def getdownTextString = ""
861   def getdownResourceDir = project.ext.getdownResourceDir
862   def getdownAppDir = project.ext.getdownAppDir
863   def getdownResourceFilenames = []
864
865   doFirst {
866     // clean the getdown website and files dir before creating getdown folders
867     delete project.ext.getdownWebsiteDir
868     delete project.ext.getdownFilesDir
869
870     copy {
871       from buildProperties
872       rename(buildPropertiesFile, getdown_build_properties)
873       into project.ext.getdownAppDir
874     }
875     getdownWebsiteResourceFilenames += "${getdown_app_dir}/${getdown_build_properties}"
876
877     // go through properties looking for getdown_txt_...
878     def props = project.properties.sort { it.key }
879     if (getdown_alt_java_min_version.length() > 0) {
880       props.put("getdown_txt_java_min_version", getdown_alt_java_min_version)
881     }
882     if (getdown_alt_java_max_version.length() > 0) {
883       props.put("getdown_txt_java_max_version", getdown_alt_java_max_version)
884     }
885     props.put("getdown_txt_multi_java_location", getdown_alt_multi_java_location)
886
887     props.put("getdown_txt_appbase", getdown_app_base)
888     props.each{ prop, val ->
889       if (prop.startsWith("getdown_txt_") && val != null) {
890         if (prop.startsWith("getdown_txt_multi_")) {
891           def key = prop.substring(18)
892           val.split(",").each{ v ->
893             def line = "${key} = ${v}\n"
894             getdownTextString += line
895           }
896         } else {
897           // file values rationalised
898           if (val.indexOf('/') > -1 || prop.startsWith("getdown_txt_resource")) {
899             def r = null
900             if (val.indexOf('/') == 0) {
901               // absolute path
902               r = file(val)
903             } else if (val.indexOf('/') > 0) {
904               // relative path (relative to jalviewDir)
905               r = file( "${jalviewDir}/${val}" )
906             }
907             if (r.exists()) {
908               val = "${getdown_resource_dir}/" + r.getName()
909               getdownWebsiteResourceFilenames += val
910               getdownResourceFilenames += r.getPath()
911             }
912           }
913           if (! prop.startsWith("getdown_txt_resource")) {
914             def line = prop.substring(12) + " = ${val}\n"
915             getdownTextString += line
916           }
917         }
918       }
919     }
920
921     getdownWebsiteResourceFilenames.each{ filename ->
922       getdownTextString += "resource = ${filename}\n"
923     }
924     getdownResourceFilenames.each{ filename ->
925       copy {
926         from filename
927         into project.ext.getdownResourceDir
928       }
929     }
930
931     def codeFiles = []
932     fileTree(file(packageDir)).each{ f ->
933       if (f.isDirectory()) {
934         def files = fileTree(dir: f, include: ["*"]).getFiles()
935         codeFiles += files
936       } else if (f.exists()) {
937         codeFiles += f
938       }
939     }
940     codeFiles.sort().each{f ->
941       def name = f.getName()
942       def line = "code = ${getdown_app_dir}/${name}\n"
943       getdownTextString += line
944       copy {
945         from f.getPath()
946         into project.ext.getdownAppDir
947       }
948     }
949
950     // NOT USING MODULES YET, EVERYTHING SHOULD BE IN dist
951     /*
952     if (JAVA_VERSION.equals("11")) {
953     def j11libFiles = fileTree(dir: "${jalviewDir}/${j11libDir}", include: ["*.jar"]).getFiles()
954     j11libFiles.sort().each{f ->
955     def name = f.getName()
956     def line = "code = ${getdown_j11lib_dir}/${name}\n"
957     getdownTextString += line
958     copy {
959     from f.getPath()
960     into project.ext.getdownJ11libDir
961     }
962     }
963     }
964      */
965
966     // 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.
967     //getdownTextString += "class = " + file(getdownLauncher).getName() + "\n"
968     getdownTextString += "resource = ${getdown_launcher_new}\n"
969     getdownTextString += "class = ${mainClass}\n"
970
971     def getdown_txt = file("${project.ext.getdownWebsiteDir}/getdown.txt")
972     getdown_txt.write(getdownTextString)
973
974     def launch_jvl = file("${project.ext.getdownWebsiteDir}/${getdown_launch_jvl}")
975     launch_jvl.write("appbase="+props.get("getdown_txt_appbase"))
976
977     copy {
978       from getdownLauncher
979       rename(file(getdownLauncher).getName(), getdown_launcher_new)
980       into project.ext.getdownWebsiteDir
981     }
982
983     copy {
984       from getdownLauncher
985       if (file(getdownLauncher).getName() != getdown_launcher) {
986         rename(file(getdownLauncher).getName(), getdown_launcher)
987       }
988       into project.ext.getdownWebsiteDir
989     }
990
991     if (! (CHANNEL.startsWith("ARCHIVE") || CHANNEL.startsWith("DEVELOP"))) {
992       copy {
993         from getdown_txt
994         from getdownLauncher
995         from "${getdownWebsiteDir}/${getdown_build_properties}"
996         if (file(getdownLauncher).getName() != getdown_launcher) {
997           rename(file(getdownLauncher).getName(), getdown_launcher)
998         }
999         into getdownInstallDir
1000       }
1001
1002       copy {
1003         from getdownInstallDir
1004         into getdownFilesInstallDir
1005       }
1006     }
1007
1008     copy {
1009       from getdown_txt
1010       from launch_jvl
1011       from getdownLauncher
1012       from "${getdownWebsiteDir}/${getdown_build_properties}"
1013       if (file(getdownLauncher).getName() != getdown_launcher) {
1014         rename(file(getdownLauncher).getName(), getdown_launcher)
1015       }
1016       into getdownFilesDir
1017     }
1018
1019     copy {
1020       from getdownResourceDir
1021       into "${project.ext.getdownFilesDir}/${getdown_resource_dir}"
1022     }
1023   }
1024
1025   if (buildDist) {
1026     inputs.dir("${jalviewDir}/${packageDir}")
1027   }
1028   outputs.dir(project.ext.getdownWebsiteDir)
1029   outputs.dir(project.ext.getdownFilesDir)
1030 }
1031
1032 task getdownDigest(type: JavaExec) {
1033   group = "distribution"
1034   description = "Digest the getdown website folder"
1035   dependsOn getdownWebsite
1036   doFirst {
1037     classpath = files("${getdownWebsiteDir}/${getdown_launcher}")
1038   }
1039   main = "com.threerings.getdown.tools.Digester"
1040   args project.ext.getdownWebsiteDir
1041   inputs.dir(project.ext.getdownWebsiteDir)
1042   outputs.file("${project.ext.getdownWebsiteDir}/digest2.txt")
1043 }
1044
1045 task getdown() {
1046   group = "distribution"
1047   description = "Create the minimal and full getdown app folder for installers and website and create digest file"
1048   dependsOn getdownDigest
1049   doLast {
1050     if (reportRsyncCommand) {
1051       def fromDir = getdownWebsiteDir + (getdownWebsiteDir.endsWith('/')?'':'/')
1052       def toDir = "${getdown_rsync_dest}/${getdownDir}" + (getdownDir.endsWith('/')?'':'/')
1053       println "LIKELY RSYNC COMMAND:"
1054       println "mkdir -p '$toDir'\nrsync -avh --delete '$fromDir' '$toDir'"
1055       if (RUNRSYNC == "true") {
1056         exec {
1057           commandLine "mkdir", "-p", toDir
1058         }
1059         exec {
1060           commandLine "rsync", "-avh", "--delete", fromDir, toDir
1061         }
1062       }
1063     }
1064   }
1065 }
1066
1067 clean {
1068   doFirst {
1069     delete project.ext.getdownWebsiteDir
1070     delete project.ext.getdownFilesDir
1071   }
1072 }
1073
1074 install4j {
1075   def install4jHomeDir = "/opt/install4j"
1076   def hostname = "hostname".execute().text.trim()
1077   if (hostname.equals("jv-bamboo")) {
1078     install4jHomeDir = System.getProperty("user.home")+"/buildtools/install4j"
1079   } else if (OperatingSystem.current().isMacOsX()) {
1080     install4jHomeDir = '/Applications/install4j.app/Contents/Resources/app'
1081     if (! file(install4jHomeDir).exists()) {
1082       install4jHomeDir = System.getProperty("user.home")+install4jHomeDir
1083     }
1084   } else if (OperatingSystem.current().isLinux()) {
1085     install4jHomeDir = System.getProperty("user.home")+"/buildtools/install4j"
1086   }
1087   installDir = file(install4jHomeDir)
1088   mediaTypes = Arrays.asList(install4jMediaTypes.split(","))
1089   if (install4jFaster.equals("true")) {
1090     faster = true
1091   }
1092 }
1093
1094 def install4jConf
1095 def macosJavaVMDir
1096 def macosJavaVMTgz
1097 def windowsJavaVMDir
1098 def windowsJavaVMTgz
1099 def install4jDir = "${jalviewDir}/${install4jResourceDir}"
1100 def install4jConfFile = "jalview-installers-java${JAVA_VERSION}.install4j"
1101 install4jConf = "${install4jDir}/${install4jConfFile}"
1102
1103 task copyInstall4jTemplate(type: Copy) {
1104   macosJavaVMDir = "${System.env.HOME}/buildtools/jre/openjdk-java_vm/getdown/macos-jre${JAVA_VERSION}/jre"
1105   macosJavaVMTgz = "${System.env.HOME}/buildtools/jre/openjdk-java_vm/install4j/tgz/macos-jre${JAVA_VERSION}.tar.gz"
1106   windowsJavaVMDir = "${System.env.HOME}/buildtools/jre/openjdk-java_vm/getdown/windows-jre${JAVA_VERSION}/jre"
1107   windowsJavaVMTgz = "${System.env.HOME}/buildtools/jre/openjdk-java_vm/install4j/tgz/windows-jre${JAVA_VERSION}.tar.gz"
1108   from (install4jDir) {
1109     include install4jTemplate
1110     rename (install4jTemplate, install4jConfFile)
1111     filter(ReplaceTokens, beginToken: '', endToken: '', tokens: ['9999999999': JAVA_VERSION])
1112     filter(ReplaceTokens, beginToken: '$$', endToken: '$$',
1113     tokens: [
1114     'JAVA_VERSION': JAVA_VERSION,
1115     'JAVA_INTEGER_VERSION': JAVA_INTEGER_VERSION,
1116     'VERSION': JALVIEW_VERSION,
1117     'MACOS_JAVA_VM_DIR': macosJavaVMDir,
1118     'MACOS_JAVA_VM_TGZ': macosJavaVMTgz,
1119     'WINDOWS_JAVA_VM_DIR': windowsJavaVMDir,
1120     'WINDOWS_JAVA_VM_TGZ': windowsJavaVMTgz,
1121     'INSTALL4JINFOPLISTFILEASSOCIATIONS': install4jInfoPlistFileAssociations,
1122     'COPYRIGHT_MESSAGE': install4jCopyrightMessage,
1123     'MACOS_BUNDLE_ID': install4jMacOSBundleId,
1124     'GETDOWN_RESOURCE_DIR': getdown_resource_dir,
1125     'GETDOWN_DIST_DIR': getdown_app_dir,
1126     'GETDOWN_ALT_DIR': getdown_app_dir_alt,
1127     'GETDOWN_INSTALL_DIR': getdown_install_dir
1128     ]
1129     )
1130     if (OSX_KEYPASS=="") {
1131       filter(ReplaceTokens, beginToken: 'codeSigning macEnabled="', endToken: '"', tokens: ['true':'codeSigning macEnabled="false"'])
1132       filter(ReplaceTokens, beginToken: 'runPostProcessor="true" ',endToken: 'Processor', tokens: ['post':'runPostProcessor="false" postProcessor'])
1133     }
1134   }
1135   into install4jDir
1136   outputs.files(install4jConf)
1137
1138   doLast {
1139     // include file associations in installer
1140     def installerFileAssociationsXml = file("${install4jDir}/${install4jInstallerFileAssociations}").text
1141     ant.replaceregexp(
1142       byline: false,
1143       flags: "s",
1144       match: '<action name="EXTENSIONS_REPLACED_BY_GRADLE".*?</action>',
1145       replace: installerFileAssociationsXml,
1146       file: install4jConf
1147     )
1148     /*
1149     // include uninstaller applescript app files in dmg
1150     def installerDMGUninstallerXml = file("$install4jDir/$install4jDMGUninstallerAppFiles").text
1151     ant.replaceregexp(
1152     byline: false,
1153     flags: "s",
1154     match: '<file name="UNINSTALL_OLD_JALVIEW_APP_REPLACED_IN_GRADLE" file=.*?>',
1155     replace: installerDMGUninstallerXml,
1156     file: install4jConf
1157     )
1158      */
1159   }
1160 }
1161
1162 task installers(type: com.install4j.gradle.Install4jTask) {
1163   group = "distribution"
1164   description = "Create the install4j installers"
1165   dependsOn getdown
1166   dependsOn copyInstall4jTemplate
1167   projectFile = file(install4jConf)
1168   variables = [majorVersion: version.substring(2, 11), build: 001, OSX_KEYSTORE: OSX_KEYSTORE, JSIGN_SH: JSIGN_SH]
1169   destination = "${jalviewDir}/${install4jBuildDir}/${JAVA_VERSION}"
1170   buildSelected = true
1171
1172   if (OSX_KEYPASS) {
1173     macKeystorePassword=OSX_KEYPASS
1174   }
1175
1176   doFirst {
1177     println("Using projectFile "+projectFile)
1178   }
1179
1180   inputs.dir(project.ext.getdownWebsiteDir)
1181   inputs.file(install4jConf)
1182   inputs.dir(macosJavaVMDir)
1183   inputs.dir(windowsJavaVMDir)
1184   outputs.dir("${jalviewDir}/${install4jBuildDir}/${JAVA_VERSION}")
1185 }
1186
1187 clean {
1188   doFirst {
1189     delete install4jConf
1190   }
1191 }
1192
1193 task sourceDist (type: Tar) {
1194   
1195   def VERSION_UNDERSCORES = JALVIEW_VERSION.replaceAll("\\.", "_")
1196   def outputFileName = "${project.name}_${VERSION_UNDERSCORES}.tar.gz"
1197   // cater for buildship < 3.1 [3.0.1 is max version in eclipse 2018-09]
1198   try {
1199     archiveFileName = outputFileName
1200   } catch (Exception e) {
1201     archiveName = outputFileName
1202   }
1203   
1204   compression Compression.GZIP
1205   
1206   into project.name
1207
1208   def EXCLUDE_FILES=["build/*","bin/*","test-output/","test-reports","tests","clover*/*"
1209   ,".*"
1210   ,"benchmarking/*"
1211   ,"**/.*"
1212   ,"*.class"
1213   ,"**/*.class","${j11modDir}/**/*.jar","appletlib","**/*locales"
1214   ,"*locales/**",
1215   ,"utils/InstallAnywhere"] 
1216   def PROCESS_FILES=[   "AUTHORS",
1217   "CITATION",
1218   "FEATURETODO",
1219   "JAVA-11-README",
1220   "FEATURETODO",
1221   "LICENSE",
1222   "**/README",
1223   "RELEASE",
1224   "THIRDPARTYLIBS","TESTNG",
1225   "build.gradle",
1226   "gradle.properties",
1227   "**/*.java",
1228   "**/*.html",
1229   "**/*.xml",
1230   "**/*.gradle",
1231   "**/*.groovy",
1232   "**/*.properties",
1233   "**/*.perl",
1234   "**/*.sh"]
1235
1236   from(jalviewDir) {
1237     exclude (EXCLUDE_FILES)
1238     include (PROCESS_FILES)
1239     filter(ReplaceTokens, beginToken: '$$', endToken: '$$', tokens: ['Version-Rel': JALVIEW_VERSION,'Year-Rel': getDate("yyyy")])
1240   }
1241   from(jalviewDir) {
1242     exclude (EXCLUDE_FILES)
1243     exclude (PROCESS_FILES)
1244   exclude ("appletlib")
1245   exclude ("**/*locales")
1246   exclude ("*locales/**")
1247   exclude ("utils/InstallAnywhere")
1248
1249     exclude (getdown_files_dir)
1250   exclude (getdown_website_dir)
1251
1252   // exluding these as not using jars as modules yet
1253   exclude ("${j11modDir}/**/*.jar")
1254 }
1255 //  from (jalviewDir) {
1256 //    // explicit includes for stuff that seemed to not get included
1257 //    include(fileTree("test/**/*."))
1258 //    exclude(EXCLUDE_FILES)
1259 //    exclude(PROCESS_FILES)
1260 //  }
1261 }
1262
1263 task helppages  {
1264   dependsOn copyHelp
1265   dependsOn pubhtmlhelp
1266   
1267   inputs.dir("${classes}/${helpDir}")
1268   outputs.dir("${helpOutputDir}")
1269 }
1270
1271 def jalviewjsBuildDir
1272 def jalviewjsSiteDir
1273 def jalviewjsTransferSiteDir
1274 task jalviewjsSitePath {
1275   def relativeBuildDir = file(jalviewDirAbsolutePath).toPath().relativize(buildDir.toPath())
1276   jalviewjsBuildDir = "${relativeBuildDir}/jalviewjs"
1277   jalviewjsSiteDir = "${jalviewjsBuildDir}/${jalviewjs_site_dir}"
1278   jalviewjsTransferSiteDir = "${jalviewjsBuildDir}/tmp/site"
1279 }
1280
1281 def eclipseWorkspace
1282 task jalviewjsSetEclipseWorkspace {
1283   def propKey = "jalviewjs_eclipse_workspace"
1284   def propVal = null
1285   if (project.hasProperty(propKey)) {
1286     propVal = project.getProperty(propKey)
1287     if (propVal.startsWith("~/")) {
1288       propVal = System.getProperty("user.home") + propVal.substring(1)
1289     }
1290   }
1291   def propsFileName = "${jalviewDirAbsolutePath}/${jalviewjsBuildDir}/${jalviewjs_eclipse_workspace_location_file}"
1292   def propsFile = file(propsFileName)
1293   def eclipseWsDir = propVal
1294   def props = new Properties()
1295
1296   if (( eclipseWsDir == null || !file(eclipseWsDir).exists() ) && propsFile.exists()) {
1297     def ins = new FileInputStream(propsFileName)
1298     props.load(ins)
1299     ins.close()
1300     if (props.getProperty(propKey, null) != null) {
1301       eclipseWsDir = props.getProperty(propKey)
1302     }
1303   }
1304
1305   def writeProps = false
1306   if (eclipseWsDir == null || !file(eclipseWsDir).exists()) {
1307     def tempDir = File.createTempDir()
1308     eclipseWsDir = tempDir.getAbsolutePath()
1309     writeProps = true
1310   }
1311   eclipseWorkspace = file(eclipseWsDir)
1312
1313   doFirst {
1314     if (writeProps) {
1315       props.setProperty(propKey, eclipseWsDir)
1316       propsFile.parentFile.mkdirs()
1317       def bytes = new ByteArrayOutputStream()
1318       props.store(bytes, null)
1319       def propertiesString = bytes.toString()
1320       propsFile.text = propertiesString
1321     }
1322
1323     println("ECLIPSE WORKSPACE: "+eclipseWorkspace.getPath())
1324   }
1325
1326   inputs.property(propKey, eclipseWsDir)
1327   outputs.file(propsFileName)
1328 }
1329
1330
1331 def eclipseBinary
1332 def eclipseVersion
1333 def eclipseDebug = false
1334 def eclipseVersionSuffix = ""
1335 task jalviewjsEclipsePaths {
1336   def eclipseProduct
1337
1338   def eclipseRoot = jalviewjs_eclipse_root
1339   if (eclipseRoot.startsWith("~/")) {
1340     eclipseRoot = System.getProperty("user.home") + eclipseRoot.substring(1)
1341   }
1342   if (OperatingSystem.current().isMacOsX()) {
1343     eclipseRoot += "/Eclipse.app"
1344     eclipseBinary = "${eclipseRoot}/Contents/MacOS/eclipse"
1345     eclipseProduct = "${eclipseRoot}/Contents/Eclipse/.eclipseproduct"
1346   } else if (OperatingSystem.current().isWindows()) { // check these paths!!
1347     if (file("${eclipseRoot}/eclipse").isDirectory() && file("${eclipseRoot}/eclipse/.eclipseproduct").exists()) {
1348       eclipseRoot += "/eclipse"
1349     }
1350     eclipseBinary = "${eclipseRoot}/eclipse"
1351     eclipseProduct = "${eclipseRoot}/.eclipseproduct"
1352   } else { // linux or unix
1353     if (file("${eclipseRoot}/eclipse").isDirectory() && file("${eclipseRoot}/eclipse/.eclipseproduct").exists()) {
1354       eclipseRoot += "/eclipse"
1355     }
1356     eclipseBinary = "${eclipseRoot}/eclipse"
1357     eclipseProduct = "${eclipseRoot}/.eclipseproduct"
1358   }
1359
1360   eclipseVersion = "4.12" // default
1361   def assumedVersion = true
1362   if (file(eclipseProduct).exists()) {
1363     def fis = new FileInputStream(eclipseProduct)
1364     def props = new Properties()
1365     props.load(fis)
1366     eclipseVersion = props.getProperty("version")
1367     fis.close()
1368     assumedVersion = false
1369   }
1370   /*
1371   String[] v = eclipseVersion.split("\\.")
1372   def v0 = Integer.valueOf(v[0])
1373   def v1 = Integer.valueOf(v[1])
1374   if (v0 < 4 || ( v0 == 4 && v1 < 13 )) {
1375     eclipseVersionSuffix = "_4.12"
1376   } else {
1377     eclipseVersionSuffix = "_4.13"
1378   }
1379   */
1380
1381   
1382   def propKey = "eclipse_debug"
1383   eclipseDebug = (project.hasProperty(propKey) && project.getProperty(propKey).equals("true"))
1384
1385   doFirst {
1386     if (!assumedVersion) {
1387       println("ECLIPSE VERSION=${eclipseVersion}")
1388     }
1389   }
1390 }
1391
1392 task eclipseSetup {
1393   dependsOn eclipseProject
1394   dependsOn eclipseClasspath
1395   dependsOn eclipseJdt
1396 }
1397
1398 /* using the Copy task below
1399 task OLDjalviewjsEclipseCopyDropins {
1400   dependsOn jalviewjsEclipsePaths
1401   dependsOn jalviewjsCleanEclipse
1402   def inputFiles = fileTree(dir: "${jalviewDir}/${jalviewjs_eclipse_dropins_dir}", include: "*.jar")
1403   inputFiles += file(jalviewjs_j2s_plugin)
1404   def outputDir = "${buildDir}/jalviewjs
1405
1406   inputs.files inputFiles
1407   inputFiles.each { file ->
1408     outputs.file("${outputDir}/${file.name}")
1409   }
1410
1411   doLast {
1412     inputFiles.each { file ->
1413       copy {
1414         from file
1415         into outputDir
1416       }
1417     }
1418   }
1419 }
1420 */
1421
1422 // this version (type: Copy) will delete anything in the eclipse dropins folder that isn't in fromDropinsDir
1423 task jalviewjsEclipseCopyDropins(type: Copy) {
1424   dependsOn jalviewjsEclipsePaths
1425
1426   def inputFiles = fileTree(dir: "${jalviewDir}/${jalviewjs_eclipse_dropins_dir}", include: "*.jar")
1427   inputFiles += file("${jalviewDir}/${jalviewjs_j2s_plugin}")
1428   def outputDir = "${jalviewDir}/${jalviewjsBuildDir}/${jalviewjs_eclipse_tmp_dropins_dir}"
1429
1430   from inputFiles
1431   into outputDir
1432 }
1433
1434 // this eclipse -clean doesn't actually work
1435 task jalviewjsCleanEclipse(type: Exec) {
1436   dependsOn eclipseSetup
1437   dependsOn jalviewjsEclipsePaths
1438   dependsOn jalviewjsEclipseCopyDropins
1439
1440   executable(eclipseBinary)
1441   args(["-nosplash", "--launcher.suppressErrors", "-data", eclipseWorkspace.getPath(), "-clean", "-console", "-consoleLog"])
1442   if (eclipseDebug) {
1443     args += "-debug"
1444   }
1445   args += "-l"
1446
1447   def inputString = """exit
1448 y
1449 """
1450   def inputByteStream = new ByteArrayInputStream(inputString.getBytes())
1451   standardInput = inputByteStream
1452 }
1453 /* not really working yet
1454 jalviewjsEclipseCopyDropins.finalizedBy jalviewjsCleanEclipse
1455 */
1456
1457 task jalviewjsUnzipFiles {
1458   dependsOn jalviewjsSitePath
1459
1460   def zipFiles = fileTree(dir: "${jalviewDir}/${jalviewjs_libjs_dir}", include: "*.zip")
1461   zipFiles += "${jalviewDir}/${jalviewjs_swingjs_zip}"
1462
1463   doLast {
1464     zipFiles.each { file_zip -> 
1465       copy {
1466         from zipTree(file_zip)
1467         into "${jalviewDir}/${jalviewjsSiteDir}"
1468       }
1469     }
1470   }
1471
1472   inputs.files zipFiles
1473   outputs.dir "${jalviewDir}/${jalviewjsSiteDir}"
1474 }
1475
1476 task jalviewjsCreateJ2sSettings(type: WriteProperties) {
1477   group "JalviewJS"
1478   description "Create the .j2s file from the j2s.* properties"
1479
1480   dependsOn jalviewjsSitePath
1481
1482   outputFile ("${jalviewDir}/${jalviewjs_j2s_settings}")
1483   def j2s_props = project.properties.findAll { it.key.startsWith("j2s.") }.sort { it.key }
1484   def siteDirProperty = "j2s.site.directory"
1485   def setSiteDir = false
1486   j2s_props.each { prop, val ->
1487     if (val != null) {
1488       if (prop == siteDirProperty) {
1489         if (!(val.startsWith('/') || val.startsWith("file://") )) {
1490           val = "${jalviewDir}/${jalviewjsTransferSiteDir}/${val}"
1491         }
1492         setSiteDir = true
1493       }
1494       property(prop,val)
1495     }
1496     if (!setSiteDir) { // default site location, don't override specifically set property
1497       property(siteDirProperty,"${jalviewDir}/${jalviewjsTransferSiteDir}")
1498     }
1499   }
1500   inputs.properties(j2s_props)
1501   outputs.file(outputFile)
1502 }
1503
1504 task jalviewjsEclipseSetup {
1505   dependsOn jalviewjsEclipseCopyDropins
1506   dependsOn jalviewjsSetEclipseWorkspace
1507   dependsOn jalviewjsCreateJ2sSettings
1508 }
1509
1510 task jalviewjsCopyResources (type: Sync) {
1511   dependsOn jalviewjsSitePath
1512   def inputFiles = fileTree(dir: "${jalviewDir}/${resourceDir}")
1513   def outputDir = "${jalviewDir}/${jalviewjsSiteDir}/${jalviewjs_j2s_subdir}"
1514
1515   from inputFiles
1516   into outputDir
1517   def outputFiles = []
1518   rename { filename ->
1519     outputFiles += "${outputDir}/${filename}"
1520     null
1521   }
1522   preserve {
1523     include "**"
1524   }
1525   outputs.files outputFiles
1526   inputs.files inputFiles
1527 }
1528
1529 task jalviewjsCopySiteResources (type: Sync) {
1530   dependsOn jalviewjsSitePath
1531   def inputFiles = fileTree(dir: "${jalviewDir}/${jalviewjs_site_resource_dir}")
1532   def outputDir = "${jalviewDir}/${jalviewjsSiteDir}"
1533
1534   from inputFiles
1535   into outputDir
1536   def outputFiles = []
1537   rename { filename ->
1538     outputFiles += "${outputDir}/${filename}"
1539     null
1540   }
1541   preserve {
1542     include "**"
1543   }
1544   outputs.files outputFiles
1545   inputs.files inputFiles
1546 }
1547
1548 task jalviewjsProjectImport(type: Exec) {
1549   dependsOn eclipseSetup
1550   dependsOn jalviewjsEclipsePaths
1551   dependsOn jalviewjsEclipseSetup
1552
1553   def projdir = eclipseWorkspace.getPath()+"/.metadata/.plugins/org.eclipse.core.resources/.projects/jalview/org.eclipse.jdt.core"
1554   executable(eclipseBinary)
1555   args(["-nosplash", "--launcher.suppressErrors", "-application", "com.seeq.eclipse.importprojects.headlessimport", "-data", eclipseWorkspace.getPath(), "-import", jalviewDirAbsolutePath])
1556   if (eclipseDebug) {
1557     args += "-debug"
1558   }
1559   args += [ "--launcher.appendVmargs", "-vmargs", "-Dorg.eclipse.equinox.p2.reconciler.dropins.directory=${jalviewDirAbsolutePath}/${jalviewjsBuildDir}/${jalviewjs_eclipse_tmp_dropins_dir}" ]
1560
1561   inputs.file("${jalviewDir}/.project")
1562   outputs.upToDateWhen { file(projdir).exists() }
1563 }
1564
1565 task jalviewjsTranspile(type: Exec) {
1566   dependsOn jalviewjsEclipseSetup 
1567   dependsOn jalviewjsProjectImport
1568   dependsOn jalviewjsEclipsePaths
1569
1570   executable(eclipseBinary)
1571   args(["-nosplash", "--launcher.suppressErrors", "-application", "org.eclipse.jdt.apt.core.aptBuild", "-data", eclipseWorkspace, "-${jalviewjs_eclipse_build_arg}", eclipse_project_name ])
1572   if (eclipseDebug) {
1573     args += "-debug"
1574   }
1575   args += [ "--launcher.appendVmargs", "-vmargs", "-Dorg.eclipse.equinox.p2.reconciler.dropins.directory=${jalviewDirAbsolutePath}/${jalviewjsBuildDir}/${jalviewjs_eclipse_tmp_dropins_dir}" ]
1576
1577   def stdout
1578   def stderr
1579   doFirst {
1580     stdout = new ByteArrayOutputStream()
1581     stderr = new ByteArrayOutputStream()
1582
1583     def logOutFileName = "${jalviewDirAbsolutePath}/${jalviewjsBuildDir}/${jalviewjs_j2s_stdout}"
1584     def logOutFile = file(logOutFileName)
1585     logOutFile.createNewFile()
1586     logOutFile.text = """ROOT: ${jalviewjs_eclipse_root}
1587 BINARY: ${eclipseBinary}
1588 VERSION: ${eclipseVersion}
1589 WORKSPACE: ${eclipseWorkspace}
1590 DEBUG: ${eclipseDebug}
1591 ----
1592 """
1593     def logOutFOS = new FileOutputStream(logOutFile, true) // true == append
1594     //def logErrFileName = "${jalviewDir}/${jalviewjsBuildDir}/${jalviewjs_j2s_stderr}"
1595     //def logErrFile = file(logFileName)
1596     //logErrFile.createNewFile()
1597     //def logErrFOS = new FileErrputStream(logErrFile, false)
1598     // combine stdout and stderr
1599     def logErrFOS = logOutFOS
1600
1601     if (jalviewjs_j2s_to_console.equals("true")) {
1602       standardOutput = new org.apache.tools.ant.util.TeeOutputStream(
1603         new org.apache.tools.ant.util.TeeOutputStream(
1604           logOutFOS,
1605           stdout),
1606         standardOutput)
1607       errorOutput = new org.apache.tools.ant.util.TeeOutputStream(
1608         new org.apache.tools.ant.util.TeeOutputStream(
1609           logErrFOS,
1610           stderr),
1611         errorOutput)
1612     } else {
1613       standardOutput = new org.apache.tools.ant.util.TeeOutputStream(
1614         logOutFOS,
1615         stdout)
1616       errorOutput = new org.apache.tools.ant.util.TeeOutputStream(
1617         logErrFOS,
1618         stderr)
1619     }
1620   }
1621
1622   doLast {
1623     if (stdout.toString().contains("Error processing ")) {
1624       // j2s did not complete transpile
1625       throw new GradleException("Error during transpilation:\n${stderr}\nSee eclipse transpile log file '${jalviewDir}/${jalviewjsBuildDir}/${jalviewjs_j2s_stdout}'")
1626     }
1627   }
1628
1629   inputs.dir("${jalviewDir}/${sourceDir}")
1630   outputs.dir("${jalviewDir}/${jalviewjsTransferSiteDir}")
1631   outputs.upToDateWhen( { file("${jalviewDir}/${jalviewjsTransferSiteDir}${jalviewjs_server_resource}").exists() } )
1632 }
1633
1634 task jalviewjsCopyTransferSite(type: Copy) {
1635   dependsOn jalviewjsTranspile
1636   from "${jalviewDir}/${jalviewjsTransferSiteDir}"
1637   into "${jalviewDir}/${jalviewjsSiteDir}"
1638 }
1639
1640 jalviewjsUnzipFiles.mustRunAfter jalviewjsCopyTransferSite
1641 jalviewjsCopyResources.mustRunAfter jalviewjsCopyTransferSite
1642 jalviewjsCopySiteResources.mustRunAfter jalviewjsCopyTransferSite
1643
1644 task jalviewjsPrepareSite {
1645   group "JalviewJS"
1646   description "Prepares the website folder including unzipping files and copying resources"
1647   dependsOn jalviewjsSitePath
1648   dependsOn jalviewjsUnzipFiles
1649   dependsOn jalviewjsCopyResources
1650   dependsOn jalviewjsCopySiteResources
1651 }
1652
1653 task jalviewjsBuildSite {
1654   group "JalviewJS"
1655   description "Builds the whole website including transpiled code"
1656   dependsOn jalviewjsCopyTransferSite
1657   dependsOn jalviewjsPrepareSite
1658 }
1659
1660 task cleanJalviewjsSite {
1661   doFirst {
1662     delete "${jalviewDir}/${jalviewjsTransferSiteDir}"
1663     delete "${jalviewDir}/${jalviewjsSiteDir}"
1664   }
1665 }
1666
1667 task jalviewjsSiteTar(type: Tar) {
1668   group "JalviewJS"
1669   description "Creates a tar.gz file for the website"
1670   dependsOn jalviewjsBuildSite
1671   def outputFilename = "jalviewjs-site-${JALVIEW_VERSION}.tar.gz"
1672   try {
1673     archiveFileName = outputFilename
1674   } catch (Exception e) {
1675     archiveName = outputFilename
1676   }
1677
1678   compression Compression.GZIP
1679
1680   from "${jalviewDir}/${jalviewjsSiteDir}"
1681   into jalviewjs_site_dir // this is inside the tar file
1682
1683   inputs.dir("${jalviewDir}/${jalviewjsSiteDir}")
1684 }
1685
1686 task jalviewjsServer {
1687   group "JalviewJS"
1688   description "Starts a webserver on localhost to test the website"
1689   dependsOn jalviewjsSitePath
1690   def htmlFile = "${jalviewDirAbsolutePath}/jalviewjsTest.html"
1691   doLast {
1692
1693     SimpleHttpFileServerFactory factory = new SimpleHttpFileServerFactory()
1694     def port = Integer.valueOf(jalviewjs_server_port)
1695     def start = port
1696     def running = false
1697     def url
1698     while(port < start+1000 && !running) {
1699       try {
1700         def doc_root = new File("${jalviewDirAbsolutePath}/${jalviewjsSiteDir}")
1701         def jalviewjsServer = factory.start(doc_root, port)
1702         running = true
1703         url = jalviewjsServer.getResourceUrl(jalviewjs_server_resource)
1704         println("SERVER STARTED with document root ${doc_root}.")
1705         println("Go to "+url+" . Run  gradle --stop  to stop (kills all gradle daemons).")
1706         println("For debug: "+url+"?j2sdebug")
1707
1708         file(htmlFile).text = """
1709         <p><a href="${url}">Jalview JS Test. &lt;${url}&gt;</a></p>
1710         <p><a href="${url}?j2sdebug">Jalview JS Test with debug. &lt;${url}?j2sdebug&lt;</a></p>
1711         """
1712
1713       } catch (Exception e) {
1714         port++;
1715       }
1716     }
1717
1718   }
1719
1720   outputs.file(htmlFile)
1721   outputs.upToDateWhen({false})
1722 }
1723
1724 task cleanJalviewjsAll {
1725   group "JalviewJS"
1726   description "Delete all configuration and build artifacts to do with JalviewJS build"
1727   dependsOn cleanJalviewjsSite
1728   dependsOn jalviewjsEclipsePaths
1729   
1730   doFirst {
1731     delete "${jalviewDir}/${jalviewjsBuildDir}"
1732     delete "${jalviewDir}/${eclipse_bin_dir}"
1733     if (eclipseWorkspace != null && file(eclipseWorkspace.getAbsolutePath()+"/.metadata").exists()) {
1734       delete file(eclipseWorkspace.getAbsolutePath()+"/.metadata")
1735     }
1736     delete "${jalviewDir}/${jalviewjs_j2s_settings}"
1737   }
1738
1739   outputs.upToDateWhen( { false } )
1740 }
1741
1742 task jalviewjs {
1743   group "JalviewJS"
1744   description "Build the site"
1745   dependsOn jalviewjsBuildSite
1746 }
1747
1748
1749 task jalviewjsIDECopyTransferSite(type: Copy) {
1750   from "${jalviewDir}/${jalviewjsTransferSiteDir}"
1751   into "${jalviewDir}/${jalviewjsSiteDir}"
1752 }
1753
1754 task jalviewjsIDEj2s {
1755   group "JalviewJS in Eclipse"
1756   description "Creates the .j2s file"
1757   dependsOn jalviewjsCreateJ2sSettings
1758 }
1759
1760 task jalviewjsIDEBuildSite {
1761   group "JalviewJS in Eclipse"
1762   description "Copies the Eclipse transpiled site and unzips supporting zipfiles"
1763   dependsOn jalviewjsIDECopyTransferSite
1764   dependsOn jalviewjsPrepareSite
1765 }
1766
1767 task jalviewjsIDESiteClean {
1768   group "JalviewJS in Eclipse"
1769   description "Deletes the Eclipse transpiled site"
1770   dependsOn cleanJalviewjsSite
1771 }
1772
1773 task jalviewjsIDEServer {
1774   group "JalviewJS in Eclipse"
1775   description "Starts a webserver on localhost to test the website"
1776   dependsOn jalviewjsServer
1777 }
1778