JAL-3315 getdown_1_8_6 patch file. Needs careful applying.
[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 = pandoc_exec
488   def hostname = "hostname".execute().text.trim()
489   if (! file(pandoc).exists() && hostname.equals("jv-bamboo")) {
490     pandoc = System.getProperty("user.home")+"/buildtools/pandoc/bin/pandoc"
491   }
492
493   if (file(pandoc).exists()) {
494     commandLine pandoc, '-s', '-o', buildingHTML, '--metadata', 'pagetitle="Building Jalview from Source"', '--toc', '-H', css, buildingMD
495   } else {
496     commandLine "true"
497   }
498
499   ignoreExitValue true
500
501   inputs.file(buildingMD)
502   inputs.file(css)
503   outputs.file(buildingHTML)
504 }
505 clean {
506   delete buildingHTML
507 }
508
509 task syncDocs(type: Sync) {
510   dependsOn convertBuildingMD
511   def syncDir = "$classes/$docDir"
512   from fileTree("$jalviewDir/$docDir")
513   into syncDir
514
515 }
516
517 def helpFile = "$classes/$helpDir/help.jhm"
518
519 task copyHelp(type: Copy) {
520   def inputDir = "$jalviewDir/$helpParentDir/$helpDir"
521   def outputDir = "$classes/$helpDir"
522   from(inputDir) {
523     exclude '**/*.gif'
524     exclude '**/*.jpg'
525     exclude '**/*.png'
526     filter(ReplaceTokens, beginToken: '$$', endToken: '$$', tokens: ['Version-Rel': JALVIEW_VERSION])
527   }
528   from(inputDir) {
529     include '**/*.gif'
530     include '**/*.jpg'
531     include '**/*.png'
532   }
533   into outputDir
534
535   inputs.dir(inputDir)
536   outputs.files(helpFile)
537   outputs.dir(outputDir)
538 }
539
540 task syncLib(type: Sync) {
541   def syncDir = "$classes/$libDistDir"
542   from fileTree("$jalviewDir/$libDistDir")
543   into syncDir
544 }
545
546 task syncResources(type: Sync) {
547   from "$jalviewDir/$resourceDir"
548   include "**/*.*"
549   exclude "install4j"
550   into "$classes"
551   preserve {
552     include "**"
553   }
554 }
555
556 task prepare {
557   dependsOn syncResources
558   dependsOn syncDocs
559   dependsOn copyHelp
560 }
561
562
563 //testReportDirName = "test-reports" // note that test workingDir will be $jalviewDir
564 test {
565   dependsOn prepare
566   dependsOn compileJava
567   if (use_clover) {
568     dependsOn cloverInstr
569   }
570
571   print("Running tests " + (use_clover?"WITH":"WITHOUT") + " clover [clover="+use_clover+"]\n")
572
573   useTestNG() {
574     includeGroups testngGroups
575     preserveOrder true
576     useDefaultListeners=true
577   }
578
579   workingDir = jalviewDir
580   //systemProperties 'clover.jar' System.properties.clover.jar
581   sourceCompatibility = compile_source_compatibility
582   targetCompatibility = compile_target_compatibility
583   jvmArgs += additional_compiler_args
584   print ("Setting target compatibility to "+targetCompatibility+"\n")
585 }
586
587 task buildIndices(type: JavaExec) {
588   dependsOn copyHelp
589   classpath = sourceSets.main.compileClasspath
590   main = "com.sun.java.help.search.Indexer"
591   workingDir = "$classes/$helpDir"
592   def argDir = "html"
593   args = [ argDir ]
594   inputs.dir("$workingDir/$argDir")
595
596   outputs.dir("$classes/doc")
597   outputs.dir("$classes/help")
598   outputs.file("$workingDir/JavaHelpSearch/DOCS")
599   outputs.file("$workingDir/JavaHelpSearch/DOCS.TAB")
600   outputs.file("$workingDir/JavaHelpSearch/OFFSETS")
601   outputs.file("$workingDir/JavaHelpSearch/POSITIONS")
602   outputs.file("$workingDir/JavaHelpSearch/SCHEMA")
603   outputs.file("$workingDir/JavaHelpSearch/TMAP")
604 }
605
606 task compileLinkCheck(type: JavaCompile) {
607   options.fork = true
608   classpath = files("$jalviewDir/$utilsDir")
609   destinationDir = file("$jalviewDir/$utilsDir")
610   source = fileTree(dir: "$jalviewDir/$utilsDir", include: ["HelpLinksChecker.java", "BufferedLineReader.java"])
611
612   inputs.file("$jalviewDir/$utilsDir/HelpLinksChecker.java")
613   inputs.file("$jalviewDir/$utilsDir/HelpLinksChecker.java")
614   outputs.file("$jalviewDir/$utilsDir/HelpLinksChecker.class")
615   outputs.file("$jalviewDir/$utilsDir/BufferedLineReader.class")
616 }
617
618 def helplinkscheckeroutputfile = file("$jalviewDir/$utilsDir/HelpLinksChecker.out")
619 task linkCheck(type: JavaExec) {
620   dependsOn prepare, compileLinkCheck
621   classpath = files("$jalviewDir/$utilsDir")
622   main = "HelpLinksChecker"
623   workingDir = jalviewDir
624   def help = "$classes/$helpDir"
625   args = [ "$classes/$helpDir", "-nointernet" ]
626
627   doFirst {
628     helplinkscheckeroutputfile.createNewFile()
629     standardOutput new FileOutputStream(helplinkscheckeroutputfile, false)
630   }
631
632   outputs.file(helplinkscheckeroutputfile)
633 }
634
635 task cleanPackageDir(type: Delete) {
636   delete fileTree("$jalviewDir/$packageDir").include("*.jar")
637 }
638
639 jar {
640   dependsOn linkCheck
641   dependsOn buildIndices
642   dependsOn createBuildProperties
643
644   manifest {
645     attributes "Main-Class": mainClass,
646     "Permissions": "all-permissions",
647     "Application-Name": "Jalview Desktop",
648     "Codebase": application_codebase
649   }
650
651   destinationDir = file("$jalviewDir/$packageDir")
652   archiveName = rootProject.name+".jar"
653
654   exclude "cache*/**"
655   exclude "*.jar"
656   exclude "*.jar.*"
657   exclude "**/*.jar"
658   exclude "**/*.jar.*"
659
660   inputs.dir("$classes")
661   outputs.file("$jalviewDir/$packageDir/$archiveName")
662 }
663
664 task copyJars(type: Copy) {
665   from fileTree("$classes").include("**/*.jar").include("*.jar").files
666   into "$jalviewDir/$packageDir"
667 }
668
669 // doing a Sync instead of Copy as Copy doesn't deal with "outputs" very well
670 task syncJars(type: Sync) {
671   from fileTree("$jalviewDir/$libDistDir").include("**/*.jar").include("*.jar").files
672   into "$jalviewDir/$packageDir"
673   preserve {
674     include jar.archiveName
675   }
676 }
677
678 task makeDist {
679   group = "build"
680   description = "Put all required libraries in dist"
681   // order of "cleanPackageDir", "copyJars", "jar" important!
682   jar.mustRunAfter cleanPackageDir
683   syncJars.mustRunAfter cleanPackageDir
684   dependsOn cleanPackageDir
685   dependsOn syncJars
686   dependsOn jar
687   outputs.dir("$jalviewDir/$packageDir")
688 }
689
690 task cleanDist {
691   dependsOn cleanPackageDir
692   dependsOn cleanTest
693   dependsOn clean
694 }
695
696 shadowJar {
697   group = "distribution"
698   dependsOn makeDist
699   from ("$jalviewDir/$libDistDir") {
700     include("*.jar")
701   }
702   mainClassName = shadowJarMainClass
703   mergeServiceFiles()
704   classifier = "all-"+JAVA_VERSION
705   minimize()
706 }
707
708 task getdownWebsite() {
709   group = "distribution"
710   description = "Create the getdown minimal app folder, and website folder for this version of jalview. Website folder also used for offline app installer"
711   dependsOn makeDist
712   def getdownWebsiteResourceFilenames = []
713   def getdownTextString = ""
714   def getdownResourceDir = project.ext.getdownResourceDir
715   def getdownAppDir = project.ext.getdownAppDir
716   def getdownResourceFilenames = []
717   doFirst {
718     // go through properties looking for getdown_txt_...
719     def props = project.properties.sort { it.key }
720     props.put("getdown_txt_java_min_version", getdown_alt_java_min_version)
721     props.put("getdown_txt_multi_java_location", getdown_alt_multi_java_location)
722
723     if (getdown_local == "true") {
724       getdown_app_base = file(getdownWebsiteDir).toURI().toString()
725     }
726     props.put("getdown_txt_appbase", getdown_app_base)
727     props.each{ prop, val ->
728       if (prop.startsWith("getdown_txt_") && val != null) {
729         if (prop.startsWith("getdown_txt_multi_")) {
730           def key = prop.substring(18)
731           val.split(",").each{ v ->
732             def line = key + " = " + v + "\n"
733             getdownTextString += line
734           }
735         } else {
736           // file values rationalised
737           if (val.indexOf('/') > -1) {
738             def r = null
739             if (val.indexOf('/') == 0) {
740               // absolute path
741               r = file(val)
742             } else if (val.indexOf('/') > 0) {
743               // relative path (relative to jalviewDir)
744               r = file( jalviewDir + '/' + val )
745             }
746             if (r.exists()) {
747               val = getdown_resource_dir + '/' + r.getName()
748               getdownWebsiteResourceFilenames += val
749               getdownResourceFilenames += r.getPath()
750             }
751           }
752           def line = prop.substring(12) + " = " + val + "\n"
753           getdownTextString += line
754         }
755       }
756     }
757
758     getdownWebsiteResourceFilenames.each{ filename ->
759       getdownTextString += "resource = "+filename+"\n"
760     }
761     getdownResourceFilenames.each{ filename ->
762       copy {
763         from filename
764         into project.ext.getdownResourceDir
765       }
766     }
767
768     def codeFiles = []
769     makeDist.outputs.files.each{ f ->
770       if (f.isDirectory()) {
771         def files = fileTree(dir: f, include: ["*"]).getFiles()
772         codeFiles += files
773       } else if (f.exists()) {
774         codeFiles += f
775       }
776     }
777     codeFiles.sort().each{f ->
778       def line = "code = " + getdown_app_dir + '/' + f.getName() + "\n"
779       getdownTextString += line
780       copy {
781         from f.getPath()
782         into project.ext.getdownAppDir
783       }
784     }
785
786     // NOT USING MODULES YET, EVERYTHING SHOULD BE IN dist
787     /*
788      if (JAVA_VERSION.equals("11")) {
789      def j11libFiles = fileTree(dir: "$jalviewDir/$j11libDir", include: ["*.jar"]).getFiles()
790      j11libFiles.sort().each{f ->
791      def line = "code = " + getdown_j11lib_dir + '/' + f.getName() + "\n"
792      getdownTextString += line
793      copy {
794      from f.getPath()
795      into project.ext.getdownJ11libDir
796      }
797      }
798      }
799      */
800
801     // 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.
802     //getdownTextString += "class = " + file(getdownLauncher).getName() + "\n"
803     getdownTextString += "resource = " + file(getdownLauncher).getName() + "\n"
804     getdownTextString += "class = " + mainClass + "\n"
805
806     def getdown_txt = file(project.ext.getdownWebsiteDir + "/getdown.txt")
807     getdown_txt.write(getdownTextString)
808
809     copy {
810       from getdown_txt
811       into project.ext.getdownFilesDir
812     }
813
814     copy {
815       from getdownLauncher
816       into project.ext.getdownFilesDir
817     }
818
819     copy {
820       from getdownLauncher
821       into project.ext.getdownWebsiteDir
822     }
823
824     copy {
825       from jalviewDir + '/' + project.getProperty('getdown_txt_ui.background_image')
826       from jalviewDir + '/' + project.getProperty('getdown_txt_ui.error_background')
827       from jalviewDir + '/' + project.getProperty('getdown_txt_ui.progress_image')
828       from jalviewDir + '/' + project.getProperty('getdown_txt_ui.icon')
829       from jalviewDir + '/' + project.getProperty('getdown_txt_ui.mac_dock_icon')
830       into project.ext.getdownFilesDir + '/' + getdown_resource_dir
831     }
832   }
833
834   inputs.dir(jalviewDir + '/' + packageDir)
835   outputs.dir(project.ext.getdownWebsiteDir)
836   outputs.dir(project.ext.getdownFilesDir)
837 }
838
839 task getdownDigest(type: JavaExec) {
840   group = "distribution"
841   description = "Digest the getdown website folder"
842   dependsOn getdownWebsite
843   classpath = files(jalviewDir + '/' + getdown_core, jalviewDir+'/'+getdown_launcher)
844   main = "com.threerings.getdown.tools.Digester"
845   args project.ext.getdownWebsiteDir
846   inputs.dir(project.ext.getdownWebsiteDir)
847   outputs.file(project.ext.getdownWebsiteDir + '/' + "digest2.txt")
848 }
849
850 task getdown() {
851   group = "distribution"
852   description = "Create the minimal and full getdown app folder for installers and website and create digest file"
853   dependsOn getdownDigest
854 }
855
856 clean {
857   delete project.ext.getdownWebsiteDir
858   delete project.ext.getdownFilesDir
859 }
860
861 install4j {
862   def install4jHomeDir = "/opt/install4j"
863   def hostname = "hostname".execute().text.trim()
864   if (hostname.equals("jv-bamboo")) {
865     install4jHomeDir = System.getProperty("user.home")+"/buildtools/install4j"
866   } else if (OperatingSystem.current().isMacOsX()) {
867     install4jHomeDir = '/Applications/install4j.app/Contents/Resources/app'
868     if (! file(install4jHomeDir).exists()) {
869       install4jHomeDir = System.getProperty("user.home")+install4jHomeDir
870     }
871   } else if (OperatingSystem.current().isLinux()) {
872     install4jHomeDir = System.getProperty("user.home")+"/buildtools/install4j"
873   }
874   installDir = file(install4jHomeDir)
875   mediaTypes = Arrays.asList(install4jMediaTypes.split(","))
876   if (install4jFaster.equals("true")) {
877     faster = true
878   }
879 }
880
881 def install4jConf
882 def macosJavaVMDir
883 def macosJavaVMTgz
884 def windowsJavaVMDir
885 def windowsJavaVMTgz
886 def install4jDir = "$jalviewDir/$install4jResourceDir"
887 def install4jConfFile = "jalview-installers-java"+JAVA_VERSION+".install4j"
888 install4jConf = "$install4jDir/$install4jConfFile"
889
890 task copyInstall4jTemplate(type: Copy) {
891   macosJavaVMDir = System.env.HOME+"/buildtools/jre/openjdk-java_vm/getdown/macos-jre"+JAVA_VERSION+"/jre"
892   macosJavaVMTgz = System.env.HOME+"/buildtools/jre/openjdk-java_vm/install4j/tgz/macos-jre"+JAVA_VERSION+".tar.gz"
893   windowsJavaVMDir = System.env.HOME+"/buildtools/jre/openjdk-java_vm/getdown/windows-jre"+JAVA_VERSION+"/jre"
894   windowsJavaVMTgz = System.env.HOME+"/buildtools/jre/openjdk-java_vm/install4j/tgz/windows-jre"+JAVA_VERSION+".tar.gz"
895   from (install4jDir) {
896     include install4jTemplate
897     rename (install4jTemplate, install4jConfFile)
898     filter(ReplaceTokens, beginToken: '', endToken: '', tokens: ['9999999999': JAVA_VERSION])
899     filter(ReplaceTokens, beginToken: '$$', endToken: '$$',
900       tokens: [
901         'JAVA_VERSION': JAVA_VERSION,
902         'JAVA_INTEGER_VERSION': JAVA_INTEGER_VERSION,
903         'VERSION': JALVIEW_VERSION,
904         'MACOS_JAVA_VM_DIR': macosJavaVMDir,
905         'MACOS_JAVA_VM_TGZ': macosJavaVMTgz,
906         'WINDOWS_JAVA_VM_DIR': windowsJavaVMDir,
907         'WINDOWS_JAVA_VM_TGZ': windowsJavaVMTgz,
908         'INSTALL4JINFOPLISTFILEASSOCIATIONS': install4jInfoPlistFileAssociations,
909         'COPYRIGHT_MESSAGE': install4jCopyrightMessage,
910         'MACOS_BUNDLE_ID': install4jMacOSBundleId
911       ]
912     )
913     if (OSX_KEYPASS=="") {
914       filter(ReplaceTokens, beginToken: 'codeSigning macEnabled="', endToken: '"', tokens: ['true':'codeSigning macEnabled="false"'])
915       filter(ReplaceTokens, beginToken: 'runPostProcessor="true" ',endToken: 'Processor', tokens: ['post':'runPostProcessor="false" postProcessor'])
916     }
917   }
918   into install4jDir
919   outputs.files(install4jConf)
920
921   doLast {
922     // include file associations in installer
923     def installerFileAssociationsXml = file("$install4jDir/$install4jInstallerFileAssociations").text
924     ant.replaceregexp(
925       byline: false,
926       flags: "s",
927       match: '<action name="EXTENSIONS_REPLACED_BY_GRADLE".*?</action>',
928       replace: installerFileAssociationsXml,
929       file: install4jConf
930     )
931     /*
932     // include uninstaller applescript app files in dmg
933     def installerDMGUninstallerXml = file("$install4jDir/$install4jDMGUninstallerAppFiles").text
934     ant.replaceregexp(
935       byline: false,
936       flags: "s",
937       match: '<file name="UNINSTALL_OLD_JALVIEW_APP_REPLACED_IN_GRADLE" file=.*?>',
938       replace: installerDMGUninstallerXml,
939       file: install4jConf
940     )
941     */
942   }
943 }
944
945 task installers(type: com.install4j.gradle.Install4jTask) {
946   group = "distribution"
947   description = "Create the install4j installers"
948   dependsOn getdown
949   dependsOn copyInstall4jTemplate
950   projectFile = file(install4jConf)
951   println("Using projectFile "+projectFile)
952   variables = [majorVersion: version.substring(2, 11), build: 001, OSX_KEYSTORE: OSX_KEYSTORE, JSIGN_SH: JSIGN_SH]
953   destination = "$jalviewDir/$install4jBuildDir/$JAVA_VERSION"
954   buildSelected = true
955
956   if (OSX_KEYPASS) {
957     macKeystorePassword=OSX_KEYPASS
958     
959   }
960   
961   inputs.dir(project.ext.getdownWebsiteDir)
962   inputs.file(install4jConf)
963   inputs.dir(macosJavaVMDir)
964   inputs.dir(windowsJavaVMDir)
965   outputs.dir("$jalviewDir/$install4jBuildDir/$JAVA_VERSION")
966 }
967
968 clean {
969   delete install4jConf
970 }