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