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