JAL-3295 Can now compile/run/debug in Eclipse with Java 1.8. Change JAVA_VERSION...
[jalview.git] / build.gradle
1 import org.apache.tools.ant.filters.ReplaceTokens
2 //import org.apache.tools.ant.filters.ReplaceRegexp
3 import org.gradle.internal.os.OperatingSystem
4 import org.gradle.plugins.ide.eclipse.model.*
5
6
7 import groovy.transform.ExternalizeMethods
8
9 buildscript {
10   dependencies {
11     classpath 'org.openclover:clover:4.3.1'
12     classpath 'org.apache.commons:commons-compress:1.18'
13   }
14 }
15
16 plugins {
17   id 'java'
18   id 'application'
19   id 'eclipse'
20   id 'com.github.johnrengelman.shadow' version '4.0.3'
21   id 'com.install4j.gradle' version '7.0.9'
22 }
23
24 repositories {
25   jcenter()
26   mavenCentral()
27   mavenLocal()
28   flatDir {
29     dirs gradlePluginsDir
30   }
31 }
32
33 mainClassName = launcherClass
34 def cloverInstrDir = file("$buildDir/$cloverSourcesInstrDir")
35 def classes = "$jalviewDir/$classesDir"
36
37 if (clover.equals("true")) {
38   use_clover = true
39   classes = "$buildDir/$cloverClassesDir"
40 } else {
41   use_clover = false
42   classes = "$jalviewDir/$classesDir"
43 }
44
45 // configure classpath/args for j8/j11 compilation
46
47 def jalviewDirAbsolutePath = file(jalviewDir).getAbsolutePath()
48 def libDir
49 def libDistDir
50 def compile_source_compatibility
51 def compile_target_compatibility
52
53 ext {
54   // where the getdown channel will be built.
55   // TODO: consider allowing this expression to  be overridden by -P arg
56   getdownWebsiteDir = jalviewDir + '/' + getdown_website_dir + '/' + JAVA_VERSION
57   getdownAppDir = getdownWebsiteDir + '/' + getdown_app_dir
58   //getdownJ11libDir = getdownWebsiteDir + '/' + getdown_j11lib_dir
59   getdownResourceDir = getdownWebsiteDir + '/' + getdown_resource_dir
60   getdownLauncher = jalviewDir + '/' + getdown_launcher
61   getdownFilesDir = jalviewDir + '/' + getdown_files_dir + '/' + JAVA_VERSION + '/'
62   getdown_app_base = getdown_channel_base+"/"+getdown_channel_name+"/"+JAVA_VERSION+"/"
63   modules_compileClasspath = fileTree(dir: "$jalviewDir/$j11modDir", include: ["*.jar"])
64   modules_runtimeClasspath = modules_compileClasspath
65   gitHash = ""
66   gitBranch = ""
67 }
68
69 def JAVA_INTEGER_VERSION
70 def additional_compiler_args = []
71 // these are getdown.txt properties defined dependent on the JAVA_VERSION
72 def getdown_alt_java_min_version
73 // this property is assigned below and expanded to multiple lines in the getdown task
74 def getdown_alt_multi_java_location
75 // this property is for the Java library used in eclipse
76 def eclipse_java_runtime_name
77 if (JAVA_VERSION.equals("1.8")) {
78   JAVA_INTEGER_VERSION = "8"
79   //libDir = j8libDir
80   libDir = j11libDir
81   libDistDir = j8libDir
82   compile_source_compatibility = 1.8
83   compile_target_compatibility = 1.8
84   getdown_alt_java_min_version = getdown_alt_java8_min_version
85   getdown_alt_multi_java_location = getdown_alt_java8_txt_multi_java_location
86   eclipse_java_runtime_name = "JavaSE-1.8"
87 } else if (JAVA_VERSION.equals("11")) {
88   JAVA_INTEGER_VERSION = "11"
89   libDir = j11libDir
90   libDistDir = j11libDir
91   compile_source_compatibility = 11
92   compile_target_compatibility = 11
93   getdown_alt_java_min_version = getdown_alt_java11_min_version
94   getdown_alt_multi_java_location = getdown_alt_java11_txt_multi_java_location
95   eclipse_java_runtime_name = "JavaSE-11"
96   additional_compiler_args += [
97     '--module-path', ext.modules_compileClasspath.asPath,
98     '--add-modules', j11modules
99   ]
100 } else {
101   throw new GradleException("JAVA_VERSION=$JAVA_VERSION not currently supported by Jalview")
102 }
103
104 sourceSets {
105
106   main {
107     java {
108       srcDirs "$jalviewDir/$sourceDir"
109       outputDir = file("$classes")
110     }
111
112     resources {
113       srcDirs "$jalviewDir/$resourceDir"
114     }
115
116     jar.destinationDir = file("$jalviewDir/$packageDir")
117
118     compileClasspath = files(sourceSets.main.java.outputDir)
119     compileClasspath += fileTree(dir: "$jalviewDir/$libDir", include: ["*.jar"])
120
121     runtimeClasspath = compileClasspath
122   }
123   clover {
124     java {
125       srcDirs = [ cloverInstrDir ]
126       outputDir = file("${buildDir}/${cloverClassesDir}")
127     }
128
129     resources {
130       srcDirs = sourceSets.main.resources.srcDirs
131     }
132     compileClasspath = configurations.cloverRuntime + files( sourceSets.clover.java.outputDir )
133     compileClasspath += files(sourceSets.main.java.outputDir)
134     compileClasspath += sourceSets.main.compileClasspath
135     compileClasspath += fileTree(dir: "$jalviewDir/$utilsDir", include: ["**/*.jar"])
136     compileClasspath += fileTree(dir: "$jalviewDir/$libDir", include: ["*.jar"])
137
138     runtimeClasspath = compileClasspath
139   }
140
141   test {
142     java {
143       srcDirs "$jalviewDir/$testSourceDir"
144       outputDir = file("$jalviewDir/$testOutputDir")
145     }
146
147     resources {
148       srcDirs = sourceSets.main.resources.srcDirs
149     }
150
151     compileClasspath = files( sourceSets.test.java.outputDir )
152
153     if (use_clover) {
154       compileClasspath += sourceSets.clover.compileClasspath
155     } else {
156       compileClasspath += files(sourceSets.main.java.outputDir)
157     }
158     
159     compileClasspath += fileTree(dir: "$jalviewDir/$utilsDir", include: ["**/*.jar"])
160     compileClasspath += fileTree(dir: "$jalviewDir/$libDir", include: ["*.jar"])
161
162     runtimeClasspath = compileClasspath
163   }
164 }
165
166 // clover bits
167 dependencies {
168   if (use_clover) {
169     cloverCompile 'org.openclover:clover:4.3.1'
170     testCompile 'org.openclover:clover:4.3.1'
171   }
172 }
173
174 configurations {
175   cloverRuntime
176   cloverRuntime.extendsFrom cloverCompile
177 }
178
179 eclipse {
180   project {
181     name = "Jalview with gradle build"
182
183     natures 'org.eclipse.jdt.core.javanature',
184         'org.eclipse.jdt.groovy.core.groovyNature',
185         'org.eclipse.buildship.core.gradleprojectnature'
186
187     buildCommand 'org.eclipse.jdt.core.javabuilder'
188     buildCommand 'org.eclipse.buildship.core.gradleprojectbuilder'
189   }
190
191   classpath {
192     //defaultOutputDir = sourceSets.main.java.outputDir
193     def removeThese = []
194     configurations.each{ if (it.isCanBeResolved()) {
195         removeThese += it
196       }
197     }
198
199     minusConfigurations += removeThese
200     plusConfigurations = [ ]
201     file {
202
203       whenMerged { cp ->
204         def removeTheseToo = []
205         HashMap<String, Boolean> addedSrcPath = new HashMap<>();
206         cp.entries.each { entry ->
207           if (entry.kind == 'src') {
208             if (addedSrcPath.getAt(entry.path) || !(entry.path == "src" || entry.path == "test")) {
209               removeTheseToo += entry
210             } else {
211               addedSrcPath.putAt(entry.path, true)
212             }
213           }
214         }
215         cp.entries.removeAll(removeTheseToo)
216         
217         print ("CP="+cp.inspect())
218         
219         cp.entries += new Output("bin/main")
220         cp.entries += new Library(fileReference(helpParentDir))
221         cp.entries += new Library(fileReference(resourceDir))
222         
223         HashMap<String, Boolean> addedLibPath = new HashMap<>();
224         
225         // changing from sourcesets.main.classpath to specific Java version lib
226         //sourceSets.main.compileClasspath.each{
227         fileTree("$jalviewDir/$libDistDir").include("**/*.jar").include("*.jar").each {
228           //don't want to add outputDir as eclipse is using its own output dir in bin/main
229           if (it.isDirectory() || ! it.exists()) {
230             // don't add dirs to classpath
231             return
232           }
233           def itPath = it.toString()
234           if (itPath.startsWith(jalviewDirAbsolutePath+"/")) {
235             itPath = itPath.substring(jalviewDirAbsolutePath.length()+1)
236           }
237           if (addedLibPath.get(itPath)) {
238             //println("Not adding duplicate entry "+itPath)
239           } else {
240             //println("Adding entry "+itPath)
241             cp.entries += new Library(fileReference(itPath))
242             addedLibPath.put(itPath, true)
243           }
244         }
245
246         // changing from sourcesets.main.classpath to specific Java version lib
247         //sourceSets.test.compileClasspath.each{
248         fileTree(dir: "$jalviewDir/$utilsDir", include: ["**/*.jar"]).each {
249           //if ((it.isDirectory() || ! it.exists()) && ! (it.equals(sourceSets.main.java.outputDir))) {
250           //no longer want to add outputDir as eclipse is using its own output dir in bin/main
251           if (it.isDirectory() || ! it.exists()) {
252             // don't add dirs to classpath
253             return false // groovy "break" in .each loop
254           }
255           def itPath = it.toString()
256           if (itPath.startsWith(jalviewDirAbsolutePath+"/")) {
257             itPath = itPath.substring(jalviewDirAbsolutePath.length()+1)
258           }
259           if (addedLibPath.get(itPath)) {
260             // don't duplicate
261           } else {
262             def lib = new Library(fileReference(itPath))
263             // this doesn't work... yet.  Adding test=true attribute using withXml below
264             //def attrs = new Node(null, 'attributes', ["test":"true"])
265             //lib.appendNode(attrs) //
266             cp.entries += lib
267             addedLibPath.put(itPath, true)
268           }
269         }
270       }  
271
272       // withXml changes ignored by buildship, these add the "test=true" attribute
273       withXml {
274         def node = it.asNode()
275         
276         def srcTestAttributes
277         node.children().each{ cpe ->
278           def attributes = cpe.attributes()
279           if (attributes.get("kind") == "src" && attributes.get("path") == "test") {
280             srcTestAttributes = cpe.find { a -> a.name() == "attributes" }
281             return
282           }
283         }
284         def addTestAttribute = true
285         srcTestAttributes.each{a ->
286           if (a.name() == "attribute" && a.attributes().getAt("name") == "test") {
287             addTestAttribute = false
288           }
289         }
290         if (addTestAttribute) {
291           srcTestAttributes.append(new Node(null, "attribute", [name:"test", value:"true"]))
292         }
293
294         node.children().each{ cpe ->
295           def attributes = cpe.attributes()
296           if (attributes.get("kind") == "lib" && attributes.get("path").startsWith("utils/")) {
297             cpe.appendNode('attributes')
298                 .appendNode('attribute', [name:"test", value:"true"])
299           }
300         }
301       } // withXML
302     } // file
303
304     containers 'org.eclipse.buildship.core.gradleclasspathcontainer'
305   } // classpath
306
307   jdt {
308     // for the IDE, use java 11 compatibility
309     sourceCompatibility = compile_source_compatibility
310     targetCompatibility = compile_target_compatibility
311     javaRuntimeName = eclipse_java_runtime_name
312
313     file {
314       withProperties { props ->
315         def jalview_prefs = new Properties()
316         def ins = new FileInputStream(jalviewDirAbsolutePath+"/"+eclipse_extra_jdt_prefs_file)
317         jalview_prefs.load(ins)
318         ins.close()
319         jalview_prefs.forEach { t, v ->
320           if (props.getAt(t) == null) {
321             props.putAt(t, v)
322           }
323         }
324       }
325     }
326   }
327   
328   //synchronizationTasks eclipseClasspath
329   //autoBuildTasks eclipseClasspath
330
331
332 task cloverInstr() {
333   // only instrument source, we build test classes as normal
334   inputs.files files (sourceSets.main.allJava) // , fileTree(dir:"$jalviewDir/$testSourceDir", include: ["**/*.java"]))
335   outputs.dir cloverInstrDir
336
337   doFirst {
338     delete cloverInstrDir
339     def argsList = ["--initstring", "${buildDir}/clover/clover.db",
340       "-d", "${buildDir}/${cloverSourcesInstrDir}"]
341     argsList.addAll(inputs.files.files.collect({ file ->
342       file.absolutePath
343     }))
344     String[] args = argsList.toArray()
345     println("About to instrument "+args.length +" files")
346     com.atlassian.clover.CloverInstr.mainImpl(args)
347   }
348 }
349   
350
351 task cloverReport {
352   group = "Verification"
353   description = "Createst the Clover report"
354   inputs.dir "${buildDir}/clover"
355   outputs.dir "${reportsDir}/clover"
356   onlyIf {
357     file("${buildDir}/clover/clover.db").exists()
358   }
359   doFirst {
360     def argsList = ["--initstring", "${buildDir}/clover/clover.db",
361       "-o", "${reportsDir}/clover"]
362     String[] args = argsList.toArray()
363     com.atlassian.clover.reporters.html.HtmlReporter.runReport(args)
364
365     // and generate ${reportsDir}/clover/clover.xml
366     args = ["--initstring", "${buildDir}/clover/clover.db",
367       "-o", "${reportsDir}/clover/clover.xml"].toArray()
368     com.atlassian.clover.reporters.xml.XMLReporter.runReport(args)
369   }
370 }
371
372 // end clover bits
373
374
375 compileJava {
376
377   doFirst {
378     sourceCompatibility = compile_source_compatibility
379     targetCompatibility = compile_target_compatibility
380     options.compilerArgs = additional_compiler_args
381     print ("Setting target compatibility to "+targetCompatibility+"\n")
382   }
383
384 }
385
386 compileTestJava {
387   if (use_clover) {
388     dependsOn compileCloverJava
389     classpath += configurations.cloverRuntime
390   } else {
391     classpath += sourceSets.main.runtimeClasspath
392   }
393   doFirst {
394     sourceCompatibility = compile_source_compatibility
395     targetCompatibility = compile_target_compatibility
396     options.compilerArgs = additional_compiler_args
397     print ("Setting target compatibility to "+targetCompatibility+"\n")
398   }
399 }
400
401
402 compileCloverJava {
403
404   doFirst {
405     sourceCompatibility = compile_source_compatibility
406     targetCompatibility = compile_target_compatibility
407     options.compilerArgs += additional_compiler_args
408     print ("Setting target compatibility to "+targetCompatibility+"\n")
409   }
410   classpath += configurations.cloverRuntime
411 }
412
413 clean {
414   delete sourceSets.main.java.outputDir
415 }
416
417 cleanTest {
418   delete sourceSets.test.java.outputDir
419   delete cloverInstrDir
420 }
421
422 def getDate(format) {
423   def date = new Date()
424   //return date.format("dd MMMM yyyy")
425   return date.format(format)
426 }
427
428 task setGitVals {
429   def hashStdOut = new ByteArrayOutputStream()
430   exec {
431     commandLine "git", "rev-parse", "--short", "HEAD"
432     standardOutput = hashStdOut
433     ignoreExitValue true
434   }
435
436   def branchStdOut = new ByteArrayOutputStream()
437   exec {
438     commandLine "git", "rev-parse", "--abbrev-ref", "HEAD"
439     standardOutput = branchStdOut
440     ignoreExitValue true
441   }
442
443   project.ext.gitHash = hashStdOut.toString().trim()
444   project.ext.gitBranch = branchStdOut.toString().trim()
445
446   outputs.upToDateWhen { false }
447 }
448
449 task createBuildProperties(type: WriteProperties) {
450   dependsOn setGitVals
451   inputs.dir("$jalviewDir/$sourceDir")
452   inputs.dir("$classes")
453   inputs.dir("$jalviewDir/$resourceDir")
454   outputFile "$classes/$buildPropertiesFile"
455   // taking time specific comment out to allow better incremental builds
456   //comment "--Jalview Build Details--\n"+getDate("yyyy-MM-dd HH:mm:ss")
457   comment "--Jalview Build Details--\n"+getDate("yyyy-MM-dd")
458   property "BUILD_DATE", getDate("dd MMMM yyyy")
459   property "VERSION", JALVIEW_VERSION
460   property "INSTALLATION", INSTALLATION+" git-commit:"+project.ext.gitHash+" ["+project.ext.gitBranch+"]"
461   outputs.file(outputFile)
462 }
463
464 def buildingHTML = "$jalviewDir/$docDir/building.html"
465 task deleteBuildingHTML(type: Delete) {
466   delete buildingHTML
467 }
468
469 task convertBuildingMD(type: Exec) {
470   dependsOn deleteBuildingHTML
471   def buildingMD = "$jalviewDir/$docDir/building.md"
472   def css = "$jalviewDir/$docDir/github.css"
473
474   def pandoc = pandoc_exec
475   def hostname = "hostname".execute().text.trim()
476   if (! file(pandoc).exists() && hostname.equals("jv-bamboo")) {
477     pandoc = System.getProperty("user.home")+"/buildtools/pandoc/bin/pandoc"
478   }
479
480   if (file(pandoc).exists()) {
481     commandLine pandoc, '-s', '-o', buildingHTML, '--metadata', 'pagetitle="Building Jalview from Source"', '--toc', '-H', css, buildingMD
482   } else {
483     commandLine "true"
484   }
485
486   ignoreExitValue true
487
488   inputs.file(buildingMD)
489   inputs.file(css)
490   outputs.file(buildingHTML)
491 }
492 clean {
493   delete buildingHTML
494 }
495
496 task syncDocs(type: Sync) {
497   dependsOn convertBuildingMD
498   def syncDir = "$classes/$docDir"
499   from fileTree("$jalviewDir/$docDir")
500   into syncDir
501
502 }
503
504 def helpFile = "$classes/$helpDir/help.jhm"
505
506 task copyHelp(type: Copy) {
507   def inputDir = "$jalviewDir/$helpParentDir/$helpDir"
508   def outputDir = "$classes/$helpDir"
509   from(inputDir) {
510     exclude '**/*.gif'
511     exclude '**/*.jpg'
512     exclude '**/*.png'
513     filter(ReplaceTokens, beginToken: '$$', endToken: '$$', tokens: ['Version-Rel': JALVIEW_VERSION])
514   }
515   from(inputDir) {
516     include '**/*.gif'
517     include '**/*.jpg'
518     include '**/*.png'
519   }
520   into outputDir
521
522   inputs.dir(inputDir)
523   outputs.files(helpFile)
524   outputs.dir(outputDir)
525 }
526
527 task syncLib(type: Sync) {
528   def syncDir = "$classes/$libDistDir"
529   from fileTree("$jalviewDir/$libDistDir")
530   into syncDir
531 }
532
533 task syncResources(type: Sync) {
534   from "$jalviewDir/$resourceDir"
535   include "**/*.*"
536   exclude "install4j"
537   into "$classes"
538   preserve {
539     include "**"
540   }
541 }
542
543 task prepare {
544   dependsOn syncResources
545   dependsOn syncDocs
546   dependsOn copyHelp
547 }
548
549
550 //testReportDirName = "test-reports" // note that test workingDir will be $jalviewDir
551 test {
552   dependsOn prepare
553   dependsOn compileJava
554   if (use_clover) {
555     dependsOn cloverInstr
556   }
557
558   print("Running tests " + (use_clover?"WITH":"WITHOUT") + " clover [clover="+use_clover+"]\n")
559
560   useTestNG() {
561     includeGroups testngGroups
562     preserveOrder true
563     useDefaultListeners=true
564   }
565
566   workingDir = jalviewDir
567   //systemProperties 'clover.jar' System.properties.clover.jar
568   sourceCompatibility = compile_source_compatibility
569   targetCompatibility = compile_target_compatibility
570   jvmArgs += additional_compiler_args
571   print ("Setting target compatibility to "+targetCompatibility+"\n")
572 }
573
574 task buildIndices(type: JavaExec) {
575   dependsOn copyHelp
576   classpath = sourceSets.main.compileClasspath
577   main = "com.sun.java.help.search.Indexer"
578   workingDir = "$classes/$helpDir"
579   def argDir = "html"
580   args = [ argDir ]
581   inputs.dir("$workingDir/$argDir")
582
583   outputs.dir("$classes/doc")
584   outputs.dir("$classes/help")
585   outputs.file("$workingDir/JavaHelpSearch/DOCS")
586   outputs.file("$workingDir/JavaHelpSearch/DOCS.TAB")
587   outputs.file("$workingDir/JavaHelpSearch/OFFSETS")
588   outputs.file("$workingDir/JavaHelpSearch/POSITIONS")
589   outputs.file("$workingDir/JavaHelpSearch/SCHEMA")
590   outputs.file("$workingDir/JavaHelpSearch/TMAP")
591 }
592
593 task compileLinkCheck(type: JavaCompile) {
594   options.fork = true
595   classpath = files("$jalviewDir/$utilsDir")
596   destinationDir = file("$jalviewDir/$utilsDir")
597   source = fileTree(dir: "$jalviewDir/$utilsDir", include: ["HelpLinksChecker.java", "BufferedLineReader.java"])
598
599   inputs.file("$jalviewDir/$utilsDir/HelpLinksChecker.java")
600   inputs.file("$jalviewDir/$utilsDir/HelpLinksChecker.java")
601   outputs.file("$jalviewDir/$utilsDir/HelpLinksChecker.class")
602   outputs.file("$jalviewDir/$utilsDir/BufferedLineReader.class")
603 }
604
605 def helplinkscheckeroutputfile = file("$jalviewDir/$utilsDir/HelpLinksChecker.out")
606 task linkCheck(type: JavaExec) {
607   dependsOn prepare, compileLinkCheck
608   classpath = files("$jalviewDir/$utilsDir")
609   main = "HelpLinksChecker"
610   workingDir = jalviewDir
611   def help = "$classes/$helpDir"
612   args = [ "$classes/$helpDir", "-nointernet" ]
613
614   doFirst {
615     helplinkscheckeroutputfile.createNewFile()
616     standardOutput new FileOutputStream(helplinkscheckeroutputfile, false)
617   }
618
619   outputs.file(helplinkscheckeroutputfile)
620 }
621
622 task cleanPackageDir(type: Delete) {
623   delete fileTree("$jalviewDir/$packageDir").include("*.jar")
624 }
625
626 jar {
627   dependsOn linkCheck
628   dependsOn buildIndices
629   dependsOn createBuildProperties
630
631   manifest {
632     attributes "Main-Class": mainClass,
633     "Permissions": "all-permissions",
634     "Application-Name": "Jalview Desktop",
635     "Codebase": application_codebase
636   }
637
638   destinationDir = file("$jalviewDir/$packageDir")
639   archiveName = rootProject.name+".jar"
640
641   exclude "cache*/**"
642   exclude "*.jar"
643   exclude "*.jar.*"
644   exclude "**/*.jar"
645   exclude "**/*.jar.*"
646
647   inputs.dir("$classes")
648   outputs.file("$jalviewDir/$packageDir/$archiveName")
649 }
650
651 task copyJars(type: Copy) {
652   from fileTree("$classes").include("**/*.jar").include("*.jar").files
653   into "$jalviewDir/$packageDir"
654 }
655
656 // doing a Sync instead of Copy as Copy doesn't deal with "outputs" very well
657 task syncJars(type: Sync) {
658   from fileTree("$jalviewDir/$libDistDir").include("**/*.jar").include("*.jar").files
659   into "$jalviewDir/$packageDir"
660   preserve {
661     include jar.archiveName
662   }
663 }
664
665 task makeDist {
666   group = "build"
667   description = "Put all required libraries in dist"
668   // order of "cleanPackageDir", "copyJars", "jar" important!
669   jar.mustRunAfter cleanPackageDir
670   syncJars.mustRunAfter cleanPackageDir
671   dependsOn cleanPackageDir
672   dependsOn syncJars
673   dependsOn jar
674   outputs.dir("$jalviewDir/$packageDir")
675 }
676
677 task cleanDist {
678   dependsOn cleanPackageDir
679   dependsOn cleanTest
680   dependsOn clean
681 }
682
683 shadowJar {
684   group = "distribution"
685   dependsOn makeDist
686   from ("$jalviewDir/$libDistDir") {
687     include("*.jar")
688   }
689   mainClassName = shadowJarMainClass
690   mergeServiceFiles()
691   classifier = "all-"+JAVA_VERSION
692   minimize()
693 }
694
695 task getdownWebsite() {
696   group = "distribution"
697   description = "Create the getdown minimal app folder, and website folder for this version of jalview. Website folder also used for offline app installer"
698   dependsOn makeDist
699   def getdownWebsiteResourceFilenames = []
700   def getdownTextString = ""
701   def getdownResourceDir = project.ext.getdownResourceDir
702   def getdownAppDir = project.ext.getdownAppDir
703   def getdownResourceFilenames = []
704   doFirst {
705     // go through properties looking for getdown_txt_...
706     def props = project.properties.sort { it.key }
707     props.put("getdown_txt_java_min_version", getdown_alt_java_min_version)
708     props.put("getdown_txt_multi_java_location", getdown_alt_multi_java_location)
709
710     if (getdown_local == "true") {
711       getdown_app_base = file(getdownWebsiteDir).toURI().toString()
712     }
713     props.put("getdown_txt_appbase", getdown_app_base)
714     props.each{ prop, val ->
715       if (prop.startsWith("getdown_txt_") && val != null) {
716         if (prop.startsWith("getdown_txt_multi_")) {
717           def key = prop.substring(18)
718           val.split(",").each{ v ->
719             def line = key + " = " + v + "\n"
720             getdownTextString += line
721           }
722         } else {
723           // file values rationalised
724           if (val.indexOf('/') > -1) {
725             def r = null
726             if (val.indexOf('/') == 0) {
727               // absolute path
728               r = file(val)
729             } else if (val.indexOf('/') > 0) {
730               // relative path (relative to jalviewDir)
731               r = file( jalviewDir + '/' + val )
732             }
733             if (r.exists()) {
734               val = getdown_resource_dir + '/' + r.getName()
735               getdownWebsiteResourceFilenames += val
736               getdownResourceFilenames += r.getPath()
737             }
738           }
739           def line = prop.substring(12) + " = " + val + "\n"
740           getdownTextString += line
741         }
742       }
743     }
744
745     getdownWebsiteResourceFilenames.each{ filename ->
746       getdownTextString += "resource = "+filename+"\n"
747     }
748     getdownResourceFilenames.each{ filename ->
749       copy {
750         from filename
751         into project.ext.getdownResourceDir
752       }
753     }
754
755     def codeFiles = []
756     makeDist.outputs.files.each{ f ->
757       if (f.isDirectory()) {
758         def files = fileTree(dir: f, include: ["*"]).getFiles()
759         codeFiles += files
760       } else if (f.exists()) {
761         codeFiles += f
762       }
763     }
764     codeFiles.sort().each{f ->
765       def line = "code = " + getdown_app_dir + '/' + f.getName() + "\n"
766       getdownTextString += line
767       copy {
768         from f.getPath()
769         into project.ext.getdownAppDir
770       }
771     }
772
773     // NOT USING MODULES YET, EVERYTHING SHOULD BE IN dist
774     /*
775      if (JAVA_VERSION.equals("11")) {
776      def j11libFiles = fileTree(dir: "$jalviewDir/$j11libDir", include: ["*.jar"]).getFiles()
777      j11libFiles.sort().each{f ->
778      def line = "code = " + getdown_j11lib_dir + '/' + f.getName() + "\n"
779      getdownTextString += line
780      copy {
781      from f.getPath()
782      into project.ext.getdownJ11libDir
783      }
784      }
785      }
786      */
787
788     // 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.
789     //getdownTextString += "class = " + file(getdownLauncher).getName() + "\n"
790     getdownTextString += "resource = " + file(getdownLauncher).getName() + "\n"
791     getdownTextString += "class = " + mainClass + "\n"
792
793     def getdown_txt = file(project.ext.getdownWebsiteDir + "/getdown.txt")
794     getdown_txt.write(getdownTextString)
795
796     copy {
797       from getdown_txt
798       into project.ext.getdownFilesDir
799     }
800
801     copy {
802       from getdownLauncher
803       into project.ext.getdownFilesDir
804     }
805
806     copy {
807       from getdownLauncher
808       into project.ext.getdownWebsiteDir
809     }
810
811     copy {
812       from jalviewDir + '/' + project.getProperty('getdown_txt_ui.background_image')
813       from jalviewDir + '/' + project.getProperty('getdown_txt_ui.error_background')
814       from jalviewDir + '/' + project.getProperty('getdown_txt_ui.progress_image')
815       from jalviewDir + '/' + project.getProperty('getdown_txt_ui.icon')
816       from jalviewDir + '/' + project.getProperty('getdown_txt_ui.mac_dock_icon')
817       into project.ext.getdownFilesDir + '/' + getdown_resource_dir
818     }
819   }
820
821   inputs.dir(jalviewDir + '/' + packageDir)
822   outputs.dir(project.ext.getdownWebsiteDir)
823   outputs.dir(project.ext.getdownFilesDir)
824 }
825
826 task getdownDigest(type: JavaExec) {
827   group = "distribution"
828   description = "Digest the getdown website folder"
829   dependsOn getdownWebsite
830   classpath = files(jalviewDir + '/' + getdown_core, jalviewDir+'/'+getdown_launcher)
831   main = "com.threerings.getdown.tools.Digester"
832   args project.ext.getdownWebsiteDir
833   inputs.dir(project.ext.getdownWebsiteDir)
834   outputs.file(project.ext.getdownWebsiteDir + '/' + "digest2.txt")
835 }
836
837 task getdown() {
838   group = "distribution"
839   description = "Create the minimal and full getdown app folder for installers and website and create digest file"
840   dependsOn getdownDigest
841 }
842
843 clean {
844   delete project.ext.getdownWebsiteDir
845   delete project.ext.getdownFilesDir
846 }
847
848 install4j {
849   def install4jHomeDir = "/opt/install4j"
850   def hostname = "hostname".execute().text.trim()
851   if (hostname.equals("jv-bamboo")) {
852     install4jHomeDir = System.getProperty("user.home")+"/buildtools/install4j"
853   } else if (OperatingSystem.current().isMacOsX()) {
854     install4jHomeDir = '/Applications/install4j.app/Contents/Resources/app'
855     if (! file(install4jHomeDir).exists()) {
856       install4jHomeDir = System.getProperty("user.home")+install4jHomeDir
857     }
858   } else if (OperatingSystem.current().isLinux()) {
859     install4jHomeDir = System.getProperty("user.home")+"/buildtools/install4j"
860   }
861   installDir = file(install4jHomeDir)
862   mediaTypes = Arrays.asList(install4jMediaTypes.split(","))
863   if (install4jFaster.equals("true")) {
864     faster = true
865   }
866 }
867
868 def install4jConf
869 def macosJavaVMDir
870 def macosJavaVMTgz
871 def windowsJavaVMDir
872 def windowsJavaVMTgz
873 def install4jDir = "$jalviewDir/$install4jResourceDir"
874 def install4jConfFile = "jalview-installers-java"+JAVA_VERSION+".install4j"
875 install4jConf = "$install4jDir/$install4jConfFile"
876
877 task copyInstall4jTemplate(type: Copy) {
878   macosJavaVMDir = System.env.HOME+"/buildtools/jre/openjdk-java_vm/getdown/macos-jre"+JAVA_VERSION+"/jre"
879   macosJavaVMTgz = System.env.HOME+"/buildtools/jre/openjdk-java_vm/install4j/tgz/macos-jre"+JAVA_VERSION+".tar.gz"
880   windowsJavaVMDir = System.env.HOME+"/buildtools/jre/openjdk-java_vm/getdown/windows-jre"+JAVA_VERSION+"/jre"
881   windowsJavaVMTgz = System.env.HOME+"/buildtools/jre/openjdk-java_vm/install4j/tgz/windows-jre"+JAVA_VERSION+".tar.gz"
882   from (install4jDir) {
883     include install4jTemplate
884     rename (install4jTemplate, install4jConfFile)
885     filter(ReplaceTokens, beginToken: '', endToken: '', tokens: ['9999999999': JAVA_VERSION])
886     filter(ReplaceTokens, beginToken: '$$', endToken: '$$',
887       tokens: [
888         'JAVA_VERSION': JAVA_VERSION,
889         'JAVA_INTEGER_VERSION': JAVA_INTEGER_VERSION,
890         'VERSION': JALVIEW_VERSION,
891         'MACOS_JAVA_VM_DIR': macosJavaVMDir,
892         'MACOS_JAVA_VM_TGZ': macosJavaVMTgz,
893         'WINDOWS_JAVA_VM_DIR': windowsJavaVMDir,
894         'WINDOWS_JAVA_VM_TGZ': windowsJavaVMTgz,
895         'INSTALL4JINFOPLISTFILEASSOCIATIONS': install4jInfoPlistFileAssociations,
896         'COPYRIGHT_MESSAGE': install4jCopyrightMessage,
897         'MACOS_BUNDLE_ID': install4jMacOSBundleId
898       ]
899     )
900     if (OSX_KEYPASS=="") {
901       filter(ReplaceTokens, beginToken: 'codeSigning macEnabled="', endToken: '"', tokens: ['true':'codeSigning macEnabled="false"'])
902       filter(ReplaceTokens, beginToken: 'runPostProcessor="true" ',endToken: 'Processor', tokens: ['post':'runPostProcessor="false" postProcessor'])
903     }
904   }
905   into install4jDir
906   outputs.files(install4jConf)
907
908   doLast {
909     // include file associations in installer
910     def installerFileAssociationsXml = file("$install4jDir/$install4jInstallerFileAssociations").text
911     ant.replaceregexp(
912       byline: false,
913       flags: "s",
914       match: '<action name="EXTENSIONS_REPLACED_BY_GRADLE".*?</action>',
915       replace: installerFileAssociationsXml,
916       file: install4jConf
917     )
918     /*
919     // include uninstaller applescript app files in dmg
920     def installerDMGUninstallerXml = file("$install4jDir/$install4jDMGUninstallerAppFiles").text
921     ant.replaceregexp(
922       byline: false,
923       flags: "s",
924       match: '<file name="UNINSTALL_OLD_JALVIEW_APP_REPLACED_IN_GRADLE" file=.*?>',
925       replace: installerDMGUninstallerXml,
926       file: install4jConf
927     )
928     */
929   }
930 }
931
932 task installers(type: com.install4j.gradle.Install4jTask) {
933   group = "distribution"
934   description = "Create the install4j installers"
935   dependsOn getdown
936   dependsOn copyInstall4jTemplate
937   projectFile = file(install4jConf)
938   println("Using projectFile "+projectFile)
939   variables = [majorVersion: version.substring(2, 11), build: 001, OSX_KEYSTORE: OSX_KEYSTORE, JSIGN_SH: JSIGN_SH]
940   destination = "$jalviewDir/$install4jBuildDir/$JAVA_VERSION"
941   buildSelected = true
942
943   if (OSX_KEYPASS) {
944     macKeystorePassword=OSX_KEYPASS
945     
946   }
947   
948   inputs.dir(project.ext.getdownWebsiteDir)
949   inputs.file(install4jConf)
950   inputs.dir(macosJavaVMDir)
951   inputs.dir(windowsJavaVMDir)
952   outputs.dir("$jalviewDir/$install4jBuildDir/$JAVA_VERSION")
953 }
954
955 clean {
956   delete install4jConf
957 }