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